java手写多级缓存

时间:2023-05-17

多级缓存完成类,时间有限,该类未抽取接口,目前只支持两级缓存:JVM缓存(完成请查看上一篇:java手写JVM高性能缓存)、redis缓存(在spring的redisTemplate根底完成)
复制代码
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);
}
}
/**2024澳门原料网1688白老虎
*提供数据,并缓存
*
*@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;
}
/**
*提供数据,并缓存一定的时间
*
*@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);
}
}
}
复制代码
redis缓存完成类
复制代码
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;
}
}

文章标签:

Copyright © 2016 2024澳门原料网1688白老虎,保留所有权利。 粤ICP备09033321号

2024澳门原料网1688白老虎 2024澳门原料网1688白老虎
2024澳门原料网1688白老虎
扫描二维码
与项目经理交流
2024澳门原料网1688白老虎 2024澳门原料网1688白老虎
2024澳门原料网1688白老虎
扫描二维码
与项目经理交流
2024澳门原料网1688白老虎
ciya68