所謂緩存,就是將程序或系統經常要調用的對象存在內存中,一遍其使用時可以快速調用,不必再去創建新的重復的實例。這樣做可以減少系統開銷,提高系統效率。
緩存主要可分為二大類:
一、通過文件緩存,顧名思義文件緩存是指把數據存儲在磁盤上,不管你是以XML格式,序列化文件DAT格式還是其它文件格式;
二、內存緩存,也就是實現一個類中靜態Map,對這個Map進行常規的增刪查.
代碼如下:
1.packagelhm.hcy.guge.frameset.cache;
2.
3.importjava.util.*;
4.
5.//Description:管理緩存
6.
7.//可擴展的功能:當chche到內存溢出時必須清除掉早期的一些緩存對象,這就要求對每個緩存對象保存創建時間
8.
9.publicclassCacheManager{
10.privatestaticHashMapcacheMap=newHashMap();
11.
12.//單實例構造方法
13.privateCacheManager(){
14.super();
15.}
16.//獲取布爾值的緩存
17.publicstaticbooleangetSimpleFlag(Stringkey){
18.try{
19.return(Boolean)cacheMap.get(key);
20.}catch(NullPointerExceptione){
21.returnfalse;
22.}
23.}
24.publicstaticlonggetServerStartdt(Stringkey){
25.try{
26.return(Long)cacheMap.get(key);
27.}catch(Exceptionex){
28.return0;
29.}
30.}
31.//設置布爾值的緩存
32.publicsynchronizedstaticbooleansetSimpleFlag(Stringkey,booleanflag){
33.if(flag&&getSimpleFlag(key)){//假如為真不允許被覆蓋
34.returnfalse;
35.}else{
36.cacheMap.put(key,flag);
37.returntrue;
38.}
39.}
40.publicsynchronizedstaticbooleansetSimpleFlag(Stringkey,longserverbegrundt){
41.if(cacheMap.get(key)==null){
42.cacheMap.put(key,serverbegrundt);
43.returntrue;
44.}else{
45.returnfalse;
46.}
47.}
48.
49.
50.//得到緩存。同步靜態方法
51.privatesynchronizedstaticCachegetCache(Stringkey){
52.return(Cache)cacheMap.get(key);
53.}
54.
55.//判斷是否存在一個緩存
56.privatesynchronizedstaticbooleanhasCache(Stringkey){
57.returncacheMap.containsKey(key);
58.}
59.
60.//清除所有緩存
61.publicsynchronizedstaticvoidclearAll(){
62.cacheMap.clear();
63.}
64.
65.//清除某一類特定緩存,通過遍歷HASHMAP下的所有對象,來判斷它的KEY與傳入的TYPE是否匹配
66.publicsynchronizedstaticvoidclearAll(Stringtype){
67.Iteratori=cacheMap.entrySet().iterator();
68.Stringkey;
69.ArrayListarr=newArrayList();
70.try{
71.while(i.hasNext()){
72.java.util.Map.Entryentry=(java.util.Map.Entry)i.next();
73.key=(String)entry.getKey();
74.if(key.startsWith(type)){//如果匹配則刪除掉
75.arr.add(key);
76.}
77.}
78.for(intk=0;k<arr.size();k++){
79.clearOnly(arr.get(k));
80.}
81.}catch(Exceptionex){
82.ex.printStackTrace();
83.}
84.}
85.
86.//清除指定的緩存
87.publicsynchronizedstaticvoidclearOnly(Stringkey){
88.cacheMap.remove(key);
89.}
90.
91.//載入緩存
92.publicsynchronizedstaticvoidputCache(Stringkey,Cacheobj){
93.cacheMap.put(key,obj);
94.}
95.
96.//獲取緩存信息
97.publicstaticCachegetCacheInfo(Stringkey){
98.
99.if(hasCache(key)){
100.Cachecache=getCache(key);
101.if(cacheExpired(cache)){//調用判斷是否終止方法
102.cache.setExpired(true);
103.}
104.returncache;
105.}else
106.returnnull;
107.}
108.
109.//載入緩存信息
110.publicstaticvoidputCacheInfo(Stringkey,Cacheobj,longdt,booleanexpired){
111.Cachecache=newCache();
112.cache.setKey(key);
113.cache.setTimeOut(dt+System.currentTimeMillis());//設置多久后更新緩存
114.cache.setValue(obj);
115.cache.setExpired(expired);//緩存默認載入時,終止狀態為FALSE
116.cacheMap.put(key,cache);
117.}
118.//重寫載入緩存信息方法
119.publicstaticvoidputCacheInfo(Stringkey,Cacheobj,longdt){
120.Cachecache=newCache();
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.publicstaticbooleancacheExpired(Cachecache){
130.if(null==cache){//傳入的緩存不存在
131.returnfalse;
132.}
133.longnowDt=System.currentTimeMillis();//系統當前的毫秒數
134.longcacheDt=cache.getTimeOut();//緩存內的過期毫秒數
135.if(cacheDt<=0||cacheDt>nowDt){//過期時間小于等于零時,或者過期時間大于當前時間時,則為FALSE
136.returnfalse;
137.}else{//大于過期時間即過期
138.returntrue;
139.}
140.}
141.
142.//獲取緩存中的大小
143.publicstaticintgetCacheSize(){
144.returncacheMap.size();
145.}
146.
147.//獲取指定的類型的大小
148.publicstaticintgetCacheSize(Stringtype){
149.intk=0;
150.Iteratori=cacheMap.entrySet().iterator();
151.Stringkey;
152.try{
153.while(i.hasNext()){
154.java.util.Map.Entryentry=(java.util.Map.Entry)i.next();
155.key=(String)entry.getKey();
156.if(key.indexOf(type)!=-1){//如果匹配則刪除掉
157.k++;
158.}
159.}
160.}catch(Exceptionex){
161.ex.printStackTrace();
162.}
163.
164.returnk;
165.}
166.
167.//獲取緩存對象中的所有鍵值名稱
168.publicstaticArrayListgetCacheAllkey(){
169.ArrayLista=newArrayList();
170.try{
171.Iteratori=cacheMap.entrySet().iterator();
172.while(i.hasNext()){
173.java.util.Map.Entryentry=(java.util.Map.Entry)i.next();
174.a.add((String)entry.getKey());
175.}
176.}catch(Exceptionex){}finally{
177.returna;
178.}
179.}
180.
181.//獲取緩存對象中指定類型的鍵值名稱
182.publicstaticArrayListgetCacheListkey(Stringtype){
183.ArrayLista=newArrayList();
184.Stringkey;
185.try{
186.Iteratori=cacheMap.entrySet().iterator();
187.while(i.hasNext()){
188.java.util.Map.Entryentry=(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(Exceptionex){}finally{
195.returna;
196.}
197.}
198.
199.}
200.
201.
202.packagelhm.hcy.guge.frameset.cache;
203.
204.publicclassCache{
205.privateStringkey;//緩存ID
206.privateObjectvalue;//緩存數據
207.privatelongtimeOut;//更新時間
208.privatebooleanexpired;//是否終止
209.publicCache(){
210.super();
211.}
212.
213.publicCache(Stringkey,Objectvalue,longtimeOut,booleanexpired){
214.this.key=key;
215.this.value=value;
216.this.timeOut=timeOut;
217.this.expired=expired;
218.}
219.
220.publicStringgetKey(){
221.returnkey;
222.}
223.
224.publiclonggetTimeOut(){
225.returntimeOut;
226.}
227.
228.publicObjectgetValue(){
229.returnvalue;
230.}
231.
232.publicvoidsetKey(Stringstring){
233.key=string;
234.}
235.
236.publicvoidsetTimeOut(longl){
237.timeOut=l;
238.}
239.
240.publicvoidsetValue(Objectobject){
241.value=object;
242.}
243.
244.publicbooleanisExpired(){
245.returnexpired;
246.}
247.
248.publicvoidsetExpired(booleanb){
249.expired=b;
250.}
251.}
252.
253.//測試類,
254.classTest{
255.publicstaticvoidmain(String[]args){
256.System.out.println(CacheManager.getSimpleFlag("alksd"));
257.//CacheManager.putCache("abc",newCache());
258.//CacheManager.putCache("def",newCache());
259.//CacheManager.putCache("ccc",newCache());
260.//CacheManager.clearOnly("");
261.//Cachec=newCache();
262.//for(inti=0;i<10;i++){
263.//CacheManager.putCache(""+i,c);
264.//}
265.//CacheManager.putCache("aaaaaaaa",c);
266.//CacheManager.putCache("abchcy;alskd",c);
267.//CacheManager.putCache("cccccccc",c);
268.//CacheManager.putCache("abcoqiwhcy",c);
269.//System.out.println("刪除前的大小:"+CacheManager.getCacheSize());
270.//CacheManager.getCacheAllkey();
271.//CacheManager.clearAll("aaaa");
272.//System.out.println("刪除后的大小:"+CacheManager.getCacheSize());
273.//CacheManager.getCacheAllkey();.
274.}
275.}
知識講解的怎么樣,對作為Java程序員的你們有沒有幫助?如果你們有好的干貨分享,可以關注動力節點的官方微信,來和動寶兒取得聯系喲。