java缓存.docx

上传人:b****5 文档编号:7187142 上传时间:2023-01-21 格式:DOCX 页数:25 大小:25.19KB
下载 相关 举报
java缓存.docx_第1页
第1页 / 共25页
java缓存.docx_第2页
第2页 / 共25页
java缓存.docx_第3页
第3页 / 共25页
java缓存.docx_第4页
第4页 / 共25页
java缓存.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

java缓存.docx

《java缓存.docx》由会员分享,可在线阅读,更多相关《java缓存.docx(25页珍藏版)》请在冰豆网上搜索。

java缓存.docx

java缓存

JAVA缓存

∙1Cache类

∙2CacheManager类

∙1Cache类

∙2CacheManager类

JAVA缓存有两种:

一、文件缓存,是指把数据存储在磁盘上,可以XML格式,也可以序列化文件DAT格式还是其它文件格式。

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

其代码如下:

JAVA缓存-Cache类

publicclassCache{

privateStringkey;//缓存ID

privateObjectvalue;//缓存数据

privatelongtimeOut;//更新时间

privatebooleanexpired;//是否终止

publicCache(){

super();

}

publicCache(Stringkey,Objectvalue,longtimeOut,booleanexpired){

this.key=key;

this.value=value;

this.timeOut=timeOut;

this.expired=expired;

}

publicStringgetKey(){

returnkey;

}

publiclonggetTimeOut(){

returntimeOut;

}

publicObjectgetValue(){

returnvalue;

}

publicvoidsetKey(Stringstring){

key=string;

}

publicvoidsetTimeOut(longl){

timeOut=l;

}

publicvoidsetValue(Objectobject){

value=object;

}

publicbooleanisExpired(){

returnexpired;

}

publicvoidsetExpired(booleanb){

expired=b;

}

}

//测试类,

classTest{

publicstaticvoidmain(String[]args){

System.out.println(CacheManager.getSimpleFlag("alksd"));

//CacheManager.putCache("abc",newCache());

//CacheManager.putCache("def",newCache());

//CacheManager.putCache("ccc",newCache());

//CacheManager.clearOnly("");

//Cachec=newCache();

//for(inti=0;i<10;i++){

//CacheManager.putCache(""+i,c);

//}

//CacheManager.putCache("aaaaaaaa",c);

//CacheManager.putCache("abchcy;alskd",c);

//CacheManager.putCache("cccccccc",c);

//CacheManager.putCache("abcoqiwhcy",c);

//System.out.println("删除前的大小:

"+CacheManager.getCacheSize());

//CacheManager.getCacheAllkey();

//CacheManager.clearAll("aaaa");

//System.out.println("删除后的大小:

"+CacheManager.getCacheSize());

//CacheManager.getCacheAllkey();

}

}

JAVA缓存-CacheManager类

