java手写多级缓存

多级缓存完成类,时间有限,该类未抽取接口,目前只支持两级缓存: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);
}
}
/**

java手写多级缓存


*提供数据,并缓存
*
*@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;
}
}

© 版权声明
好牛新坐标
版权声明:
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com

相关文章