外国vps开发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”/>
一诺网络提供的外国VPS,是一款弹性计算服务类产品,其功能基本上与传统的x86物理服务器相当,具备分钟级交付效率,可快速部署和创建所需要的业务应用环境,服务器资源和配置支持弹性伸缩;拥有管理便捷、易于操作等诸多优点。可以极大降低用户的IT使用成本,用户无需亲自搭建基础设施、简化了运维和管理的日常工作量,使得用户能够更专注于自身的业务发展和创新。