本文主要是介绍spring+ehcache 缓存,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.在web.xml中添加
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/application.xml</param-value>
</context-param>
2.在application.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:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--数据源的配置略......-->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:resources/ehcache.xml"/>
</bean>
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
</beans>
3.添加ehcache.xml到2步骤中设置的路径resources/ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<cache name="serviceCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU"/>
</ehcache>
name:Cache的唯一标识
maxElementsInMemory:内存中最大缓存对象数。
eternal:Element是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。
overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
4.在service层添加缓存注解
public class UserInfoServiceImpl implements UserInfoServiceI {
@Cacheable(value = "serviceCache", key = "#id")
public UserInfo getById(String id) {
return userInfoDao.get(UserInfo.class, id);
}
@Cacheable(value = "serviceCache", key = "#o.userName")
public List<UserInfo> getList(UserInfo o) {
String hql = "from UserInfo where userName like '%" + o.getUserName() + "'%";
return userInfoDao.find(hql);
}
@CacheEvict(value="serviceCache",allEntries=true)
public void removeAll(){
System.out.println("清除所有缓存");
}
}
@Cacheable的作用主要针对方法配置,能够根据方法的请求参数对其结果进行缓存。
value:缓存的名称,在 ehcache.xml文件中定义,必须指定至少一个。如:@Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”}
key:缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合。如:@Cacheable(value=”testcache”,key=”#userName”)
condition:缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存。如:@Cacheable(value=”testcache”,condition=”#userName.length()>2”)
@CacheEvictde 的作用主要针对方法配置,能够根据一定的条件对缓存进行清空。
value、key、condition同上。
allEntries:是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存。如:@CachEvict(value=”testcache”,allEntries=true)
beforeInvocation:是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存。如:@CachEvict(value=”testcache”,beforeInvocation=true)
5.测试
1.对@Cacheable注解的方法打断点,第一次请求时,会进去方法执行,以同样的条件第二次请求,则不会进入方法,直接从缓存中拿去数据。
2.通过日志观察打印的sql语句,第一次请求会打印出sql语句,以同样的条件第二次请求不会打印sql语句,直接从缓存中拿去数据。
可通过上面任一方法进行验证,若成功则表示缓存配置成功。
这篇关于spring+ehcache 缓存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!