一、定义一个工具类
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
/**
* ehcache 缓存工具类
*/
public class EHCacheUtils {
public static final String MAP_CACHE_NAME = "mapCache";
public static final String ENTITY_CACHE_NAME = "entityCache";
public static final String COMMON_CACHE_NAME = "commonCache";
public static final String DICTIONARY_CACHE_NAME = "dictionaryCache";
public static final CacheManager manager = CacheManager.create();
public static Map<String, Cache> cacheMap = new HashMap<>();
/**
* 根据缓存库名称和key获取value
*
* @param cacheName 缓存名称
* @param key key值
* @return 返回缓存的值
*/
public static Object getCacheValue(String cacheName, Object key) {
Cache cache = getCache(cacheName);
if (cache != null) {
Element element = cache.get(key);
if (element != null) {
return element.getObjectValue();
}
}
return null;
}
/**
* 根据缓存名称和key存入一个缓存
*
* @param cacheName 缓存名称
* @param key key值
* @param value 缓存的值
*/
public static void putCacheValue(String cacheName, Object key, Object value) {
Cache cache = getCache(cacheName);
if (cache != null) {
cache.put(new Element(key, value));
}
}
/**
* 删除一个缓存
*
* @param cacheName 缓存名称
* @param key key值
*/
@NotNull
public static Boolean removeCacheValue(String cacheName, Object key) {
Cache cache = getCache(cacheName);
if (cache != null) {
return cache.remove(key);
}
return false;
}
public static Cache getCache(String cacheName) {
if (cacheMap.get(cacheName) == null) {
cacheMap.put(cacheName, manager.getCache(cacheName));
}
return manager.getCache(cacheName);
}
}
二、进行使用
/**
* 查询所有的城市信息
* 缓冲中有直接返回,没有存入缓冲返回
*/
@SuppressWarnings("unchecked")
@Override
public List<DictionaryVO> selectAllCities() {
if (EHCacheUtils.getCacheValue(EHCacheUtils.DICTIONARY_CACHE_NAME, DictionaryConstants.OP_TYPE_CITY) == null) {
EHCacheUtils.putCacheValue(EHCacheUtils.DICTIONARY_CACHE_NAME, DictionaryConstants.OP_TYPE_CITY, commonDao.selectAllCities());
}
return (List<DictionaryVO>) EHCacheUtils.getCacheValue(EHCacheUtils.DICTIONARY_CACHE_NAME, DictionaryConstants.OP_TYPE_CITY);
}
三、ehcache的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!-- 磁盘缓存位置 -->
<diskStore path="D:/Workspace/IdeaWorkSpace/zld_refacting/ehcache"/>
<!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="1000000"
eternal="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
<!-- 数据字典缓存,主要用来缓存数据字典的内容以及省份、城市、逻辑链路等一些信息 -->
<cache name="dictionaryCache"
maxElementsInMemory="1000000"
eternal="true"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
<!-- commonCache缓存,主要用来缓存默认省份代码以及一些公共常量 -->
<cache name="commonCache"
maxElementsInMemory="1000000"
eternal="true"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>