publicclassCacheManager{

privatestaticHashMapcacheMap=newHashMap();

//单实例构造方法

privateCacheManager(){

super();

}

//获取布尔值的缓存

publicstaticbooleangetSimpleFlag(Stringkey){

try{

return(Boolean)cacheMap.get(key);

}catch(NullPointerExceptione){

returnfalse;

}

}

publicstaticlonggetServerStartdt(Stringkey){

try{

return(Long)cacheMap.get(key);

}catch(Exceptionex){

return0;

}

}

//设置布尔值的缓存

publicsynchronizedstaticbooleansetSimpleFlag(Stringkey,booleanflag){

if(flag&&getSimpleFlag(key)){//假如为真不允许被覆盖

returnfalse;

}else{

cacheMap.put(key,flag);

returntrue;

}

}

publicsynchronizedstaticbooleansetSimpleFlag(Stringkey,longserverbegrundt){

if(cacheMap.get(key)==null){

cacheMap.put(key,serverbegrundt);

returntrue;

}else{

returnfalse;

}

}

//得到缓存。

同步静态方法

privatesynchronizedstaticCachegetCache(Stringkey){

return(Cache)cacheMap.get(key);

}

//判断是否存在一个缓存

privatesynchronizedstaticbooleanhasCache(Stringkey){

returncacheMap.containsKey(key);

}

//清除所有缓存

publicsynchronizedstaticvoidclearAll(){

cacheMap.clear();

}

//清除某一类特定缓存,通过遍历HASHMAP下的所有对象,来判断它的KEY与传入的TYPE是否匹配

publicsynchronizedstaticvoidclearAll(Stringtype){

Iteratori=cacheMap.entrySet().iterator();

Stringkey;

ArrayListarr=newArrayList();

try{

while(i.hasNext()){

java.util.Map.Entryentry=(java.util.Map.Entry)i.next();

key=(String)entry.getKey();

if(key.startsWith(type)){//如果匹配则删除掉

arr.add(key);

}

}

for(intk=0;k

clearOnly(arr.get(k));

}

}catch(Exceptionex){

ex.printStackTrace();

}

}

//清除指定的缓存

publicsynchronizedstaticvoidclearOnly(Stringkey){

cacheMap.remove(key);

}

//载入缓存

publicsynchronizedstaticvoidputCache(Stringkey,Cacheobj){

cacheMap.put(key,obj);

}

//获取缓存信息

publicstaticCachegetCacheInfo(Stringkey){

if(hasCache(key)){

Cachecache=getCache(key);

if(cacheExpired(cache)){//调用判断是否终止方法

cache.setExpired(true);

}

returncache;

}else

returnnull;

}

//载入缓存信息

publicstaticvoidputCacheInfo(Stringkey,Cacheobj,longdt,booleanexpired){

Cachecache=newCache();

cache.setKey(key);

cache.setTimeOut(dt+System.currentTimeMillis());//设置多久后更新缓存

cache.setValue(obj);

cache.setExpired(expired);//缓存默认载入时,终止状态为FALSE

cacheMap.put(key,cache);

}

//重写载入缓存信息方法

publicstaticvoidputCacheInfo(Stringkey,Cacheobj,longdt){

Cachecache=newCache();

cache.setKey(key);

cache.setTimeOut(dt+System.currentTimeMillis());

cache.setValue(obj);

cache.setExpired(false);

cacheMap.put(key,cache);

}

//判断缓存是否终止

publicstaticbooleancacheExpired(Cachecache){

if(null==cache){//传入的缓存不存在

returnfalse;

}

longnowDt=System.currentTimeMillis();//系统当前的毫秒数

longcacheDt=cache.getTimeOut();//缓存内的过期毫秒数

if(cacheDt<=0||cacheDt>nowDt){//过期时间小于等于零时,或者过期时间大于当前时间时,则为FALSE

returnfalse;

}else{//大于过期时间即过期

returntrue;

}

}

//获取缓存中的大小

publicstaticintgetCacheSize(){

returncacheMap.size();

}

//获取指定的类型的大小

publicstaticintgetCacheSize(Stringtype){

intk=0;

Iteratori=cacheMap.entrySet().iterator();

Stringkey;

try{

while(i.hasNext()){

java.util.Map.Entryentry=(java.util.Map.Entry)i.next();

key=(String)entry.getKey();

if(key.indexOf(type)!

=-1){//如果匹配则删除掉

k++;

}

}

}catch(Exceptionex){

ex.printStackTrace();

}

returnk;

}

//获取缓存对象中的所有键值名称

publicstaticArrayListgetCacheAllkey(){

ArrayLista=newArrayList();

try{

Iteratori=cacheMap.entrySet().iterator();

while(i.hasNext()){

java.util.Map.Entryentry=(java.util.Map.Entry)i.next();

a.add((String)entry.getKey());

}

}catch(Exceptionex){}finally{

returna;

}

}

//获取缓存对象中指定类型的键值名称

publicstaticArrayListgetCacheListkey(Stringtype){

ArrayLista=newArrayList();

Stringkey;

try{

Iteratori=cacheMap.entrySet().iterator();

while(i.hasNext()){

java.util.Map.Entryentry=(java.util.Map.Entry)i.next();

key=(String)entry.getKey();

if(key.indexOf(type)!

=-1){

a.add(key);

}

}

}catch(Exceptionex){}finally{

returna;

}

}

}

 

2007-09-29

简单LRU算法实现缓存

   最简单的LRU算法实现,就是利用jdk的LinkedHashMap,覆写其中的removeEldestEntry(Map.Entry)方法即可,如下所示:

