12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
建站问答 时间:2024-12-01 19:15:37
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
开发redis二级缓存的示例:redis二级缓存的实现,主要是重写了Cache.java的方法,代码:publicclassimplementsCache{privatestaticfinalLoggerlogger=LoggerFactory.getLogger(Myba
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
开发redis二级缓存的示例:
redis二级缓存的实现,主要是重写了Cache.java的方法,代码:
publicclassimplementsCache{
privatestaticfinalLoggerlogger=LoggerFactory.getLogger(MybatisRedisCache.class);
privatefinalReadWriteLockreadWriteLock=newReentrantReadWriteLock();
privateRedisTemplateredisTemplate;
privateStringid;
privatestaticfinallongEXPIRE_TIME_IN_MINUTES=30;
public(Stringid){
if(id==null){
thrownewIllegalArgumentException("CacheinstancesrequireanID");
}
logger.info("=====================================Rediscacheid="+id);
this.id=id;
}
publicStringgetId(){
returnid;
}
publicvoidputObject(Objectkey,Objectvalue){
logger.debug("==============================redisput="+key);
RedisTemplateredisTemplate=getRedisTemplate();
ValueOperationsopsForValue=redisTemplate.opsForValue();
opsForValue.set(key,value,EXPIRE_TIME_IN_MINUTES,TimeUnit.MINUTES);
}
publicObjectgetObject(Objectkey){
logger.debug("================================redisget================================");
RedisTemplateredisTemplate=getRedisTemplate();
ValueOperationsopsForValue=redisTemplate.opsForValue();
returnopsForValue.get(key);
}
publicObjectremoveObject(Objectkey){
logger.debug("==========================================redisremove==========================");
RedisTemplateredisTemplate=getRedisTemplate();
redisTemplate.delete(key);
returnnull;
}
publicvoidclear(){
logger.debug("=====================================clearredis================================");
RedisTemplateredisTemplate=getRedisTemplate();
redisTemplate.execute(newRedisCallback(){
@Override
publicObjectdoInRedis(RedisConnectionconnection)throwsDataAccessException{
大专栏redis实现二级缓存
connection.flushDb();
return"OK";
}
});
}
@Override
publicintgetSize(){
return0;
}
@Override
publicReadWriteLockgetReadWriteLock(){
returnreadWriteLock;
}
publicRedisTemplategetRedisTemplate(){
if(redisTemplate==null){
redisTemplate=ApplicationContextHolder.getBean("redisTemplate");
}
returnredisTemplate;
}
publicvoidsetRedisTemplate(RedisTemplateredisTemplate){
this.redisTemplate=redisTemplate;
}
}
使用redisTemplate方法实现一个ApplicationContextHolder工具类,代码:
@Component
publicclassApplicationContextHolderimplementsApplicationContextAware{
privatestaticApplicationContextapplicationContext;
@Override
publicvoidsetApplicationContext(ApplicationContextctx)throwsBeansException{
applicationContext=ctx;
}
/**
*Getapplicationcontextfromeverywhere
*
*@return
*/
publicstaticApplicationContextgetApplicationContext(){
returnapplicationContext;
}
/**
*Getbeanbyclass
*
*@paramclazz
*@param<T>
*@return
*/
publicstatic<T>TgetBean(Class<T>clazz){
returnapplicationContext.getBean(clazz);
}
/**
*Getbeanbyclassname
*
*@paramname
*@param<T>
*@return
*/
@SuppressWarnings("unchecked")
publicstatic<T>TgetBean(Stringname){
return(T)applicationContext.getBean(name);
}
}
在mapper中添加自定义cache,使用二级缓存,例如:
<cachetype="com.yif.utils.MybatisRedisCache"eviction="LRU"/>
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19