工具类:ehCache,用于缓存

来源:互联网 发布:java long 长度获取 编辑:程序博客网 时间:2024/06/04 06:21

准备工作:

1.导入依赖:

[html] view plain copy
  1. <ehcache.version>2.6.9</ehcache.version>  
  2.         <ehcache-web.version>2.0.4</ehcache-web.version>  
  3.   
  4. <dependency>  
  5.             <groupId>net.sf.ehcache</groupId>  
  6.             <artifactId>ehcache-core</artifactId>  
  7.             <version>${ehcache.version}</version>  
  8.         </dependency>  
  9.         <dependency>  
  10.             <groupId>net.sf.ehcache</groupId>    
  11.             <artifactId>ehcache-web</artifactId>    
  12.             <version>${ehcache-web.version}</version>  
  13.         </dependency>  

2.properties文件添加配置:ehcache.configFile=cache/ehcache-local.xml

3.spring-context.xml加入:

[html] view plain copy
  1. <!-- 加载配置属性文件 -->  
  2.     <context:property-placeholder ignore-unresolvable="true" location="classpath:xxx.properties" />  
  3. <!-- 缓存配置 -->  
  4.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
  5.         <property name="configLocation" value="classpath:${ehcache.configFile}" />  
  6.     </bean>  
4.在cache目录下配置缓存ehcache-local.xml,例如

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache updateCheck="false" name="defaultCache">  
  3. <!--     参数说明:  
  4.         (0)diskStore: 临时缓存存放路径  
  5.         (1)name:Cache的唯一标识。  
  6.         (2)maxElementsInMemory:内存中最大缓存对象数。  
  7.         (3)eternal:Element是否永久有效,一旦设置true,timeout将不起作用。  
  8.         (4)timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。  
  9.         (5)timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。  
  10.         (6)overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。  
  11.         (7)maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。  
  12.         (8) memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略 去清理缓存中的内容。默认策略是LRU(最近最少使用),你也可以设置为FIFO(先进先出)或是LFU(较少使用)   
  13.  -->  
  14.   
  15.     <diskStore path="../temp/jeesite/ehcache" />  
  16.   
  17.     <!-- 默认缓存配置. -->  
  18.     <defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"  
  19.         overflowToDisk="true" maxEntriesLocalDisk="100000" />  
  20.       
  21.     <!-- 系统缓存 -->  
  22.     <cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>  
  23.       
  24.     <!-- 用户缓存 -->  
  25.     <cache name="userCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>  
  26.       
  27.     <!-- 工作流模块缓存 -->  
  28.     <cache name="actCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>  
  29.       
  30.     <!-- 内容管理模块缓存  
  31.     <cache name="cmsCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/> -->  
  32.       
  33.     <!-- 系统活动会话缓存 -->  
  34.     <cache name="activeSessionsCache" maxEntriesLocalHeap="10000" overflowToDisk="true"  
  35.            eternal="true" timeToLiveSeconds="0" timeToIdleSeconds="0"  
  36.            diskPersistent="true" diskExpiryThreadIntervalSeconds="600"/>  
  37.       
  38.     <!-- 简单页面缓存  
  39.     <cache name="SimplePageCachingFilter" maxEntriesLocalHeap="100" eternal="false" overflowToDisk="true"  
  40.         timeToIdleSeconds="120" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LFU"/> -->  
  41.           
  42. </ehcache>  

最后工具类:

[java] view plain copy
  1. /** 
  2.  * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. 
  3.  */  
  4. package com.thinkgem.jeesite.common.utils;  
  5.   
  6. import net.sf.ehcache.Cache;  
  7. import net.sf.ehcache.CacheManager;  
  8. import net.sf.ehcache.Element;  
  9.   
  10. /** 
  11.  * Cache工具类 
  12.  * @author ThinkGem 
  13.  * @version 2013-5-29 
  14.  */  
  15. public class CacheUtils {  
  16.       
  17.     private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager"));  
  18.   
  19.     private static final String SYS_CACHE = "sysCache";  
  20.   
  21.     /** 
  22.      * 获取SYS_CACHE缓存 
  23.      * @param key 
  24.      * @return 
  25.      */  
  26.     public static Object get(String key) {  
  27.         return get(SYS_CACHE, key);  
  28.     }  
  29.       
  30.     /** 
  31.      * 写入SYS_CACHE缓存 
  32.      * @param key 
  33.      * @return 
  34.      */  
  35.     public static void put(String key, Object value) {  
  36.         put(SYS_CACHE, key, value);  
  37.     }  
  38.       
  39.     /** 
  40.      * 从SYS_CACHE缓存中移除 
  41.      * @param key 
  42.      * @return 
  43.      */  
  44.     public static void remove(String key) {  
  45.         remove(SYS_CACHE, key);  
  46.     }  
  47.       
  48.     /** 
  49.      * 获取缓存 
  50.      * @param cacheName 
  51.      * @param key 
  52.      * @return 
  53.      */  
  54.     public static Object get(String cacheName, String key) {  
  55.         Element element = getCache(cacheName).get(key);  
  56.         return element==null?null:element.getObjectValue();  
  57.     }  
  58.   
  59.     /** 
  60.      * 写入缓存 
  61.      * @param cacheName 
  62.      * @param key 
  63.      * @param value 
  64.      */  
  65.     public static void put(String cacheName, String key, Object value) {  
  66.         Element element = new Element(key, value);  
  67.         getCache(cacheName).put(element);  
  68.     }  
  69.   
  70.     /** 
  71.      * 从缓存中移除 
  72.      * @param cacheName 
  73.      * @param key 
  74.      */  
  75.     public static void remove(String cacheName, String key) {  
  76.         getCache(cacheName).remove(key);  
  77.     }  
  78.       
  79.     /** 
  80.      * 获得一个Cache,没有则创建一个。 
  81.      * @param cacheName 
  82.      * @return 
  83.      */  
  84.     private static Cache getCache(String cacheName){  
  85.         Cache cache = cacheManager.getCache(cacheName);  
  86.         if (cache == null){  
  87.             cacheManager.addCache(cacheName);  
  88.             cache = cacheManager.getCache(cacheName);  
  89.             cache.getCacheConfiguration().setEternal(true);  
  90.         }  
  91.         return cache;  
  92.     }  
  93.   
  94.     public static CacheManager getCacheManager() {  
  95.         return cacheManager;  
  96.     }  
  97.       
  98. }  

上面工具类对应的是系统缓存,想变成其他的缓存,改一下常量即可。

用法:get方法set方法一样

原创粉丝点击