ehcache配置文件:
diskStore是缓存持久化,存放于本地磁盘的位置路径:
cache是由CacheManager统一管理的,有默认的一个cache,以及有我们自己配置的(可以有多个)
ehcache主要使用:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Component;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
/**
* ehcache缓存工具类
* @author Administrator
*
*/
@Component
@Configurable
@EnableCaching//标注启动缓存
public class EhCacheManager {
static Log log = LogFactory.getLog(EhCacheManager.class);
//缓存管理器
public static CacheManager cacheManager = null;
//数据缓存
public static Cache dataCache = null;
//模板缓存
public static Cache templateCache = null;
static {
//设置系统属性 缓存异常改变则清除缓存(貌似然并卵)
System.setProperty("net.sf.ehcache.enableShutdownHook", "true");
//创建CacheManager,使用指定配置文件创建
cacheManager = CacheManager.create();
//重新加载缓存的时候,清空原有的磁盘缓存
cacheManager.clearAll();
//得到dataCache 如果没有在配置文件(ehcache.xml)获得cache缓存 则创建一个现成的缓存
dataCache = cacheManager.getCache("dataCache");
//得到templateCache 如果没有在配置文件(ehcache.xml)获得cache缓存 则创建一个现成的缓存
templateCache=cacheManager.getCache("templateCache");
}
/**
* 将对象添加到某一个cache中的缓存中
* @param cacheKey 某个缓存的key值
* @param cacheValue 某个缓存的value值
* @param cache 具体的缓存对象
*/
public static void addCache(String cacheKey, Object cacheValue, Cache cache) {
if(!"".equals(cacheKey)){
Element element = new Element(cacheKey, cacheValue);
cache.put(element);
cache.flush();
}else {
/*NOTHING TO DO*/
}
}
/**
* 将对象从某一个cache中的缓存中删除
* @param cacheKey 缓存中某个对象的key值
* @param cache 具体的缓存对象
*/
public static void removeCacheByKey(String cacheKey, Cache cache) {
cache.remove(cacheKey);
cache.flush();
}
/**
* 获得某一个cache缓存中的所有数据
* @param cacheKey 某个缓存的key值
* @param cache 具体的缓存对象
* @return 缓存中某个对应key值得对应的value值对象
*/
@SuppressWarnings("unchecked")
public static List<Object> getAllCacheValue(Cache cache) {
List<Object> keys = cache.getKeys();
List<Object> list = new ArrayList<Object>();
for(Object key : keys){
Object object = getCacheValueByKey(key.toString(), cache);
list.add(object);
}
return list;
}
/**
* 通过key从某一个cache中的缓存中获取数据
* @param cacheKey 某个缓存的key值
* @param cache 具体的缓存对象
* @return 缓存中某个对应key值得对应的value值对象
*/
public static Object getCacheValueByKey(String cacheKey, Cache cache) {
Element element = cache.get(cacheKey);
flush(cache);
if (element == null) {
return null;
}
Object object = element.getObjectValue();
flush(cache);
return object;
}
/**
* 获取某一个cache中的所有的缓存对象的key值
* @param cache 具体的缓存对象
*/
@SuppressWarnings("unchecked")
public static List<Object> getAllCacheKeys(Cache cache) {
List<Object> keys = cache.getKeys();
flush(cache);
return keys;
}
/**
* 获取缓存管理器中的缓存配置名称
*/
public static String[] getAllCacheNames() {
String[] cacheNames = cacheManager.getCacheNames();
return cacheNames;
}
/**
* 刷新
* @param cache
*/
public static void flush(Cache cache){
cache.flush();
}
/**
* 判断该key值是否存在在知道缓存对象中
* @param cache 具体的缓存对象
* @param key
* @return flag
*/
public static boolean isCacheKeyExist(Cache cache, String key){
boolean flag = false;
List<Object> keys = getAllCacheKeys(cache);
for (Object object : keys) {
if(key.equalsIgnoreCase(object.toString())){
flag = true;
}
}
return flag;
}
}
配置文件ehcache.xml原文:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
http://www.springmodules.org/schema/cache/springmodules-cache.xsd
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false" >
<!-- 达到内存上限后缓存文件保存位置 -->
<diskStore path="D:/ehcache/temporary" />
<!-- <diskStore path="java.io.tmpdir"/> -->
<!-- maxElementsInMemory、最大内存占用,超出后缓存保存至文件(缓存最大个数) -->
<!-- memoryStoreEvictionPolicy、当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 -->
<!-- eternal、对象是否永久有效,一但设置了,timeout将不起作用。 -->
<!-- timeToIdleSeconds、设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 -->
<!-- timeToLiveSeconds、设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 -->
<!-- overflowToDisk、当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 -->
<!-- diskPersistent、是否缓存虚拟机重启期数据 -->
<!-- maxElementsOnDisk、硬盘最大缓存个数。 -->
<!-- diskExpiryThreadIntervalSeconds、磁盘失效线程运行时间间隔,默认是120秒。 -->
<defaultCache
eternal="true"
maxElementsInMemory="100"
maxElementsOnDisk="1000000"
overflowToDisk="true"
diskPersistent="true"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="FIFO" >
</defaultCache>
<cache
name="dataCache"
eternal="true"
maxElementsInMemory="1"
maxElementsOnDisk="1000000"
overflowToDisk="true"
diskPersistent="true"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="FIFO" >
</cache>
<cache
name="templateCache"
eternal="true"
maxElementsInMemory="1"
maxElementsOnDisk="1000000"
overflowToDisk="true"
diskPersistent="true"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="FIFO" >
</cache>
</ehcache>