【问题标题】:@Cachebale not working with Ehcache and spring MVC@Cachebale 不适用于 Ehcache 和 spring MVC
【发布时间】:2018-05-22 12:08:15
【问题描述】:

@Cacheble 不工作。我在 Spring MVC 项目中使用 ehcache。我的服务充当 DAO。使用 Spring 4 和 ehcache 2.10 版本。请看下面的实现

app-ctx.xml -- 应用程序上下文 xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.0.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd        
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <cache:annotation-driven />  
 <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
        <property name="shared" value="true"/>
    </bean> 

请看下面的ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" 
    updateCheck="true"
    monitoring="autodetect" 
    dynamicConfig="true">

    <diskStore path="java.io.tmpdir" />

    <cache name="timeTrackerDashBoardCountCache" 
        maxEntriesLocalHeap="100"
        maxEntriesLocalDisk="1000" 
        eternal="false" 
        timeToIdleSeconds="300" 
        timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU" 
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>

请参阅下面的服务类TelleCallerDashboardService.java

@Service
public class TelleCallerDashboardService extends BaseNamedParameterJdbcDaoSupport {

 @Cacheable(value = "timeTrackerDashBoardCountCache", key="#league")
    public void timeTrackerDashBoardCount(final TelleCallerDashboardDTO telleCallerDashboardDTO) {

}

请指教

【问题讨论】:

  • 应该用void 方法缓存什么?此外,您的 @Cacheable 对您调用的方法没有意义。
  • 您需要输入参数的返回类型和键名,即telleCallerDashboardDTO,而您缺少@CacheConfig
  • 方法 timeTrackerDahshBoardCount 不返回任何内容,对象 telleCallerDashboardDTO 是从调用方法传递的,它使用对象 telleCallerDashboardDTO 来存储数据库中的值。调用方法获取更新的值。我将使用返回值和 key=telleCallerDashboardDTO 测试代码。并添加@CacheConfig ...谢谢
  • 嗨我修改了@CacheConfig(cacheNames = "TelleCallerDashboard") public class TelleCallerDashboardService extends BaseNamedParameterJdbcDaoSupport { @Cacheable(value = "timeTrackerDashBoardCountCache", key="telleCallerDashboardDTO") public TelleCallerDashboardDTO timeTrackerDashBoardCount(final TelleCallerDashboardDTO telleCallerDashboardDTO System.out.println("********** timeTrackerDashBoardCountCache at " + DateUtility.dateToStringWithTime(new Date()));
  • @M. Deinum 请建议

标签: spring spring-mvc caching ehcache


【解决方案1】:

@Cacheable 用于划分可缓存的方法 - 即将结果存储到缓存中的方法等后续调用(使用相同的参数) ),则无需实际执行方法即可返回缓存中的值。

由于您的方法没有返回任何内容,因为它被标记为 void。 什么都不会放入缓存中。

如果你想使用@Cacheable 或@CachePut,你需要返回一些带有缓存注释的方法。

对于工作代码:

@Service
@CacheConfig(cacheNames = "TelleCallerDashboard") 
public class TelleCallerDashboardService extends BaseNamedParameterJdbcDaoSupport {

 @Cacheable(value = "timeTrackerDashBoardCountCache", key="#telleCallerDashboardDTO")
    public AnyReturnClass timeTrackerDashBoardCount(final TelleCallerDashboardDTO telleCallerDashboardDTO) {

        return anyReturnClassObject;
    }

}

进一步阅读:

【讨论】:

    猜你喜欢
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 2018-03-21
    • 2017-07-15
    • 2015-01-11
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多