springmvc缓存

来源:互联网 发布:通达信炒股软件编程 编辑:程序博客网 时间:2024/05/20 17:23
   数据量大的时候缓存对于提高效率是很显著的。而缓存一般包括前台静态资源缓存和后台查询出来的数据缓存,这里介绍的是后者。
    1.在springmvc的配置文件中加入缓存配置,代码如下:
[html] view plaincopyprint?
  1. <!-- 缓存配置(两种) -->    
  2. <!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->    
  3. <cache:annotation-driven cache-manager="cacheManager"/>    
  4. <!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->    
  5. <!--     
  6. <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">    
  7.     <property name="caches">    
  8.         <set>    
  9.             <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>    
  10.         </set>    
  11.     </property>    
  12. </bean>    
  13.  -->    
  14. <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->    
  15. <!-- Spring提供的基于的Ehcache实现的缓存管理器 -->    
  16. <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
  17.     <property name="configLocation" value="classpath:ehcache.xml"/>    
  18. </bean>    
  19. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">    
  20.     <property name="cacheManager" ref="cacheManagerFactory"/>    
  21. </bean>   

        (注意不要忘记引入对应的命名空间)
        
    2.在配置路径下(这里是默认的src下)建立ehcache.xml文件,并配置程序的相关cache策略,代码如下:
    
[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache dynamicConfig="false" monitoring="off" updateCheck="false"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">  
  4.       
  5.     <!-- 定义缓存策略  
  6.         eternal="false"                 // 元素是否永恒,如果是就永不过期(必须设置)  
  7.         maxEntriesLocalHeap="1000"      // 堆内存中最大缓存对象数,0没有限制(必须设置)  
  8.         overflowToDisk="false"          // 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)  
  9.         diskPersistent="false"          // 磁盘缓存在VM重新启动时是否保持(默认为false)  
  10.         timeToIdleSeconds="0"           // 导致元素过期的访问间隔(秒为单位). 当eternal为false时,这个属性才有效,0表示可以永远空闲,默认为0  
  11.         timeToLiveSeconds="600"         // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期  
  12.         memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU  
  13.    -->  
  14.     <defaultCache eternal="false" maxEntriesLocalHeap="0" timeToIdleSeconds="300" timeToLiveSeconds="300"/>  
  15.     <cache name="myCache" maxEntriesLocalHeap="1000" />   
  16.      
  17. </ehcache>  

    3.既然是ehcache,肯定要引入ehcache的jar:ehcache-2.8.3,至于还需要什么jar,运行后就会发现。
    
    4.运行后报错,nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor   
    是因为缺少aopaliance-1.0 jar包,加入即可。
    
    5.后台service代码(我的注解是加在service的方法上的):
    
[html] view plaincopyprint?
  1. //将查询到的数据缓存到myCache中,并使用方法名称加上参数中的userNo作为缓存的key    
  2. //通常更新操作只需刷新缓存中的某个值,所以为了准确的清除特定的缓存,故定义了这个唯一的key,从而不会影响其它缓存值    
  3. @Cacheable(value="myCache"key="#id")    
  4. public String getUsernameById(int id){    
  5.     System.out.println("调用了测试缓存的方法");   
  6.     System.out.println("数据库中查到此用户号[" + id + "]对应的用户名为[" + userMapper.getUsernameById(id) + "]");    
  7.     return userMapper.getUsernameById(id);    
  8. }  

    注意:springmvc有关缓存的注解主要是@Cacheable、@CachePut、@CacheEvict。关于这三个的详细使用可参考:http://my.oschina.net/duoduo3369/blog/173924
    

    6.第一次访问前台页面:


console后台有相关日志,日志如下:


     第二次执行,日志如下:


     程序没有执行我加了缓存注解的方法,后台没有日志,但是前台返回了数据,说明是从缓存里读取的数据,即缓存配置成功。

     over!

http://download.csdn.net/detail/tonytfjing/8302369


http://my.oschina.net/duoduo3369/blog/173924
http://blog.csdn.net/jadyer/article/details/12257865

0 0
原创粉丝点击