java代码

 

1.import java.util.ArrayList;  

2.import java.util.Collection;  

3.import java.util.LinkedHashMap;  

4.import java.util.concurrent.locks.Lock;  

5.import java.util.concurrent.locks.ReentrantLock;  

6.import java.util.Map;  

7.  

8.  

9./** 

10. * 类说明:

利用LinkedHashMap实现简单的缓存, 必须实现removeEldestEntry方法,具体参见JDK文档 

11. *  

12. * @author dennis 

13. *  

14. * @param  

15. * @param  

16. */  

17.public class LRULinkedHashMap extends LinkedHashMap {  

18.    private final int maxCapacity;  

19.  

20.    private static final float DEFAULT_LOAD_FACTOR = 0.75f;  

21.  

22.    private final Lock lock = new ReentrantLock();  

23.  

24.    public LRULinkedHashMap(int maxCapacity) {  

25.        super(maxCapacity, DEFAULT_LOAD_FACTOR, true);  

26.        this.maxCapacity = maxCapacity;  

27.    }  

28.  

29.    @Override  

30.    protected boolean removeEldestEntry(java.util.Map.Entry eldest) {  

31.        return size() > maxCapacity;  

32.    }  

33.    @Override  

34.    public boolean containsKey(Object key) {  

35.        try {  

36.            lock.lock();  

37.            return super.containsKey(key);  

38.        } finally {  

39.            lock.unlock();  

40.        }  

41.    }  

42.  

43.      

44.    @Override  

45.    public V get(Object key) {  

46.        try {  

47.            lock.lock();  

48.            return super.get(key);  

49.        } finally {  

50.            lock.unlock();  

51.        }  

52.    }  

53.  

54.    @Override  

55.    public V put(K key, V value) {  

56.        try {  

57.            lock.lock();  

58.            return super.put(key, value);  

59.        } finally {  

60.            lock.unlock();  

61.        }  

62.    }  

63.  

64.    public int size() {  

65.        try {  

66.            lock.lock();  

67.            return super.size();  

68.        } finally {  

69.            lock.unlock();  

70.        }  

71.    }  

72.  

73.    public void clear() {  

74.        try {  

75.            lock.lock();  

76.            super.clear();  

77.        } finally {  

78.            lock.unlock();  

79.        }  

80.    }  

81.  

82.    public Collection> getAll() {  

83.        try {  

84.            lock.lock();  

85.            return new ArrayList>(super.entrySet());  

86.        } finally {  

87.            lock.unlock();  

88.        }  

89.    }  

90.}  

91.    

 如果你去看LinkedHashMap的源码可知,LRU算法是通过双向链表来实现,当某个位置被命中,通过调整链表的指向将该位置调整到头位置,新加入的内容直接放在链表头,如此一来,最近被命中的内容就向链表头移动,需要替换时,链表最后的位置就是最近最少使用的位置。

   LRU算法还可以通过计数来实现,缓存存储的位置附带一个计数器,当命中时将计数器加1,替换时就查找计数最小的位置并替换,结合访问时间戳来实现。

这种算法比较适合缓存数据量较小的场景,显然,遍历查找计数最小位置的时间复杂度为O(n)。

我实现了一个,结合了访问时间戳,当最小计数大于MINI_ACESS时,就移除最久没有被访问的项:

java代码

 

1.import java.io.Serializable;  

2.import java.util.ArrayList;  

3.import java.util.Collection;  

4.import java.util.HashMap;  

5.import java.util.Iterator;  

6.import java.util.Map;  

7.import java.util.Set;  

8.import java.util.concurrent.atomic.AtomicInteger;  

9.import java.util.concurrent.atomic.AtomicLong;  

10.import java.util.concurrent.locks.Lock;  

11.import java.util.concurrent.locks.ReentrantLock;  

12.  

13./** 

14. *  

15. * @author dennis  

16. * 类说明:

当缓存数目不多时,才用缓存计数的传统LRU算法 

17. * @param  

18. * @param  

19. */  

20.public class LRUCache implements Serializable {  

21.  

22.    private static final int DEFAULT_CAPACITY = 100;  

23.  

24.    protected Map

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1