【发布时间】: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