ehcache配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
*
* 缓存配置
* @author zyz
* @date 2013年7月2日
*
*/ -->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="3000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="100"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="mallListCache"
maxElementsInMemory="3000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="36000"
timeToLiveSeconds="36000"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>
spring配置文件
application.xml
<!-- 配置Ehcache缓存 -->
<!-- 启动缓存注解功能 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
<!-- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"></bean>
</set>
</property>
</bean> -->
<!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
<bean id="ehCacheManagerFactoryBean" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache-hibernate-local.xml"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManagerFactoryBean"></property>
</bean>
service代码:
@Override
@Cacheable(value = "mallListCache")
public List<Role> getRoleListByName(String roleName) {
return roleDao.getRoleByName(roleName);
}
value值为ehcache.xml配置的name;
同时执行两次请求,第一次打印sql,第二次不打印;---成功;
数据库更新修改操作时,需要清除缓存数据
方法加注解即可:
@CacheEvict(value="mallListCache",allEntries=true)
更多方法具体参考:
http://tom-seed.iteye.com/blog/2104430
spring mvc使用spring ehcache缓存
猜你喜欢
转载自lihuiyongapple.iteye.com/blog/2240336
今日推荐
周排行