博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC + ehcache( ehcache-spring-annotations)基于注解的服务器端数据缓存
阅读量:4036 次
发布时间:2019-05-24

本文共 3203 字,大约阅读时间需要 10 分钟。

SpringMVC + ehcache(google  ehcache-spring-annotations),基于注解解决服务器端数据缓存。

1.      下载所需jar包

1.         Ehcache的jar:

           ehcache-2.7.1.jar

          slf4j-api-1.6.6.jar

         slf4j-jdk14-1.6.6.jar

down下来之后lib里面会有以上三个包(这两个slf4j的包一般项目里会有,换成最新的版本即可),下载地址如下:

 

2.        ehcache-spring-annotations 的jar:

ehcache-spring-annotations-1.2.0.jar

ehcache-spring-annotations-1.2.0-sources.jar

2.      配置

   1.   ehcacheApplication.xml,该文件里面配置基本不变,需要将该文件加入web.xml

 

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"    
  6.     xsi:schemaLocation="    
  7.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring  
  9.     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">  
  10.                                          
  11.     <ehcache:annotation-driven />    
  12.         
  13.     <ehcache:config cache-manager="cacheManager">    
  14.         <ehcache:evict-expired-elements interval="60" />    
  15.     </ehcache:config>    
  16.         
  17.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
  18.         <property name="configLocation"  value="classpath:spring/ehcache.xml"/>    
  19.     </bean>  
  20. </beans>  

 

    2. web.xml

Xml代码  
  1. <context-param>  
  2.         <param-name>contextConfigLocation</param-name>  
  3.         <param-value>classpath:spring/applicationContext.xml,classpath:spring/ehcacheApplication.xml</param-value>  
  4.     </context-param>  

 

    3.   ehcache.xml

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.  
  3. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">    
  4.         
  5.     <diskStore path="java.io.tmpdir/ehcache_hsrj"  
  6.         
  7.     <defaultCache    
  8.             maxElementsInMemory="3000"    
  9.             eternal="false"    
  10.             timeToIdleSeconds="3600"    
  11.             timeToLiveSeconds="3600"    
  12.             overflowToDisk="true"    
  13.             diskPersistent="false"    
  14.             diskExpiryThreadIntervalSeconds="100"    
  15.             memoryStoreEvictionPolicy="LRU"    
  16.             />    
  17.     <cache name="methodCache"    
  18.            maxElementsInMemory="3000"    
  19.            eternal="false"    
  20.            overflowToDisk="true"    
  21.            timeToIdleSeconds="36000"    
  22.            timeToLiveSeconds="36000"    
  23.            memoryStoreEvictionPolicy="LFU"    
  24.             />    
  25. </ehcache>  

 

3.      代码

1.  缓存

 

给你需要缓存的方法加上如下这句

 

Java代码  
  1. @Cacheable(cacheName = "methodCache")  
  2. public int find() {  
  3.  List<Car> list=carDao.findCar();  
  4.     Return list.size();  
  5. }  

  

cacheName里面对应ehcache.xml中配置的<cache name="methodCache"  ,

 

这里我想说,网上很多人都是加到dao上的,我是直接加到service(业务层)里面的方法上,因为我的业务层方法会对数据做处理,而我需要缓存整个处理结果,所以加到service里面的方法上是可以的。

 

   2. 清除缓存

 

Java代码  
  1. @TriggersRemove(cacheName="methodCache",removeAll=true)   
  2.     public void flush(){  
  3.        log.info("清除缓存!");  
  4.     }  

  

cacheName里面对应缓存里面的名称,removeAll=true 这句是必须加的,否则无法清除缓存。

4.      测试

    1.     我的测试方法是,在控制层提供了两个方法,缓存和清除缓存,分别调用service中的两个方法find与flush,缓存方法将service中返回的size返给页面,在浏览器里面去请求这两个方法。

 

    2.    首次请求缓存方法,发现后台打印sql查询信息,再在数据库中添加数据,再次请求缓存方法,发现后台不打印sql查询信息,页面显示list的size不变。证明缓存成功。

 

    3.    请求清除缓存方法。

 

    4.   再次请求缓存方法,会发现后台打印sql查询信息,页面显示list的size发生变化。证明清除缓存成功。

 

5.      其他

    1.对于清除缓存的方法,ehcache提供了两种,一种是在ehcache.xml中配置的时间过后自动清除,一种是在数据发生变化后触发清除。个人感觉第二种比较好。可以将

 

@TriggersRemove(cacheName="methodCache",removeAll=true)

 

这句代码加到service里面的添加、删除、修改方法上。这样只要这几个方法有调用,缓存自动清除。

  

    2. 如果是通过触发的方式清除缓存,那时间可以配置为永不过期。在ehcache.xml中将eternal="true"  设置为true,超时设置将被忽略,在未触发清除缓存方法之前,对象从不过期。

参考的资料url:http://doc.okbase.net/yunzhu/archive/101034.html

转载地址:http://zdjdi.baihongyu.com/

你可能感兴趣的文章
uboot start.s文件分析
查看>>
没有路由器的情况下,开发板,虚拟机Ubuntu,win10主机,三者也可以ping通
查看>>
本地服务方式搭建etcd集群
查看>>
安装k8s Master高可用集群
查看>>
忽略图片透明区域的事件(Flex)
查看>>
忽略图片透明区域的事件(Flex)
查看>>
AS3 Flex基础知识100条
查看>>
Flex动态获取flash资源库文件
查看>>
flex中设置Label标签文字的自动换行
查看>>
Flex 中的元数据标签
查看>>
flex4 中创建自定义弹出窗口
查看>>
01Java基础语法-11. 数据类型之间的转换
查看>>
01Java基础语法-13. if分支语句的灵活使用
查看>>
01Java基础语法-15.for循环结构
查看>>
01Java基础语法-16. while循环结构
查看>>
01Java基础语法-17. do..while循环结构
查看>>
01Java基础语法-18. 各种循环语句的区别和应用场景
查看>>
01Java基础语法-19. 循环跳转控制语句
查看>>
Django框架全面讲解 -- Form
查看>>
socket,accept函数解析
查看>>