@Cachebale没有使用Ehcache和spring MVC

时间:2017-12-08 09:16:21

标签: spring spring-mvc caching ehcache

@Cacheble无效。我在Spring MVC Project中使用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) {

}

请咨询

1 个答案:

答案 0 :(得分:0)

@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;
    }

}

进一步阅读: