自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java 中常用緩存Cache機(jī)制的實(shí)現(xiàn)

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

Cache

Cache

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

緩存主要可分為二大類(lèi): 

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

二、內(nèi)存緩存,也就是實(shí)現(xiàn)一個(gè)類(lèi)中靜態(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.     //清除某一類(lèi)特定緩存,通過(guò)遍歷HASHMAP下的所有對(duì)象,來(lái)判斷它的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.     //重寫(xiě)載入緩存信息方法 
  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)的過(guò)期毫秒數(shù) 
  135.         if (cacheDt <= 0||cacheDt>nowDt) { //過(guò)期時(shí)間小于等于零時(shí),或者過(guò)期時(shí)間大于當(dāng)前時(shí)間時(shí),則為FALSE 
  136.             return false
  137.         } else { //大于過(guò)期時(shí)間 即過(guò)期 
  138.             return true
  139.         } 
  140.     } 
  141.  
  142.     //獲取緩存中的大小 
  143.     public static int getCacheSize() { 
  144.         return cacheMap.size(); 
  145.     } 
  146.  
  147.     //獲取指定的類(lèi)型的大小 
  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ì)象中的所有鍵值名稱(chēng) 
  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ì)象中指定類(lèi)型 的鍵值名稱(chēng) 
  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è)試類(lèi), 
  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("刪除前的大?。?quot;+CacheManager.getCacheSize()); 
  268. //        CacheManager.getCacheAllkey(); 
  269. //        CacheManager.clearAll("aaaa"); 
  270. //        System.out.println("刪除后的大?。?quot;+CacheManager.getCacheSize()); 
  271. //        CacheManager.getCacheAllkey(); 
  272.  
  273.  
  274.     } 

 

責(zé)任編輯:張偉 來(lái)源: 安度博客
相關(guān)推薦

2019-10-11 08:41:18

JavaMemcached數(shù)據(jù)庫(kù)

2013-08-02 14:19:50

Java日志緩存

2010-09-26 08:46:06

HTML 5Cache Manif

2011-12-15 09:33:19

Java

2010-04-06 08:48:44

JavaOSCacheJBossCache

2023-05-05 18:38:33

多級(jí)緩存Caffeine開(kāi)發(fā)

2024-04-03 14:31:08

大型語(yǔ)言模型PytorchGQA

2024-12-30 08:55:09

2009-09-22 10:50:04

Hibernate c

2022-08-30 21:01:17

開(kāi)發(fā)Java框架

2023-03-17 16:49:42

開(kāi)發(fā)Java框架

2016-09-06 22:16:42

JavaDOCXPDF

2025-02-05 12:22:21

2023-03-15 17:37:26

Java8ListMap

2021-11-26 09:41:50

繪圖工具軟件工具開(kāi)發(fā)

2023-03-30 08:00:56

MySQL日期函數(shù)

2018-11-30 15:17:38

CPUCache緩存行

2018-07-14 21:59:57

緩存數(shù)據(jù)庫(kù)數(shù)據(jù)

2010-04-06 16:50:07

Oracle數(shù)據(jù)庫(kù)

2021-04-18 18:13:42

SQLHive表格
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)