開發redis二級緩存的示例:
redis二級緩存的實現,主要是重寫了Cache.java的方法,代碼:
public class implements Cache {
private static final Logger logger = LoggerFactory.getLogger(MybatisRedisCache.class);
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private RedisTemplate redisTemplate;
private String id;
private static final long EXPIRE_TIME_IN_MINUTES = 30;
public (String id){
if (id==null){
throw new IllegalArgumentException("Cache instances require an ID");
}
logger.info("=====================================Redis cache id = "+id);
this.id = id;
}
public String getId() {
return id;
}
public void putObject(Object key, Object value) {
logger.debug("==============================redis put= "+key);
RedisTemplate redisTemplate = getRedisTemplate();
ValueOperations opsForValue = redisTemplate.opsForValue();
opsForValue.set(key, value, EXPIRE_TIME_IN_MINUTES, TimeUnit.MINUTES);
}
public Object getObject(Object key) {
logger.debug("================================redis get================================");
RedisTemplate redisTemplate = getRedisTemplate();
ValueOperations opsForValue = redisTemplate.opsForValue();
return opsForValue.get(key);
}
public Object removeObject(Object key) {
logger.debug("==========================================redis remove==========================");
RedisTemplate redisTemplate = getRedisTemplate();
redisTemplate.delete(key);
return null;
}
public void clear() {
logger.debug("=====================================clear redis================================");
RedisTemplate redisTemplate = getRedisTemplate();
redisTemplate.execute(new RedisCallback() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
大專欄 redis實現二級緩存
connection.flushDb();
return "OK";
}
});
}
@Override
public int getSize() {
return 0;
}
@Override
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
public RedisTemplate getRedisTemplate() {
if (redisTemplate == null) {
redisTemplate = ApplicationContextHolder.getBean("redisTemplate");
}
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
使用redisTemplate方法實現一個ApplicationContextHolder工具類,代碼:
@Component
public class ApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
applicationContext = ctx;
}
/**
* Get application context from everywhere
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* Get bean by class
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
/**
* Get bean by class name
*
* @param name
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
return (T) applicationContext.getBean(name);
}
}
在mapper中添加自定義cache,使用二級緩存,例如:
<cache type="com.yif.utils.MybatisRedisCache" eviction="LRU"/>