工具类:ehCache,用于缓存
来源:互联网 发布:java long 长度获取 编辑:程序博客网 时间:2024/06/04 06:21
准备工作:
1.导入依赖:
- <ehcache.version>2.6.9</ehcache.version>
- <ehcache-web.version>2.0.4</ehcache-web.version>
-
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache-core</artifactId>
- <version>${ehcache.version}</version>
- </dependency>
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache-web</artifactId>
- <version>${ehcache-web.version}</version>
- </dependency>
2.properties文件添加配置:ehcache.configFile=cache/ehcache-local.xml3.spring-context.xml加入:
-
- <context:property-placeholder ignore-unresolvable="true" location="classpath:xxx.properties" />
-
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
- <property name="configLocation" value="classpath:${ehcache.configFile}" />
- </bean>
4.在cache目录下配置缓存ehcache-local.xml,例如- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache updateCheck="false" name="defaultCache">
- <!-- 参数说明:
- (0)diskStore: 临时缓存存放路径
- (1)name:Cache的唯一标识。
- (2)maxElementsInMemory:内存中最大缓存对象数。
- (3)eternal:Element是否永久有效,一旦设置true,timeout将不起作用。
- (4)timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
- (5)timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。
- (6)overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。
- (7)maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。
- (8) memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略 去清理缓存中的内容。默认策略是LRU(最近最少使用),你也可以设置为FIFO(先进先出)或是LFU(较少使用)
- -->
-
- <diskStore path="../temp/jeesite/ehcache" />
-
-
- <defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
- overflowToDisk="true" maxEntriesLocalDisk="100000" />
-
-
- <cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
-
-
- <cache name="userCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
-
-
- <cache name="actCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
-
- <!-- 内容管理模块缓存
- <cache name="cmsCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/> -->
-
-
- <cache name="activeSessionsCache" maxEntriesLocalHeap="10000" overflowToDisk="true"
- eternal="true" timeToLiveSeconds="0" timeToIdleSeconds="0"
- diskPersistent="true" diskExpiryThreadIntervalSeconds="600"/>
-
- <!-- 简单页面缓存
- <cache name="SimplePageCachingFilter" maxEntriesLocalHeap="100" eternal="false" overflowToDisk="true"
- timeToIdleSeconds="120" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LFU"/> -->
-
- </ehcache>
最后工具类:-
-
-
- package com.thinkgem.jeesite.common.utils;
-
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.CacheManager;
- import net.sf.ehcache.Element;
-
-
-
-
-
-
- public class CacheUtils {
-
- private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager"));
-
- private static final String SYS_CACHE = "sysCache";
-
-
-
-
-
-
- public static Object get(String key) {
- return get(SYS_CACHE, key);
- }
-
-
-
-
-
-
- public static void put(String key, Object value) {
- put(SYS_CACHE, key, value);
- }
-
-
-
-
-
-
- public static void remove(String key) {
- remove(SYS_CACHE, key);
- }
-
-
-
-
-
-
-
- public static Object get(String cacheName, String key) {
- Element element = getCache(cacheName).get(key);
- return element==null?null:element.getObjectValue();
- }
-
-
-
-
-
-
-
- public static void put(String cacheName, String key, Object value) {
- Element element = new Element(key, value);
- getCache(cacheName).put(element);
- }
-
-
-
-
-
-
- public static void remove(String cacheName, String key) {
- getCache(cacheName).remove(key);
- }
-
-
-
-
-
-
- private static Cache getCache(String cacheName){
- Cache cache = cacheManager.getCache(cacheName);
- if (cache == null){
- cacheManager.addCache(cacheName);
- cache = cacheManager.getCache(cacheName);
- cache.getCacheConfiguration().setEternal(true);
- }
- return cache;
- }
-
- public static CacheManager getCacheManager() {
- return cacheManager;
- }
-
- }
上面工具类对应的是系统缓存,想变成其他的缓存,改一下常量即可。用法:get方法set方法一样