日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java中常用緩存Cache機(jī)制的實(shí)現(xiàn)

Cache

鐵門關(guān)網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,鐵門關(guān)網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為鐵門關(guān)成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的鐵門關(guān)做網(wǎng)站的公司定做!

所謂緩存,就是將程序或系統(tǒng)經(jīng)常要調(diào)用的對(duì)象存在內(nèi)存中,一遍其使用時(shí)可以快速調(diào)用,不必再去創(chuàng)建新的重復(fù)的實(shí)例。這樣做可以減少系統(tǒng)開銷,提高系統(tǒng)效率。

緩存主要可分為二大類: 

一、通過文件緩存,顧名思義文件緩存是指把數(shù)據(jù)存儲(chǔ)在磁盤上,不管你是以XML格式,序列化文件DAT格式還是其它文件格式;  

二、內(nèi)存緩存,也就是實(shí)現(xiàn)一個(gè)類中靜態(tài)Map,對(duì)這個(gè)Map進(jìn)行常規(guī)的增刪查. 

代碼如下 :

 
 
  1. package lhm.hcy.guge.frameset.cache; 
  2.  
  3. import java.util.*; 
  4.  
  5.  //Description: 管理緩存 
  6.  
  7.  //可擴(kuò)展的功能:當(dāng)chche到內(nèi)存溢出時(shí)必須清除掉最早期的一些緩存對(duì)象,這就要求對(duì)每個(gè)緩存對(duì)象保存創(chuàng)建時(shí)間 
  8.  
  9. public class CacheManager { 
  10.     private static HashMap cacheMap = new HashMap(); 
  11.  
  12.     //單實(shí)例構(gòu)造方法 
  13.     private CacheManager() { 
  14.         super(); 
  15.     } 
  16.     //獲取布爾值的緩存 
  17.     public static boolean getSimpleFlag(String key){ 
  18.         try{ 
  19.             return (Boolean) cacheMap.get(key); 
  20.         }catch(NullPointerException e){ 
  21.             return false; 
  22.         } 
  23.     } 
  24.     public static long getServerStartdt(String key){ 
  25.         try { 
  26.             return (Long)cacheMap.get(key); 
  27.         } catch (Exception ex) { 
  28.             return 0; 
  29.         } 
  30.     } 
  31.     //設(shè)置布爾值的緩存 
  32.     public synchronized static boolean setSimpleFlag(String key,boolean flag){ 
  33.         if (flag && getSimpleFlag(key)) {//假如為真不允許被覆蓋 
  34.             return false; 
  35.         }else{ 
  36.             cacheMap.put(key, flag); 
  37.             return true; 
  38.         } 
  39.     } 
  40.     public synchronized static boolean setSimpleFlag(String key,long serverbegrundt){ 
  41.         if (cacheMap.get(key) == null) { 
  42.             cacheMap.put(key,serverbegrundt); 
  43.             return true; 
  44.         }else{ 
  45.             return false; 
  46.         } 
  47.     } 
  48.  
  49.  
  50.     //得到緩存。同步靜態(tài)方法 
  51.     private synchronized static Cache getCache(String key) { 
  52.         return (Cache) cacheMap.get(key); 
  53.     } 
  54.  
  55.     //判斷是否存在一個(gè)緩存 
  56.     private synchronized static boolean hasCache(String key) { 
  57.         return cacheMap.containsKey(key); 
  58.     } 
  59.  
  60.     //清除所有緩存 
  61.     public synchronized static void clearAll() { 
  62.         cacheMap.clear(); 
  63.     } 
  64.  
  65.     //清除某一類特定緩存,通過遍歷HASHMAP下的所有對(duì)象,來判斷它的KEY與傳入的TYPE是否匹配 
  66.     public synchronized static void clearAll(String type) { 
  67.         Iterator i = cacheMap.entrySet().iterator(); 
  68.         String key; 
  69.         ArrayList arr = new ArrayList(); 
  70.         try { 
  71.             while (i.hasNext()) { 
  72.                 java.util.Map.Entry entry = (java.util.Map.Entry) i.next(); 
  73.                 key = (String) entry.getKey(); 
  74.                 if (key.startsWith(type)) { //如果匹配則刪除掉 
  75.                     arr.add(key); 
  76.                 } 
  77.             } 
  78.             for (int k = 0; k < arr.size(); k++) { 
  79.                 clearOnly(arr.get(k)); 
  80.             } 
  81.         } catch (Exception ex) { 
  82.             ex.printStackTrace(); 
  83.         } 
  84.     } 
  85.  
  86.     //清除指定的緩存 
  87.     public synchronized static void clearOnly(String key) { 
  88.         cacheMap.remove(key); 
  89.     } 
  90.  
  91.     //載入緩存 
  92.     public synchronized static void putCache(String key, Cache obj) { 
  93.         cacheMap.put(key, obj); 
  94.     } 
  95.  
  96.     //獲取緩存信息 
  97.     public static Cache getCacheInfo(String key) { 
  98.  
  99.         if (hasCache(key)) { 
  100.             Cache cache = getCache(key); 
  101.             if (cacheExpired(cache)) { //調(diào)用判斷是否終止方法 
  102.                 cache.setExpired(true); 
  103.             } 
  104.             return cache; 
  105.         }else 
  106.             return null; 
  107.     } 
  108.  
  109.     //載入緩存信息 
  110.     public static void putCacheInfo(String key, Cache obj, long dt,boolean expired) { 
  111.         Cache cache = new Cache(); 
  112.         cache.setKey(key); 
  113.         cache.setTimeOut(dt + System.currentTimeMillis()); //設(shè)置多久后更新緩存 
  114.         cache.setValue(obj); 
  115.         cache.setExpired(expired); //緩存默認(rèn)載入時(shí),終止?fàn)顟B(tài)為FALSE 
  116.         cacheMap.put(key, cache); 
  117.     } 
  118.     //重寫載入緩存信息方法 
  119.     public static void putCacheInfo(String key,Cache obj,long dt){ 
  120.         Cache cache = new Cache(); 
  121.         cache.setKey(key); 
  122.         cache.setTimeOut(dt+System.currentTimeMillis()); 
  123.         cache.setValue(obj); 
  124.         cache.setExpired(false); 
  125.         cacheMap.put(key,cache); 
  126.     } 
  127.  
  128.     //判斷緩存是否終止 
  129.     public static boolean cacheExpired(Cache cache) { 
  130.         if (null == cache) { //傳入的緩存不存在 
  131.             return false; 
  132.         } 
  133.         long nowDt = System.currentTimeMillis(); //系統(tǒng)當(dāng)前的毫秒數(shù) 
  134.         long cacheDt = cache.getTimeOut(); //緩存內(nèi)的過期毫秒數(shù) 
  135.         if (cacheDt <= 0||cacheDt>nowDt) { //過期時(shí)間小于等于零時(shí),或者過期時(shí)間大于當(dāng)前時(shí)間時(shí),則為FALSE 
  136.             return false; 
  137.         } else { //大于過期時(shí)間 即過期 
  138.             return true; 
  139.         } 
  140.     } 
  141.  
  142.     //獲取緩存中的大小 
  143.     public static int getCacheSize() { 
  144.         return cacheMap.size(); 
  145.     } 
  146.  
  147.     //獲取指定的類型的大小 
  148.     public static int getCacheSize(String type) { 
  149.         int k = 0; 
  150.         Iterator i = cacheMap.entrySet().iterator(); 
  151.         String key; 
  152.         try { 
  153.             while (i.hasNext()) { 
  154.                 java.util.Map.Entry entry = (java.util.Map.Entry) i.next(); 
  155.                 key = (String) entry.getKey(); 
  156.                 if (key.indexOf(type) != -1) { //如果匹配則刪除掉 
  157.                     k++; 
  158.                 } 
  159.             } 
  160.         } catch (Exception ex) { 
  161.             ex.printStackTrace(); 
  162.         } 
  163.  
  164.         return k; 
  165.     } 
  166.  
  167.     //獲取緩存對(duì)象中的所有鍵值名稱 
  168.     public static ArrayList getCacheAllkey() { 
  169.         ArrayList a = new ArrayList(); 
  170.         try { 
  171.             Iterator i = cacheMap.entrySet().iterator(); 
  172.             while (i.hasNext()) { 
  173.                 java.util.Map.Entry entry = (java.util.Map.Entry) i.next(); 
  174.                 a.add((String) entry.getKey()); 
  175.             } 
  176.         } catch (Exception ex) {} finally { 
  177.             return a; 
  178.         } 
  179.     } 
  180.  
  181.     //獲取緩存對(duì)象中指定類型 的鍵值名稱 
  182.     public static ArrayList getCacheListkey(String type) { 
  183.         ArrayList a = new ArrayList(); 
  184.         String key; 
  185.         try { 
  186.             Iterator i = cacheMap.entrySet().iterator(); 
  187.             while (i.hasNext()) { 
  188.                 java.util.Map.Entry entry = (java.util.Map.Entry) i.next(); 
  189.                 key = (String) entry.getKey(); 
  190.                 if (key.indexOf(type) != -1) { 
  191.                     a.add(key); 
  192.                 } 
  193.             } 
  194.         } catch (Exception ex) {} finally { 
  195.             return a; 
  196.         } 
  197.     } 
  198.  
  199.  
  200.  
  201. package lhm.hcy.guge.frameset.cache; 
  202.  
  203. public class Cache { 
  204.         private String key;//緩存ID 
  205.         private Object value;//緩存數(shù)據(jù) 
  206.         private long timeOut;//更新時(shí)間 
  207.         private boolean expired; //是否終止 
  208.         public Cache() { 
  209.                 super(); 
  210.         } 
  211.  
  212.         public Cache(String key, Object value, long timeOut, boolean expired) { 
  213.                 this.key = key; 
  214.                 this.value = value; 
  215.                 this.timeOut = timeOut; 
  216.                 this.expired = expired; 
  217.         } 
  218.  
  219.         public String getKey() { 
  220.                 return key; 
  221.         } 
  222.  
  223.         public long getTimeOut() { 
  224.                 return timeOut; 
  225.         } 
  226.  
  227.         public Object getValue() { 
  228.                 return value; 
  229.         } 
  230.  
  231.         public void setKey(String string) { 
  232.                 key = string; 
  233.         } 
  234.  
  235.         public void setTimeOut(long l) { 
  236.                 timeOut = l; 
  237.         } 
  238.  
  239.         public void setValue(Object object) { 
  240.                 value = object; 
  241.         } 
  242.  
  243.         public boolean isExpired() { 
  244.                 return expired; 
  245.         } 
  246.  
  247.         public void setExpired(boolean b) { 
  248.                 expired = b; 
  249.         } 
  250.  
  251. //測(cè)試類, 
  252. class Test { 
  253.     public static void main(String[] args) { 
  254.         System.out.println(CacheManager.getSimpleFlag("alksd")); 
  255. //        CacheManager.putCache("abc", new Cache()); 
  256. //        CacheManager.putCache("def", new Cache()); 
  257. //        CacheManager.putCache("ccc", new Cache()); 
  258. //        CacheManager.clearOnly(""); 
  259. //        Cache c = new Cache(); 
  260. //        for (int i = 0; i < 10; i++) { 
  261. //            CacheManager.putCache("" + i, c); 
  262. //        } 
  263. //        CacheManager.putCache("aaaaaaaa", c); 
  264. //        CacheManager.putCache("abchcy;alskd", c); 
  265. //        CacheManager.putCache("cccccccc", c); 
  266. //        CacheManager.putCache("abcoqiwhcy", c); 
  267. //        System.out.println("刪除前的大?。?+CacheManager.getCacheSize()); 
  268. //        CacheManager.getCacheAllkey(); 
  269. //        CacheManager.clearAll("aaaa"); 
  270. //        System.out.println("刪除后的大?。?+CacheManager.getCacheSize()); 
  271. //        CacheManager.getCacheAllkey(); 
  272.  
  273.  
  274.     } 

新聞名稱:Java中常用緩存Cache機(jī)制的實(shí)現(xiàn)
網(wǎng)頁(yè)網(wǎng)址:http://www.5511xx.com/article/dhijgog.html