`
这些年
  • 浏览: 388345 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring + Ehcache 注解形式配置(转)

 
阅读更多

Spring3.1 +Ehcache 注解形式配置

1.Spring配置文件

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"  
  5.   
  6.     xsi:schemaLocation="  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  9.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  10.             http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"  
  11.     default-lazy-init="false" default-autowire="byName">  
  12.       
  13.     <context:annotation-config />  
  14.     <context:component-scan base-package="com.miao" />  
  15.       
  16.     <!-- 启用缓存注解功能 -->  
  17.     <cache:annotation-driven cache-manager="cacheManager"/>  
  18.   
  19.     <!-- cacheManager工厂类 -->   
  20.     <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"   
  21.         p:configLocation="classpath:ehcache.xml" />   
  22.   
  23.     <!-- 声明cacheManager -->  
  24.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"   
  25.         p:cacheManager-ref="cacheManagerFactory" />   
  26.           
  27.   
  28.   
  29. </beans>  

 

 

2.ehcache文件

Xml代码  收藏代码
  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">  
  2.     <!--   
  3.         diskStore :指定数据存储位置,可指定磁盘中的文件夹位置  
  4.         defaultCache : 默认的管理策略  
  5.           
  6.         以下属性是必须的:  
  7.             name: Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。maxElementsInMemory:在内存中缓存的element的最大数目。   
  8.             maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制。   
  9.             eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。   
  10.             overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上。   
  11.               
  12.         以下属性是可选的:  
  13.             timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。  
  14.             timeToLiveSeconds: 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。  
  15.             diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。   
  16.             diskExpiryThreadIntervalSeconds: 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。   
  17.             diskSpoolBufferSizeMB: DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。   
  18.             memoryStoreEvictionPolicy: 如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。   
  19.           
  20.             缓存的3 种清空策略 :  
  21.             FIFO ,first in first out (先进先出).  
  22.             LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。  
  23.             LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。  
  24.      -->  
  25.   
  26.     <diskStore path="java.io.tmpdir"/>  
  27.   
  28.     <defaultCache  
  29.             maxElementsInMemory="10000"  
  30.             eternal="false"  
  31.             timeToIdleSeconds="520"  
  32.             timeToLiveSeconds="520"  
  33.             overflowToDisk="true"  
  34.             maxElementsOnDisk="10000000"  
  35.             diskPersistent="false"  
  36.             diskExpiryThreadIntervalSeconds="120"  
  37.             memoryStoreEvictionPolicy="LRU"  
  38.             />  
  39.               
  40.     <cache   name="testCache"   
  41.             maxElementsInMemory="10000"  
  42.             maxElementsOnDisk="1000"   
  43.             eternal="false"   
  44.             overflowToDisk="true"  
  45.             diskSpoolBufferSizeMB="20"   
  46.             timeToIdleSeconds="300"   
  47.             timeToLiveSeconds="600"  
  48.             memoryStoreEvictionPolicy="LFU" />  
  49. </ehcache>  

  

 

3.测试类的代码

Java代码  收藏代码
  1. package com.miao.service.user;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.annotation.Resource;  
  6.   
  7. import org.springframework.cache.annotation.CacheEvict;  
  8. import org.springframework.cache.annotation.Cacheable;  
  9. import org.springframework.stereotype.Service;  
  10.   
  11. import com.miao.dao.user.UserDao;  
  12.   
  13. @Service("userService")  
  14. public class UserServiceImp implements UserService{  
  15.       
  16.     @Resource  
  17.     private UserDao userDao;  
  18.       
  19.     /** 
  20.      * Cacheable 参数意义 
  21.      * value:缓存位置名称,不能为空,对应ehcache.xml中声明的cache的name 
  22.      * key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL 
  23.      * condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL 
  24.      *  
  25.      */  
  26.   
  27.     @Override  
  28.     @Cacheable(value="testCache")           //指定cache  
  29.     public List getAllUsers() {  
  30.         return userDao.getAllUsers();  
  31.     }  
  32.   
  33.     @Override  
  34.     //@Cacheable(value="testCache",key="#id +'getUserById'")          
  35.     @Cacheable(value="testCache",key="'getUserById('+#id+')'")      //指定cache, 设置key  
  36.     public List getUserById(String id) {  
  37.         return userDao.getUserById(id);  
  38.     }  
  39.   
  40.     @Override  
  41.     //@CacheEvict(value="testCache",key="'getUserById('+#id+')'")           //清除指定缓存 (业务逻辑修改了某个User,所以要将这个User的缓存清除;其实也要将获取所有User的缓存清除)  
  42.     //@CacheEvict(value="testCache",allEntries=true)                    //清除所有的缓存  
  43.     @CacheEvict(value="testCache",allEntries=true,condition="#id=='lisi'")  //当id==“lisi”时,清除所有的缓存  
  44.     public boolean modifyUserById(String id) {  
  45.         return true;  
  46.     }  
  47.       
  48.   
  49. }  

   

 

4.调用方法进行测试即可

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics