spring 使用 ehcache
第一种方式:注解
@Cacheable(value = "baseCache",key = "'baseCache'")
第二种方式:手动管理
Element el = null;
CacheManager manager = CacheManager.create();
Cache cache = manager.getCache("baseCache");
if(cache.isKeyInCache("baseCache")){
el = cache.get("baseCache");
return (List<String>) el.getObjectValue();
}
List<String> list=new ArrayList();
el = new Element("HotKeyWordsMapper.defaultlist", list);
cache.put(el);
return list;
本文主要介绍第一种,
<!--
cache-manager属性用来指定当前所使用的CacheManager对应的bean的名称,默认是cacheManager这里使用springCacheManager
annotation-driven还有一个proxy-target-class属性,表示是否要代理class,默认为false。我们前面提到的@Cacheable、@cacheEvict等也可以标注在接口上,这对于基于接口的代理来说是没有什么问题的,但是需要注意的是当我们设置proxy-target-class为true或者mode为aspectj时,是直接基于class进行操作的,定义在接口上的@Cacheable等Cache注解不会被识别到,那对应的Spring Cache也不会起作用了。
-->
<cache:annotation-driven cache-manager="springCacheManager" />
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<!-- shared=true
EhCacheManagerFactoryBean内部在得到shared信息就不会重复重复创建cache了。
-->
<property name="shared" value="true"></property>
<property name="cacheManagerName" value="ehcache"/>
</bean>
<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
<property name="transactionAware" value="true"/>
</bean>
特别注意:
1、
cache配置放在ApplicationContext.xml放在mvc配置文件中有时会出现@Cacheable无效
2、
mvc.xml中需要添加<cache:annotation-driven cache-manager="springCacheManager" /> 不然@Cacheable无效
3、
如果使用了 shiro需要添加 下面代码方式重复创建。
<property name="shared" value="true"></property>
4、
class AAAA{
@Cacheable
public void a(){
b();
}
@Cacheable
public void b(){
}
}
这种情况下 a()调用b(),a中缓存有效b中缓存是无效的,和 @Transactional类似
附ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir" /> <!-- 缓存存放目录(此目录为放入系统默认缓存目录),也可以是”D:/cache“ java.io.tmpdir -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="baseCache"
eternal="false"
maxEntriesLocalHeap="20000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="600"
statistics="true"
timeToLiveSeconds="600"/>
<!--
name:Cache的唯一标识
maxElementsInMemory:内存中最大缓存对象数
maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大
eternal:Element是否永久有效,一但设置了,timeout将不起作用
overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大
timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大
diskPersistent:是否缓存虚拟机重启期数据
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)
-->
</ehcache>