天天看点

常用缓存Cache机制的实现

http://s9.51cto.com/wyfs02/M02/4D/B8/wKiom1RYPOTQNg_TAABhiu7QyrQ667.jpg-wh_651x-s_3000583049.jpg

Cache

所谓缓存,就是将程序或系统经常要调用的对象存在内存中,一遍其使用时可以快速调用,不必再去创建新的重复的实例。这样做可以减少系统开销,提高系统效率。

缓存主要可分为二大类:

一、通过文件缓存,顾名思义文件缓存是指把数据存储在磁盘上,不管你是以XML格式,序列化文件DAT格式还是其它文件格式; 

二、内存缓存,也就是实现一个类中静态Map,对这个Map进行常规的增删查.

代码如下 :

  1. package lhm.hcy.guge.frameset.cache; 
  2. import java.util.*; 
  3.  //Description: 管理缓存 
  4.  //可扩展的功能:当chche到内存溢出时必须清除掉最早期的一些缓存对象,这就要求对每个缓存对象保存创建时间 
  5. public class CacheManager { 
  6.     private static HashMap cacheMap = new HashMap(); 
  7.     //单实例构造方法 
  8.     private CacheManager() { 
  9.         super(); 
  10.     } 
  11.     //获取布尔值的缓存 
  12.     public static boolean getSimpleFlag(String key){ 
  13.         try{ 
  14.             return (Boolean) cacheMap.get(key); 
  15.         }catch(NullPointerException e){ 
  16.             return false; 
  17.         } 
  18.     public static long getServerStartdt(String key){ 
  19.         try { 
  20.             return (Long)cacheMap.get(key); 
  21.         } catch (Exception ex) { 
  22.             return 0; 
  23.     //设置布尔值的缓存 
  24.     public synchronized static boolean setSimpleFlag(String key,boolean flag){ 
  25.         if (flag && getSimpleFlag(key)) {//假如为真不允许被覆盖 
  26.         }else{ 
  27.             cacheMap.put(key, flag); 
  28.             return true; 
  29.     public synchronized static boolean setSimpleFlag(String key,long serverbegrundt){ 
  30.         if (cacheMap.get(key) == null) { 
  31.             cacheMap.put(key,serverbegrundt); 
  32.     //得到缓存。同步静态方法 
  33.     private synchronized static Cache getCache(String key) { 
  34.         return (Cache) cacheMap.get(key); 
  35.     //判断是否存在一个缓存 
  36.     private synchronized static boolean hasCache(String key) { 
  37.         return cacheMap.containsKey(key); 
  38.     //清除所有缓存 
  39.     public synchronized static void clearAll() { 
  40.         cacheMap.clear(); 
  41.     //清除某一类特定缓存,通过遍历HASHMAP下的所有对象,来判断它的KEY与传入的TYPE是否匹配 
  42.     public synchronized static void clearAll(String type) { 
  43.         Iterator i = cacheMap.entrySet().iterator(); 
  44.         String key; 
  45.         ArrayList arr = new ArrayList(); 
  46.             while (i.hasNext()) { 
  47.                 java.util.Map.Entry entry = (java.util.Map.Entry) i.next(); 
  48.                 key = (String) entry.getKey(); 
  49.                 if (key.startsWith(type)) { //如果匹配则删除掉 
  50.                     arr.add(key); 
  51.                 } 
  52.             } 
  53.             for (int k = 0; k < arr.size(); k++) { 
  54.                 clearOnly(arr.get(k)); 
  55.             ex.printStackTrace(); 
  56.     //清除指定的缓存 
  57.     public synchronized static void clearOnly(String key) { 
  58.         cacheMap.remove(key); 
  59.     //载入缓存 
  60.     public synchronized static void putCache(String key, Cache obj) { 
  61.         cacheMap.put(key, obj); 
  62.     //获取缓存信息 
  63.     public static Cache getCacheInfo(String key) { 
  64.         if (hasCache(key)) { 
  65.             Cache cache = getCache(key); 
  66.             if (cacheExpired(cache)) { //调用判断是否终止方法 
  67.                 cache.setExpired(true); 
  68.             return cache; 
  69.         }else 
  70.             return null; 
  71.     //载入缓存信息 
  72.     public static void putCacheInfo(String key, Cache obj, long dt,boolean expired) { 
  73.         Cache cache = new Cache(); 
  74.         cache.setKey(key); 
  75.         cache.setTimeOut(dt + System.currentTimeMillis()); //设置多久后更新缓存 
  76.         cache.setValue(obj); 
  77.         cache.setExpired(expired); //缓存默认载入时,终止状态为FALSE 
  78.         cacheMap.put(key, cache); 
  79.     //重写载入缓存信息方法 
  80.     public static void putCacheInfo(String key,Cache obj,long dt){ 
  81.         cache.setTimeOut(dt+System.currentTimeMillis()); 
  82.         cache.setExpired(false); 
  83.         cacheMap.put(key,cache); 
  84.     //判断缓存是否终止 
  85.     public static boolean cacheExpired(Cache cache) { 
  86.         if (null == cache) { //传入的缓存不存在 
  87.         long nowDt = System.currentTimeMillis(); //系统当前的毫秒数 
  88.         long cacheDt = cache.getTimeOut(); //缓存内的过期毫秒数 
  89.         if (cacheDt <= 0||cacheDt>nowDt) { //过期时间小于等于零时,或者过期时间大于当前时间时,则为FALSE 
  90.         } else { //大于过期时间 即过期 
  91.     //获取缓存中的大小 
  92.     public static int getCacheSize() { 
  93.         return cacheMap.size(); 
  94.     //获取指定的类型的大小 
  95.     public static int getCacheSize(String type) { 
  96.         int k = 0; 
  97.                 if (key.indexOf(type) != -1) { //如果匹配则删除掉 
  98.                     k++; 
  99.         return k; 
  100.     //获取缓存对象中的所有键值名称 
  101.     public static ArrayList getCacheAllkey() { 
  102.         ArrayList a = new ArrayList(); 
  103.             Iterator i = cacheMap.entrySet().iterator(); 
  104.                 a.add((String) entry.getKey()); 
  105.         } catch (Exception ex) {} finally { 
  106.             return a; 
  107.     //获取缓存对象中指定类型 的键值名称 
  108.     public static ArrayList getCacheListkey(String type) { 
  109.                 if (key.indexOf(type) != -1) { 
  110.                     a.add(key); 
  111. public class Cache { 
  112.         private String key;//缓存ID 
  113.         private Object value;//缓存数据 
  114.         private long timeOut;//更新时间 
  115.         private boolean expired; //是否终止 
  116.         public Cache() { 
  117.                 super(); 
  118.         public Cache(String key, Object value, long timeOut, boolean expired) { 
  119.                 this.key = key; 
  120.                 this.value = value; 
  121.                 this.timeOut = timeOut; 
  122.                 this.expired = expired; 
  123.         public String getKey() { 
  124.                 return key; 
  125.         public long getTimeOut() { 
  126.                 return timeOut; 
  127.         public Object getValue() { 
  128.                 return value; 
  129.         public void setKey(String string) { 
  130.                 key = string; 
  131.         public void setTimeOut(long l) { 
  132.                 timeOut = l; 
  133.         public void setValue(Object object) { 
  134.                 value = object; 
  135.         public boolean isExpired() { 
  136.                 return expired; 
  137.         public void setExpired(boolean b) { 
  138.                 expired = b; 
  139. //测试类, 
  140. class Test { 
  141.     public static void main(String[] args) { 
  142.         System.out.println(CacheManager.getSimpleFlag("alksd")); 
  143. //        CacheManager.putCache("abc", new Cache()); 
  144. //        CacheManager.putCache("def", new Cache()); 
  145. //        CacheManager.putCache("ccc", new Cache()); 
  146. //        CacheManager.clearOnly(""); 
  147. //        Cache c = new Cache(); 
  148. //        for (int i = 0; i < 10; i++) { 
  149. //            CacheManager.putCache("" + i, c); 
  150. //        } 
  151. //        CacheManager.putCache("aaaaaaaa", c); 
  152. //        CacheManager.putCache("abchcy;alskd", c); 
  153. //        CacheManager.putCache("cccccccc", c); 
  154. //        CacheManager.putCache("abcoqiwhcy", c); 
  155. //        System.out.println("删除前的大小:"+CacheManager.getCacheSize()); 
  156. //        CacheManager.getCacheAllkey(); 
  157. //        CacheManager.clearAll("aaaa"); 
  158. //        System.out.println("删除后的大小:"+CacheManager.getCacheSize());