整体步骤:
1.添加ehcache jar包:ehcache-xxx.jar
maven: <dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.5</version>
</dependency>
2.classpath:下添加ehcache.xml配置文件。
3.在spring 配置文件中定义ehcache bean:
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
<property name="shared" value="true" />
</bean>
<bean id="simpleCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManager" />
<property name="cacheName" value="SimplePageCachingFilter" />
</bean>
4.编写工具类。
5.使用工具类操作缓存实体。
具体代码如下:
maven代码:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.5</version>
</dependency>
ehcache.xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir"/>
<cache name="B2B2CEhCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="36000"
timeToLiveSeconds="36000"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="100"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
<property name="shared" value="true" />
</bean>
<bean id="simpleCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManager" />
<property name="cacheName" value="SimplePageCachingFilter" />
</bean>
</beans>
ehcache 工具类
package cn.eshore.common.cache;
import org.slf4j.Logger;
import cn.eshore.common.utils.Slf4jLogUtils;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class EhCacheUtil {
private static Cache cache = null;
private static Logger logger = Slf4jLogUtils.getLogger(EhCacheUtil.class);
private static Cache getSingleCache(CacheManager cacheManager){
if(cache == null){
return cacheManager.getCache("B2B2CEhCache");
}
return cache;
}
public static boolean putValue(CacheManager cacheManager,String key,Object value){
try {
Cache cacheConfig = getSingleCache(cacheManager);
Element element = new Element(key,value);
cacheConfig.put(element);
return true;
} catch (Exception e) {
logger.debug("缓存存放出错:"+key, e);
return false;
}
}
public static Object getValue(CacheManager cacheManager,String key){
try {
Cache cacheConfig = getSingleCache(cacheManager);
Element element = cacheConfig.get(key);
if(element == null){
return null;
}
return element.getObjectValue();
} catch (Exception e) {
logger.debug("获取缓存对象失败:"+key, e);
return null;
}
}
public static void removeByKey(CacheManager cacheManager,String key){
//获取指定Cache对象
Cache configCache =getSingleCache(cacheManager);
configCache.remove(key);
}
}
在业务类中使用,先注入bean
@Autowired
private CacheManager cacheManager;
使用示例:
private CatgroupView getMainTree(){
CatgroupView mainTree = new CatgroupView();
mainTree = (CatgroupView)EhCacheUtil.getValue(cacheManager, Constants.CATGROUP_CACHE_KEY);
if(mainTree == null){
CatgroupView param1 = new CatgroupView();
CatgroupView catgroupTree = catgroupService.getCatgroup(param1);
//过滤销售目录
if(catgroupTree != null && catgroupTree.getChildren() != null && catgroupTree.getChildren().size() > 0){
for(CatgroupView node:catgroupTree.getChildren()){
if(Constants.CATGROUP_MAIN_IDEN.equals(node.getIdentifier())){
mainTree = node;
}
}
}
EhCacheUtil.putValue(cacheManager, Constants.CATGROUP_CACHE_KEY, mainTree);
}
return mainTree;
}