基于方法的缓存,顾名思义;
就是缓存的级别是在方法上,并且调用的方法的参数必须相同,那么直接从缓存中取数据,反之不取缓存中的数据。
最近查询了些资料,总结了下如何用Spring与Ehcache简单配置:
1、首先当然要配置ehcache的标准配置文件 ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect"> <diskStore path="D:/cachetmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="userCache" maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /> </ehcache>
以上配置文件参数说明:
cache:每个需要配置的缓存节点
name:定义的缓存名字,在注解或配置文件中会引用到
maxElementsInMemory:内存中最大存储缓存的对象个数
eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效
timeToLiveSeconds: 对象存活时间,指对象从创建到失效所需要的时间
overflowToDisk:如果内存中数据超过内存限制,是否要缓存到磁盘上
diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false
diskExpiryThreadIntervalSeconds:对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次
memoryStoreEvictionPolicy:如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU
2、将Ehcache纳入Spring的beanFacory中,也就是spring的配置文件中配置缓存管理的相关类:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --> <cache:annotation-driven cache-manager="ehCacheManager"/> <!-- cacheManager工厂类,指定ehcache.xml的位置 --> <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:/config/ehcache.xml" /> <!-- 声明ehCacheManager 其中类中的cacheManager属性为net.sf.ehcache.CacheManager因此必须引入ehcache-core.jar包--> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" /> <bean id="userDao" class="com.lyl.dao.impl.UserDaoImpl"></bean> </beans>
3、指定类的的方法上加入操作缓存的注解
@Cacheable:负责将方法的返回值加入到缓存中
@CacheEvict:负责清除缓存
具体参数如下:
@Cacheable 支持如下几个参数:
value:缓存位置名称,不能为空,如果使用EHCache,就是ehcache.xml中声明的cache的name
key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL
condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL
@CacheEvict 支持如下几个参数:
value:缓存位置名称,不能为空,同上
key:缓存的key,默认为空,同上
condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
allEntries:true表示清除value中的全部缓存,默认为false
//value 指定缓冲的名字,在文件配置的 key 为对应缓存的id
@Cacheable(value="userCache",key="#uid + 'findById'")
public User findUserById(String uid) {
System.out.println("User findUserById("+uid+")");
return userMap.get(uid);
}
//将查询的所有用户加入userCache缓存
@Cacheable(value="userCache")
public List<User> findAllUsers() {
List<User> userList=new ArrayList<User>();
System.out.println("Execute:List<User> findAllUsers()");
Iterator<User> it=userMap.values().iterator();
while(it.hasNext()){
userList.add(it.next());
}
return userList;
}
//清除所有的缓存userCache所有的值
@CacheEvict(value="userCache",allEntries=true)
public void updateUser(User user) {
System.out.println("Execute:updateUser(User user)");
userMap.put(user.getUid(), user);
}
//清除所有的缓存userCache所有的值
@CacheEvict(value="userCache",allEntries=true)
public void delUserbyUid(String uid){
System.out.println("Execute: delUserbyId()");
userMap.remove(uid);
}
详细参考源码: