1 packagecom.utils.cacheutils;2
3 importcom.situopenapi.constant.EhcacheConstants;4 importcom.situopenapi.constant.GestureImageConstants;5 importcom.situopenapi.data.systemUtils.ExecResult;6 importnet.sf.ehcache.Cache;7 importnet.sf.ehcache.CacheManager;8 importnet.sf.ehcache.Element;9
10 importjava.util.List;11
12 /**
13 * 使用ehcache进行数据的缓存14 *15 */
16 public classEhcacheManager {17
18 private staticCacheManager cacheManager;19
20 static{21 cacheManagerInit();22 }23
24 /**
25 * EhcacheConstants.CACHE_NAME, cache name26 * EhcacheConstants.MAX_ELEMENTS_MEMORY, 缓存最大个数27 * EhcacheConstants.WHETHER_OVERFLOW_TODISK, 内存不足时是否启用磁盘缓存28 * EhcacheConstants.WHETHER_ETERNAL, 缓存中的对象是否为永久的,如果是,超过设置将被忽略,对象从不过期29 * EhcacheConstants.timeToLiveSeconds, 缓存数据的生存时间;元素从构建到消亡的最大时间间隔值,只在元素不是永久保存时生效;若该值为0表示该元素可以停顿无穷长的时间30 * EhcacheConstants.timeToIdleSeconds 对象在失效前的允许闲置时间,仅当eternal=false对象不是永久有效时使用;可选属性,默认值是0可闲置时间无穷大;31 */
32 private staticCacheManager cacheManagerInit() {33 if (cacheManager == null) {34
35
36 //创建一个缓存管理器
37 cacheManager =CacheManager.create();38 //建立一个缓存实例
39 Cache memoryOnlyCache = newCache(EhcacheConstants.CACHE_NAME, EhcacheConstants.MAX_ELEMENTS_MEMORY, EhcacheConstants.WHETHER_OVERFLOW_TODISK, EhcacheConstants.WHETHER_ETERNAL, EhcacheConstants.timeToLiveSeconds, EhcacheConstants.timeToIdleSeconds);40 //在内存管理器中添加缓存实例
41 cacheManager.addCache(memoryOnlyCache);42 returncacheManager;43 }44 returncacheManager;45 }46
47 /**
48 * 向缓存中添加元素49 *50 *@paramkey51 *@paramvalue52 */
53 public static void put(String ehcacheInstanceName, String key, ExecResultvalue) {54 Cache cache =cacheManager.getCache(ehcacheInstanceName);55 cache.put(newElement(key, value));56
57 }58
59 /**
60 * 根据key获取对应的value值61 *62 *@paramkey63 *@return
64 */
65 public staticObject getValue(String ehcacheInstanceName, Object key) {66 Cache cache =cacheManager.getCache(ehcacheInstanceName);67 Object value =cache.get(key).getObjectValue();68 returnvalue;69 }70
71 /**
72 *获取缓存中最新的结果73 *74 *@return最后一次添加的结果集75 */
76 public staticObject getLastValue(String ehcacheInstanceName) {77 boolean flag = true;78
79 Cache cache =cacheManager.getCache(ehcacheInstanceName);80 List keys =cache.getKeys();81 String string =getMaxKey(keys).toString();82 returngetValue(ehcacheInstanceName, string);;83 }84
85
86 /**
87 * 在缓存管理器中获取一个缓存实例88 * 返回缓存数据个数89 *90 *@return
91 */
92 public static intgetCacheSize(String ehcacheInstanceName) {93 Cache cache =cacheManager.getCache(ehcacheInstanceName);94 returncache.getSize();95 }96
97 private staticObject getMaxKey(List source) {98
99 long[] target = new long[source.size() - 1];100 for (int i = 0; i < source.size() - 1; i++) {101 target[i] =Long.parseLong((String) source.get(i));102 }103 long maxKey = target[0];104 for (int i = 0; i < target.length; i++) {105 if (maxKey
112 }