zoukankan      html  css  js  c++  java
  • springmvc缓存

      前几篇文章已经搭建了一个基本的springmvc demo,现在我们来完善下。
        相信大家写程序的时候都接触过缓存的概念,也都知道,数据量大的时候缓存对于提高效率是很显著的。而缓存一般包括前台静态资源缓存和后台查询出来的数据缓存,这里介绍的是后者。最后感谢以下两篇文章,本文是在他们的基础上完成这个demo的。
    http://my.oschina.net/duoduo3369/blog/173924
    http://blog.csdn.net/jadyer/article/details/12257865
    开始奉上代码。

        1.在springmvc的配置文件中加入缓存配置,代码如下:

    [html] view plain copy
     
     print?
    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 plain copy
     
     print?
    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 plain copy
     
     print?
    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!

    注:由于有网友找我要代码,我就把这篇博文涉及的代码还有前几篇博文的代码都上传到了CSDN上,地址是 http://download.csdn.net/detail/tonytfjing/8302369  由于整理的比较仓促,有问题的地方欢迎大家下载探讨,谢谢!

  • 相关阅读:
    Lambda表达式的演变
    反射小例
    进程外Session
    页面缓存的几种方式
    数据缓存的几种方式
    Session
    Cookie
    AJAX学习
    验证码的实现
    ASP.NET动态显示数据的两种方式
  • 原文地址:https://www.cnblogs.com/yhtboke/p/6429577.html
Copyright © 2011-2022 走看看