Ehcache工具类

2018-10-12· 2507 次浏览
Ehcache工具类 ehcache.xml ```html <?xml version="1.0" encoding="UTF-8"?> <!-- 参数说明: (0)diskStore: 临时缓存存放路径 (1)name:Cache的唯一标识。 (2)maxElementsInMemory:内存中最大缓存对象数。 (3)eternal:Element是否永久有效,一旦设置true,timeout将不起作用。 (4)timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 (5)timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。 (6)overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。 (7)maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。 (8)memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略 去清理缓存中的内容。默认策略是LRU(最近最少使用),你也可以设置为FIFO(先进先出)或是LFU(较少使用) --> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="autodetect" dynamicConfig="true"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="true" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="mytest" maxElementsInMemory="100000" eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LRU"> </cache> <cache name="userCache" maxElementsInMemory="50000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="300" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" /> </ehcache> ``` EhcacheUtil.java ```java package com.itshidu.common.util; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; /** * Ehcache 缓存工具类 * cacheName在ehcache.xml中配置 * @author Master.Xia * @date 2018年10月12日 */ public class EhcacheUtil { public static CacheManager manager = CacheManager.create(); /** * 从缓存中获取数据 * @param cacheName (缓存名称) * @param key (键) * @return (键对应的数据) */ public static Object get(String cacheName, Object key) { Cache cache = getCache(cacheName); Element element = cache.get(key); if (element != null) { return element.getObjectValue(); } return null; } /** * 向缓存中加入数据,无超时时间 * @param cacheName (缓存名称) * @param key (键) * @param value (值) */ public static void put(String cacheName, Object key, Object value) { put(cacheName, key, value, 0, 0); } /** * 向缓存中加入数据 * @param cacheName (缓存名称) * @param key (键) * @param value (值) * @param timeToLiveSeconds (自创建时间开始,最多存活多少秒,默认为0,带表无穷大) * @param timeToIdleSeconds (多久不访问则释放,默认为0,带表无穷大) */ public static void put(String cacheName, Object key, Object value,int timeToLiveSeconds,int timeToIdleSeconds) { Element element = new Element(key, value); element.setTimeToLive(timeToLiveSeconds); element.setTimeToIdle(timeToIdleSeconds); getCache(cacheName).put(element); } /** * 从缓存中移除数据 * @param cacheName (缓存名称) * @param key (键) * @return (是否移除成功) */ public static boolean remove(String cacheName, Object key) { return getCache(cacheName).remove(key); } /** * 获取缓存对象,此缓存应该是ehcache.xml中存在的,否则会失败 * @param cacheName (缓存名称) * @return (缓存对象) */ public static Cache getCache(String cacheName) { Cache cache = manager.getCache(cacheName); if(cache==null) { throw new RuntimeException("The cache does not exists : "+cacheName); } return cache; } public static void main(String[] args) { String key = "key"; String value = "hello"; EhcacheUtil.put("mytest", key, value); System.out.println(EhcacheUtil.get("mytest", key)); } } ```