1 package org.jeecgframework.core.util; 2 3 import net.sf.ehcache.Cache; 4 import net.sf.ehcache.CacheManager; 5 import net.sf.ehcache.Element; 6 7 /** 8 * ehcache 缓存工具类 9 * 10 * cacheName在ehcache.xml中配置11 */12 public class EhcacheUtil {13 14 public static CacheManager manager = CacheManager.create();15 16 public static Object get(String cacheName, Object key) {17 Cache cache = manager.getCache(cacheName);18 if (cache != null) {19 Element element = cache.get(key);20 if (element != null) {21 return element.getObjectValue();22 }23 }24 return null;25 }26 27 public static void put(String cacheName, Object key, Object value) {28 Cache cache = manager.getCache(cacheName);29 if (cache != null) {30 cache.put(new Element(key, value));31 }32 }33 34 public static boolean remove(String cacheName, Object key) {35 Cache cache = manager.getCache(cacheName);36 if (cache != null) {37 return cache.remove(key);38 }39 return false;40 }41 42 public static void main(String[] args) {43 String key = "key";44 String value = "hello";45 EhcacheUtil.put("mytest", key, value);46 //System.out.println(EhcacheUtil.get("mytest", key));47 }48 49 }
1 23 4 5 6 7 11 12 15 16 17 19 23 2422 27 31 3430 38