spring mvc使用spring ehcache缓存

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值