Java中常用缓存Cache机制的实现
缓存,就是将程序或系统经常要调用的对象存在内存中,一遍其使用时可以快速调用,不必再去创建新的重复的实例。这样做可以减少系统开销,提高系统效率。
缓存主要可分为二大类:
一、通过文件缓存,顾名思义文件缓存是指把数据存储在磁盘上,不管你是以XML格式,序列化文件DAT格式还是其它文件格式;
二、内存缓存,也就是实现一个类中静态Map,对这个Map进行常规的增删查.
importjava.util.*;
//Description:管理缓存
//可扩展的功能:当chche到内存溢出时必须清除掉最早期的一些缓存对象,这就要求对每个缓存对象保存创建时间
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<arr.size();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;
}
}
}
packagelhm.hcy.guge.frameset.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();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。