Java动态缓存技术:WEB缓存应用
可以实现不等待,线程自动更新缓存
java动态缓jar包http://dl2.csdn.net/down4/20070918/18203740920.jar
源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package com.cari.web.cache; /** *//** * @author zsy * */ public class CacheData ...{ private Object data; private long time; private int count; public CacheData() ...{ } public CacheData(Object data, long time, int count) ...{ this.data = data; this.time = time; this.count = count; } public CacheData(Object data) ...{ this.data = data; this.time = System.currentTimeMillis(); this.count = 1; } public void addCount() ...{ count++; } public int getCount() ...{ return count; } public void setCount(int count) ...{ this.count = count; } public Object getData() ...{ return data; } public void setData(Object data) ...{ this.data = data; } public long getTime() ...{ return time; } public void setTime(long time) ...{ this.time = time; } } |
CacheOperation.java 缓存处理类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | package com.cari.web.cache; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Hashtable; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** *//** * @author zsy */ public class CacheOperation ...{ private static final Log log = LogFactory.getLog(CacheOperation.class); private static CacheOperation singleton = null; private Hashtable cacheMap;//存放缓存数据 private ArrayList threadKeys;//处于线程更新中的key值列表 public static CacheOperation getInstance() ...{ if (singleton == null) ...{ singleton = new CacheOperation(); } return singleton; } private CacheOperation() ...{ cacheMap = new Hashtable(); threadKeys = new ArrayList(); } /** *//** * 添加数据缓存 * 与方法getCacheData(String key, long intervalTime, int maxVisitCount)配合使用 * @param key * @param data */ public void addCacheData(String key, Object data) ...{ addCacheData(key, data, true); } private void addCacheData(String key, Object data, boolean check) ...{ if (Runtime.getRuntime().freeMemory() < 5L*1024L*1024L) ...{//虚拟机内存小于10兆,则清除缓存 log.warn("WEB缓存:内存不足,开始清空缓存!"); removeAllCacheData(); return; } else if(check && cacheMap.containsKey(key)) ...{ log.warn("WEB缓存:key值= " + key + " 在缓存中重复, 本次不缓存!"); return; } cacheMap.put(key, new CacheData(data)); } /** *//** * 取得缓存中的数据 * 与方法addCacheData(String key, Object data)配合使用 * @param key * @param intervalTime 缓存的时间周期,小于等于0时不限制 * @param maxVisitCount 访问累积次数,小于等于0时不限制 * @return */ public Object getCacheData(String key, long intervalTime, int maxVisitCount) ...{ CacheData cacheData = (CacheData)cacheMap.get(key); if (cacheData == null) ...{ return null; } if (intervalTime > 0 && (System.currentTimeMillis() - cacheData.getTime()) > intervalTime) ...{ removeCacheData(key); return null; } if (maxVisitCount > 0 && (maxVisitCount - cacheData.getCount()) <= 0) ...{ removeCacheData(key); return null; } else ...{ cacheData.addCount(); } return cacheData.getData(); } |

