所需的jar包 maven 依赖:
<!-- ehcache 相关依赖 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.2</version>
</dependency>
ehcache的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
<diskStore path="java.io.tmpdir"/>
<!-- 设定缓存的默认数据过期策略 -->
<!--timeToIdleSeconds 当缓存闲置n秒后销毁 -->
<!--timeToLiveSeconds 当缓存存活n秒后销毁 -->
<!--
缓存配置
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="900"
timeToLiveSeconds="20"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
maxBytesLocalHeap="200M"
maxBytesLocalOffHeap="500M"
maxBytesLocalDisk="1G"
/>
<cache name="cacheWeather"
maxElementsInMemory="20000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="900"
timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
EhCacheManager类:
package com.ehcache;
import net.sf.ehcache.*;
import org.apache.log4j.Logger;
import java.net.URL;
import java.util.List;
public class EhCacheManager {
private static Logger logger = Logger.getLogger(EhCacheManager.class);
/**
* Ehanche manager
*/
private static CacheManager cacheManager = null;
//configuration file
private static String configPath="ehcache-setting.xml";
//Cache definition : Define the cache configured in a file
public final static String CACHE_MYCOMMONCACHE="cacheWeather";
//Instantiation cacheManager, singleton mode
public static CacheManager getCacheManagerInstance(){
if(cacheManager==null){
synchronized(EhCacheManager.class){
URL configUrl=null;
configUrl = EhCacheManager.class.getClassLoader().getResource(configPath);
cacheManager = CacheManager.create(configUrl);
}
}
return cacheManager;
}
public static CacheManager getCacheManager() {
//Single case cache management
return getCacheManagerInstance();
}
public static void setCacheManager(CacheManager cacheManager) {
EhCacheManager.cacheManager = cacheManager;
}
//add new cache
public static void addCacheByName(String cacheName){
if(cacheName==null||cacheName.trim().equals("")){
logger.error("cacheName is null");
}else{
if(getCacheManager().getCache(cacheName.trim())!=null){
getCacheManager().removeCache(cacheName.trim());
}
getCacheManager().addCache(cacheName.trim());
logger.info(cacheName+ "is add complete");
}
}
//get cache by name
public static Cache getCacheByName(String cacheName){
Cache cache=null;
if(cacheName==null||cacheName.trim().equals("")){
logger.error("cacheName is null");
}else{
if(getCacheManager().getCache(cacheName.trim())!=null){
cache=getCacheManager().getCache(cacheName.trim());
}else{
logger.error("cacheName is not existent");
addCacheByName(cacheName);
cache=getCacheManager().getCache(cacheName.trim());
}
}
return cache;
}
//add element to cacheName
public static void putElementToCache(String cacheName,String elementKey,Object elementValue){
Cache cache=null;
if(cacheName==null||cacheName.trim().equals("")){
logger.error("Failed to add cache element,cacheName is null");
}else if("".equals(elementKey)||elementKey==null||"".equals(elementValue)||elementValue==null){
logger.error("Failed to add cache element,elementKey or elementValue is null");
}else{
if(getCacheByName(cacheName.trim())!=null){//
cache=getCacheByName(cacheName.trim());
}else{
addCacheByName(cacheName.trim());
cache=getCacheByName(cacheName.trim());
}
//Adding Element to the cache object
Element element=null;
if(cache.get(elementKey.trim())!=null){
cache.remove(elementKey.trim());
}
element=new Element(elementKey.trim(),elementValue);
cache.put(element);
logger.info("add element:"+elementKey+" success!");
}
}
//add element to cache
public static void putElementToCache(Cache cache,String elementKey,Object elementValue){
Element element=null;
if(cache.get(elementKey.trim())!=null){
cache.remove(elementKey.trim());
}
element=new Element(elementKey.trim(),elementValue);
cache.put(element);
logger.info("add element:"+elementKey+" success!");
}
//get element by elementKey and cacheName
public static Object getElementValueFromCache(String cacheName,String elementKey){
Object result=null;
Cache cache=null;
if(cacheName==null||cacheName.trim().equals("")){
logger.error("Failed to get cache element,cacheName is null");
}else if(elementKey==null){
logger.error("Failed to get cache element,elementKey is null");
}else{
if(getCacheByName(cacheName.trim())!=null){
cache=getCacheByName(cacheName.trim());
Element element=null;
if(cache.get(elementKey.trim())!=null){
element=cache.get(elementKey.trim());
if(element.getObjectValue()==null){
logger.error("The value of "+elementKey+" in the cache is empty");
}else{
result=element.getObjectValue();
}
}else{
logger.error("There is no:"+elementKey+" in the cache");
}
}else{
logger.error("Failed to get cache element,The element:"+cacheName+" in the cache is empty");
}
}
return result;
}
//get element by elementKey and cache
public static Object getElementValueFromCache(Cache cache,String elementKey){
Object result=null;
Element element=null;
if(cache.get(elementKey.trim())!=null){
element=cache.get(elementKey.trim());
if(element.getObjectValue()==null){
logger.error("The value of "+elementKey+" in the cache is empty");
}else{
result=element.getObjectValue();
}
}
return result;
}
/**
* get cache Keys
*
* @param cacheName
* @return
*/
@SuppressWarnings("unchecked")
public static List<String> getKeys(String cacheName) {
Ehcache cache = getCacheManager().getEhcache(cacheName);
return (List<String>) cache.getKeys();
}
/**
* 把所有cache中的内容删除,但是cache对象还是保留.
* Clears the contents of all caches in the CacheManager,
* but without removing any caches.
*/
public static void clearAllFromCacheManager(){
if(getCacheManager()!=null){
getCacheManager().clearAll();
logger.info("CacheManager was clearAll...");
}
}
/**
* Delete the specified object
*
* @param cacheName
* @param key
* @return
*/
public static boolean remove(String cacheName, String key) {
return getCacheManager().getCache(cacheName).remove(key);
}
/**
* 获取缓存大小
*
* @param cacheName
* @return
*/
public static int getSize(String cacheName) {
return getCacheManager().getCache(cacheName).getSize();
}
/**
*把所有cache对象都删除。慎用!
* When you don't caching, turn it off, or you'll take up CPU and memory resources
*
*/
public static void shutdownCacheManager(){
if(getCacheManager()!=null){
getCacheManager().shutdown();
logger.info("CacheManager was shutdown...");
}
}
/**
* for test1
* @param cache
*/
public static void printCache(Cache cache){
logger.info("缓存状态: "+cache.getStatus().toString());
if(cache==null){
logger.info("cache is null,no print info.");
}else if(cache.getStatus().toString().equals(Status.STATUS_UNINITIALISED)){
logger.info("缓存状态: 未初始化"+cache.getStatus().toString());
}else if(cache.getStatus().toString().equals(Status.STATUS_SHUTDOWN)){
logger.info("缓存状态: 已关闭"+cache.getStatus().toString());
}else if(cache.getStatus().toString().trim().equals(Status.STATUS_ALIVE)){
System.out.println("进入缓存取数据");
if(cache.getKeys().size()==0){
logger.info(cache.getName()+" exits,but no value.");
}else{
for(int i=0;i<cache.getKeys().size();i++){
Object thekey=cache.getKeys().get(i);
Object thevalue=cache.get(thekey);
logger.info(cache.getName()+"--"+i+",key:"+thekey.toString()+",value:"+thevalue.toString());
}
}
}
}
/**
* for test1
* @param cacheName
*/
public static void printCacheByName(String cacheName){
if(cacheName==null||cacheName.trim().equals("")){
logger.info("cacheName is null,no print info.");
}else{
if(getCacheManager().getCache(cacheName.trim())!=null){
Cache cache=getCacheManager().getCache(cacheName.trim());
printCache(cache);
}else{
logger.info(cacheName+" --null");
}
}
}
public static void main(String[] sdfsf){
Cache cache1= EhCacheManager.getCacheByName(EhCacheManager.CACHE_MYCOMMONCACHE);
printCache(cache1);
EhCacheManager.putElementToCache(EhCacheManager.CACHE_MYCOMMONCACHE, "111", "111haah");
EhCacheManager.putElementToCache(EhCacheManager.CACHE_MYCOMMONCACHE, "222", "222haah");
EhCacheManager.putElementToCache(EhCacheManager.CACHE_MYCOMMONCACHE, "333", "333haah");
printCache(cache1);
EhCacheManager.putElementToCache(EhCacheManager.CACHE_MYCOMMONCACHE, "111", "111的新值。");
logger.info(EhCacheManager.getElementValueFromCache(EhCacheManager.CACHE_MYCOMMONCACHE, "111"));
printCache(cache1);
clearAllFromCacheManager();
printCache(cache1);
//removalAllFromCacheManager();
printCache(cache1);
shutdownCacheManager();
}
}
注意:
servlet结合ehcache后,重启tomcat会导致缓存无法释放,关闭tomcat后但后台进程依旧存在,所以需要在servlet的destroy方法中调用EhCacheManager的shutdownCacheManager方法把所有cache对象都删除
public void destroy() {
LOGGER.warn("Start releasing caching");
EhCacheManager.shutdownCacheManager();
LOGGER.warn("End the release cache");
}