首先,引入需要的jar
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>net.sf.jsr107cache</groupId>
<artifactId>jsr107cache</artifactId>
<version>1.1</version>
</dependency>
配置文件 ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToLiveSeconds="7200" overflowToDisk="true"/> <cache name="cpamDefaultCache" maxElementsInMemory="10000" eternal="true" overflowToDisk="true" timeToIdleSeconds="1800" timeToLiveSeconds="14400" memoryStoreEvictionPolicy="LFU" /> </ehcache>
<!--配置缓存管理器 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>classpath:/config/deploy/ehcache.xml</value> </property> </bean>
import javax.annotation.Resource; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import cn.gov.zjport.khi.manage.service.CacheManagerService; /** * 缓存管理器。 * * @author <a href="mailto:[email protected]">fengtao</a> * @version $Id$ * @since 2.0 */ @Service("cacheManagerService") public class CacheManagerServiceImpl implements CacheManagerService { /** 日志 **/ private final Logger logger = LoggerFactory.getLogger(this.getClass()); /** 缓存管理对象 **/ @Resource private CacheManager cacheManager; /** * @param constant * String * @param key * String * @return Object */ public Object getCacheValue(String constant, String key) { logger.debug("从缓存域{}中得到缓存开始....,key={}", constant, key); Cache cache = cacheManager.getCache(constant); Element element = cache.get(key); Object obj = null == element ? null : element.getObjectValue(); logger.debug("得到的缓存值:" + obj); return obj; } /** * @param constant * String * @param key * String * @return Object */ public boolean removeCacheValue(String constant, String key) { logger.debug("从缓存域{}中移除缓存开始....,key={}", constant, key); Cache cache = cacheManager.getCache(constant); boolean result = cache.remove(key); logger.debug("得到的缓存值:" + result); return result; } /** * @param constant * String * @param key * String * @param obj * Object */ public void setCacheValue(String constant, String key, Object obj) { logger.debug("将数据存储到缓存域{}开始....,key={}", constant, key); Cache cache = cacheManager.getCache(constant); Element element = new Element(key, obj); element.setEternal(false); cache.put(element); } public void cleanAll() { logger.debug("清除所有缓存执行开始...."); cacheManager.clearAll(); } }
/** * 缓存管理器。 * * @author <a href="mailto:[email protected]">fengtao</a> * @version $Id$ * @since 2.0 */ public interface CacheManagerService { /** * 根据缓存域和key得到缓存数据。 * * @param constant * String 缓存域 * @param key * String 键 * @return Object */ public Object getCacheValue(String constant, String key); /** * 根据缓存域和key移除缓存数据。 * * @param constant * String 缓存域 * @param key * String 键 * @return Object */ public boolean removeCacheValue(String constant, String key); /** * 存储缓存数据。 * * @param constant * String 缓存域 * @param key * String 键 * @param obj * Object 值 */ public void setCacheValue(String constant, String key, Object obj); /** * 清除所有缓存。 */ public void cleanAll(); }