Spring整合Ehcache

整体步骤:

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>


 


application.xml,  bean 配置

<?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;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值