准备工作:
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.xml
3.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>
- <?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>
最后工具类:
- /**
- * Copyright © 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
- */
- package com.thinkgem.jeesite.common.utils;
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.CacheManager;
- import net.sf.ehcache.Element;
- /**
- * Cache工具类
- * @author ThinkGem
- * @version 2013-5-29
- */
- public class CacheUtils {
- private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager"));
- private static final String SYS_CACHE = "sysCache";
- /**
- * 获取SYS_CACHE缓存
- * @param key
- * @return
- */
- public static Object get(String key) {
- return get(SYS_CACHE, key);
- }
- /**
- * 写入SYS_CACHE缓存
- * @param key
- * @return
- */
- public static void put(String key, Object value) {
- put(SYS_CACHE, key, value);
- }
- /**
- * 从SYS_CACHE缓存中移除
- * @param key
- * @return
- */
- public static void remove(String key) {
- remove(SYS_CACHE, key);
- }
- /**
- * 获取缓存
- * @param cacheName
- * @param key
- * @return
- */
- public static Object get(String cacheName, String key) {
- Element element = getCache(cacheName).get(key);
- return element==null?null:element.getObjectValue();
- }
- /**
- * 写入缓存
- * @param cacheName
- * @param key
- * @param value
- */
- public static void put(String cacheName, String key, Object value) {
- Element element = new Element(key, value);
- getCache(cacheName).put(element);
- }
- /**
- * 从缓存中移除
- * @param cacheName
- * @param key
- */
- public static void remove(String cacheName, String key) {
- getCache(cacheName).remove(key);
- }
- /**
- * 获得一个Cache,没有则创建一个。
- * @param cacheName
- * @return
- */
- 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方法一样