多級緩存完成類,時間有限,該類未抽取接口,目前只支持兩級緩存:JVM緩存(完成請查看上一篇:java手寫JVM高性能緩存)、redis緩存(在spring的redisTemplate根底完成)
復(fù)制代碼
packagecom.ws.commons.cache;
importjava.util.function.Supplier;
importcom.ws.commons.tool.ThreadTool;
/**
*多級緩存完成
*
*@author塵無塵
*
*/
publicclassMultilevelCache{
privateMultilevelCache(){
}
privatestaticfinalICacheFIRST_LEVE_LCACHE=LocalCache.instance();
privatestaticICachesecondCache;
privatestaticfinalStringLOCK_PREFIX=”MUILCACHE_LOCK:”;
publicstaticsynchronizedvoidinit(ICachesecondCache){
if(MultilevelCache.secondCache==null){
MultilevelCache.secondCache=secondCache;
}
}
publicstaticvoidput(Stringkey,Objectvalue,inttimeOutSecond){
FIRST_LEVE_LCACHE.put(key,value,timeOutSecond);
if(secondCache!=null){
secondCache.put(key,value,timeOutSecond);
}
}
/**
*提供數(shù)據(jù),并緩存
*
*@paramkey
*@paramsupplier
*@return
*/
@SuppressWarnings(“unchecked”)
publicstaticTgetForload(Stringkey,Suppliersupplier){
Tdata=FIRST_LEVE_LCACHE.get(key);
if(data==null&&secondCache!=null){
data=(T)secondCache.get(key);
}
if(data!=null){
returndata;
}
synchronized(ThreadTool.buildLock(LOCK_PREFIX,key)){
data=FIRST_LEVE_LCACHE.get(key);
if(data==null&&secondCache!=null){
data=(T)secondCache.get(key);
}
if(data!=null){
returndata;
}
data=supplier.get();
if(secondCache!=null){
secondCache.put(key,data);
}
FIRST_LEVE_LCACHE.put(key,data);
}
returndata;
}
/**
*提供數(shù)據(jù),并緩存一定的時間
*
*@paramkey
*@paramtimeOutSecond
*@paramsupplier
*@return
*/
@SuppressWarnings(“unchecked”)
publicstaticTgetForload(Stringkey,inttimeOutSecond,Suppliersupplier){
Tdata=FIRST_LEVE_LCACHE.get(key);
if(data==null&&secondCache!=null){
data=(T)secondCache.get(key);
}
if(data!=null){
returndata;
}
synchronized(ThreadTool.buildLock(LOCK_PREFIX,key)){
data=FIRST_LEVE_LCACHE.get(key);
if(data==null&&secondCache!=null){
data=(T)secondCache.get(key);
}
if(data!=null){
returndata;
}
data=supplier.get();
if(secondCache!=null){
secondCache.put(key,data,timeOutSecond);
}
FIRST_LEVE_LCACHE.put(key,data,timeOutSecond);
}
returndata;
}
@SuppressWarnings(“unchecked”)
publicstaticTremoveAndGet(Stringkey){
Tdata=null;
if(secondCache!=null){
data=(T)secondCache.removeAndGet(key);
}
Tdata2=FIRST_LEVE_LCACHE.removeAndGet(key);
if(data==null){
data=data2;
}
returndata;
}
publicstaticvoidremove(Stringkey){
if(secondCache!=null){
secondCache.remove(key);
}
FIRST_LEVE_LCACHE.remove(key);
}
@SuppressWarnings(“unchecked”)
publicstaticTget(Stringkey){
Tdata=FIRST_LEVE_LCACHE.get(key);
if(data==null&&secondCache!=null){
data=(T)secondCache.get(key);
}
returndata;
}
publicstaticvoidexpire(Stringkey,inttimeOutSecond){
FIRST_LEVE_LCACHE.expire(key,timeOutSecond);
if(secondCache!=null){
secondCache.expire(key,timeOutSecond);
}
}
}
復(fù)制代碼
redis緩存完成類
復(fù)制代碼
packagecom.walmart.cirular.interfaces.application;
importjava.util.concurrent.TimeUnit;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.data.redis.core.RedisTemplate;
importorg.springframework.stereotype.Component;
importcom.ws.commons.cache.ICache;
/**
*redis緩存完成類
*
*@author塵無塵
*
*/
@Component
publicclassRedisICacheImplimplementsICache{
@Autowired(required=false)
privateRedisTemplate<String,Object>redisTemplate;
@Override
publicvoidput(Stringkey,Objectvalue){
redisTemplate.opsForValue().set(key,value);
}
@Override
publicvoidput(Stringkey,Objectvalue,inttimeOutSecond){
redisTemplate.opsForValue().set(key,value,(long)timeOutSecond,TimeUnit.SECONDS);
}
@SuppressWarnings(“unchecked”)
@Override
publicTget(Stringkey){
Tt=(T)redisTemplate.opsForValue().get(key);
redisTemplate.delete(key);
returnt;
}
@Override
publicvoidremove(Stringkey){
redisTemplate.delete(key);
}
@SuppressWarnings(“unchecked”)
@Override
publicTremoveAndGet(Stringkey){
Objectobj=get(key);
redisTemplate.delete(key);
return(T)obj;
}
@Override
publicvoidrightPush(Stringkey,Objectvalue,inttimeOutSecond){
redisTemplate.opsForList().rightPush(key,value);
redisTemplate.expire(key,timeOutSecond,TimeUnit.SECONDS);
}
@SuppressWarnings(“unchecked”)
@Override
publicTrightPop(Stringkey){
return(T)redisTemplate.opsForList().rightPop(key);
}
@SuppressWarnings(“unchecked”)
@Override
publicTleftPop(Stringkey){
return(T)redisTemplate.opsForList().leftPop(key);
}
@Override
publicvoidleftPush(Stringkey,Objectvalue){
redisTemplate.opsForList().leftPush(key,value);
}
@Override
publicvoidrightPush(Stringkey,Objectvalue){
redisTemplate.opsForList().rightPush(key,value);
}
@Override
publicvoidexpire(Stringkey,inttimeOutSecond){
redisTemplate.expire(key,timeOutSecond,TimeUnit.SECONDS);
}
@Override
publicbooleanhasKey(Stringkey){
returnredisTemplate.hasKey(key);
}
@Override
publicbooleanputIfAbsent(Stringkey,Objectvalue){
returnredisTemplate.opsForValue().setIfAbsent(key,value);
}
@Override
publicbooleanputIfAbsent(Stringkey,Objectvalue,inttimeOutSecond){
booleanflag=putIfAbsent(key,value);
if(flag){
expire(key,timeOutSecond);
}
returnflag;
}
}
廣州天河區(qū)珠江新城富力盈力大廈北塔2706
020-38013166(網(wǎng)站咨詢專線)
400-001-5281 (售后服務(wù)熱線)
深圳市坂田十二橡樹莊園F1-7棟
Site/ http://www.szciya.com
E-mail/ itciya@vip.163.com
品牌服務(wù)專線:400-001-5281
長沙市天心區(qū)芙蓉中路三段398號新時空大廈5樓
聯(lián)系電話/ (+86 0731)88282200
品牌服務(wù)專線/ 400-966-8830
旗下運營網(wǎng)站:
Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權(quán)利。 粵ICP備09033321號