Spring MVC3 + Ehcache 缓存实现
来源:互联网 发布:excel vba编程实例 编辑:程序博客网 时间:2024/05/17 21:59
版本:Spring3.0.6
准备工作:
下载 ehcache-spring-annotations-1.2.0 http://code.google.com/p/ehcache-spring-annotations/downloads/list
下载完加压后里面的lib下的jar统统添加到classpath中
在资源文件夹下(通常是src/main/resources) 添加 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" /><defaultCache eternal="false" maxElementsInMemory="1000"overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /><cache name="baseCache" eternal="false" maxElementsInMemory="500"overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"timeToLiveSeconds="300" memoryStoreEvictionPolicy="LFU" /></ehcache>
这里是定义缓存策略
eternal="false" // 元素是否永恒,如果是就永不过期(必须设置)
maxElementsInMemory="1000" // 缓存容量的内存最大值(必须设置)
overflowToDisk="false" // 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)
diskPersistent="false" // 磁盘缓存在VM重新启动时是否保持(默认为false)
timeToIdleSeconds="0" // 导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0
timeToLiveSeconds="600" // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期
memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU
然后在添加 cache-config.xml 内容如下:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"><ehcache:annotation-driven /><ehcache:config cache-manager="cacheManager"><ehcache:evict-expired-elementsinterval="60" /></ehcache:config><bean id="cacheManager"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache.xml" /></bean></beans>
然后在DAO层做如下配置:
@TriggersRemove(cacheName="baseCache",removeAll=true)public Entity save(Entity entity) throws CrudException {return entity;}@TriggersRemove(cacheName="baseCache",removeAll=true)public Entity update(Entity entity) throws CrudException {return entity;}@TriggersRemove(cacheName="baseCache",removeAll=true)public void del(Entity entity) throws CrudException {}@Cacheable(cacheName="baseCache")@SuppressWarnings("unchecked")public List<Entity> findAll() throws SearchException {return list;}
@Cacheable(cacheName="baseCache")
这个注解就是做到缓存数据,cacheName对应ehcache.xml 中配置
@TriggersRemove(cacheName="baseCache",removeAll=true)
这个注解的作用就是当数据发生变化的时候清除缓存,做到数据同步
如何知道有没生效:
新建一个单元测试,在类中添加两个搜索的方法,然后执行这个单元测试,然后查看两个方法执行的时间(Eclipse的单元测试可以查看)
如果第二个搜索方法的运行时间为0那就说吗成功了,如图
参考:
http://www.blogjava.net/zzzlyr/articles/343234.html
http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/ (英文的,推荐)
- Spring MVC3 + Ehcache 缓存实现
- Spring MVC3 + Ehcache 缓存实现
- Spring MVC3整合缓存Ehcache实现
- spring ehcache实现应用缓存
- spring整合ehcache实现缓存
- spring+ehcache实现的缓存查询参数。
- spring+ehcache实现的缓存查询参数
- Spring + ehcache 缓存配置,注解实现
- spring-boot集成ehcache实现缓存机制
- spring+ehCache+redis多级缓存自定义实现
- Spring Boot整合Ehcache实现缓存功能
- Ehcache缓存结合Spring AOP实现接口数据缓存
- spring配置EHcache缓存
- spring ehCache缓存
- spring使用ehcache缓存
- Spring整合EhCache缓存
- spring+ehcache 缓存
- Spring整合Ehcache缓存
- Android实现ListView异步加载图片
- __init的用法
- No 128 · 经典SQL语句大全
- 【dp+dfs】VK Cup 2012 Round 1-D. Distance in Tree
- Java 基础语法 条件语句,关系,逻辑表达式
- Spring MVC3 + Ehcache 缓存实现
- 动态填充/修改类属性的 DisplayNameAttribute 属性
- strftime
- 设计模式——访问者模式(Visitor)
- jsp+ajax局部刷新较验验证码,onblur事件触发较验
- 判断一个点在多边形的内外关系
- 使用Oracle ADF开发Java Swing应用程序
- ecshop number_format() 在php5.3以上的错误解决方案
- CooCox MDK插件新增LPC177x和LPC178x支持 + LPC1100 semihosting例程