spring + ehcache 基本配置

2018-05-08  本文已影响5人  bbmm

appcontent.xml
echache.xml

spring 中配置ehcache的 cacheFactory 和 cacheManager
<!-- 配置缓存 -->
    <cache:annotation-driven cache-manager="cacheManager"/>
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation" value="classpath:ehcache.xml"></property>  
    </bean>  
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
        <property name="cacheManager" ref="ehcache"></property>  
    </bean> 
1.value="xxx" xxx为ehcache.xml中配置的cache 的name 如下
<cache name="baseCache" maxElementsInMemory="0" 
        eternal="true"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        diskSpoolBufferSizeMB="100"
         maxElementsOnDisk="0"
        diskPersistent="false" 
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LFU" />
2、key为这个方法返回结果放入ehcache中的key ,注意点#userID
为方法参数 'menu_user' 为key的自定义前缀,注意必须要加''负责会报错说这个key的el解析异常
在需要配置缓存的方法上增加@cacheable
 @Cacheable(value="baseCache" ,key="'menu_user_'+#userID")
        public List<String> GetUserMenu(String userID){
            return Aarray.asList("","");
}
上一篇 下一篇

猜你喜欢

热点阅读