mybatis二级缓存springboot-redis
2018-02-05 11:02
543 查看
注入类
@Configuration
public class RedisConfig {
@Value(value = "${mybatis_redis_cache}")
private int expire;
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
redisCacheTransfer(template);
return template;
}
private void redisCacheTransfer(RedisTemplate<String,Object> redisTemplate) {
RedisCache.setExpire(expire);
RedisCache.setRedisTemplate(redisTemplate);
}
}
缓存类
public final class RedisCache implements Cache {
private static final Logger log =
LoggerFactory.getLogger(RedisCache.class);
private String id;
private static RedisTemplate<String, Object> redisTemplate;
private static ValueOperations<String, Object> valueOperations;
private static int expire;
/**
* construct cache
* @param id cache id
*/
public RedisCache(String id) {
if (id == null) {
throw new IllegalArgumentException(
"Cache Instance ID Could Not Be Null");
}
log.info("Create Redis Cache [{}].", id);
this.id = id;
}
/**
* {@inheritDoc}
*/
public String getId() {
return id;
}
/**
* {@inheritDoc}
*/
public int getSize() {
log.debug("Get Cache [{}] Size.", id);
return redisTemplate.keys(prefixedKey("*")).size();
}
/**
* {@inheritDoc}
*/
public void putObject(final Object key, final Object value) {
log.debug("Put Object Key [{}], Value [{}].", key, value);
valueOperations.set(prefixedKey(key),
value, expire, TimeUnit.MILLISECONDS);
}
/**
* {@inheritDoc}
*/
public Object getObject(final Object key) {
Object value = valueOperations.get(prefixedKey(key));
log.debug("Get Object Key [{}], Value [{}].", key, value);
return value;
}
/**
* {@inheritDoc}
*/
public Object removeObject(final Object key) {
log.debug("Remove Object Key [{}].", key);
redisTemplate.delete(prefixedKey(key));
return 1;
}
/**
* {@inheritDoc}
*/
public void clear() {
log.debug("Clear Cache Key [{}].", id);
redisTemplate.delete(redisTemplate.keys(prefixedKey("*")));
}
/**
* {@inheritDoc}
*/
@Override
public ReadWriteLock getReadWriteLock() {
return null;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "Redis Cache [" + id + "]";
}
// --
// private methods
/**
* return prefixed cache key
<
93be
span style="white-space:pre;">* @param key cache key
* @return prefixed key
*/
String prefixedKey(Object key) {
return prefix() + String.valueOf(key);
}
/**
* @return cache prefix
*/
String prefix() {
return "mybatis_redis_cache:"+id + ":";
}
public static void setRedisTemplate(RedisTemplate<String,Object> redisTemplate) {
RedisCache.redisTemplate = redisTemplate;
valueOperations = redisTemplate.opsForValue();
}
public static void setExpire(int expire) {
RedisCache.expire=expire;
}
可参考
https://gitee.com/vmaps/dubbo-app/blob/master/dubbo-app-common/src/main/java/com/wangsong/common/config/RedisConfig.java https://gitee.com/vmaps/dubbo-app/blob/master/dubbo-app-common/src/main/java/com/wangsong/common/mybatis/RedisCache.java
@Configuration
public class RedisConfig {
@Value(value = "${mybatis_redis_cache}")
private int expire;
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
redisCacheTransfer(template);
return template;
}
private void redisCacheTransfer(RedisTemplate<String,Object> redisTemplate) {
RedisCache.setExpire(expire);
RedisCache.setRedisTemplate(redisTemplate);
}
}
缓存类
public final class RedisCache implements Cache {
private static final Logger log =
LoggerFactory.getLogger(RedisCache.class);
private String id;
private static RedisTemplate<String, Object> redisTemplate;
private static ValueOperations<String, Object> valueOperations;
private static int expire;
/**
* construct cache
* @param id cache id
*/
public RedisCache(String id) {
if (id == null) {
throw new IllegalArgumentException(
"Cache Instance ID Could Not Be Null");
}
log.info("Create Redis Cache [{}].", id);
this.id = id;
}
/**
* {@inheritDoc}
*/
public String getId() {
return id;
}
/**
* {@inheritDoc}
*/
public int getSize() {
log.debug("Get Cache [{}] Size.", id);
return redisTemplate.keys(prefixedKey("*")).size();
}
/**
* {@inheritDoc}
*/
public void putObject(final Object key, final Object value) {
log.debug("Put Object Key [{}], Value [{}].", key, value);
valueOperations.set(prefixedKey(key),
value, expire, TimeUnit.MILLISECONDS);
}
/**
* {@inheritDoc}
*/
public Object getObject(final Object key) {
Object value = valueOperations.get(prefixedKey(key));
log.debug("Get Object Key [{}], Value [{}].", key, value);
return value;
}
/**
* {@inheritDoc}
*/
public Object removeObject(final Object key) {
log.debug("Remove Object Key [{}].", key);
redisTemplate.delete(prefixedKey(key));
return 1;
}
/**
* {@inheritDoc}
*/
public void clear() {
log.debug("Clear Cache Key [{}].", id);
redisTemplate.delete(redisTemplate.keys(prefixedKey("*")));
}
/**
* {@inheritDoc}
*/
@Override
public ReadWriteLock getReadWriteLock() {
return null;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "Redis Cache [" + id + "]";
}
// --
// private methods
/**
* return prefixed cache key
<
93be
span style="white-space:pre;">* @param key cache key
* @return prefixed key
*/
String prefixedKey(Object key) {
return prefix() + String.valueOf(key);
}
/**
* @return cache prefix
*/
String prefix() {
return "mybatis_redis_cache:"+id + ":";
}
public static void setRedisTemplate(RedisTemplate<String,Object> redisTemplate) {
RedisCache.redisTemplate = redisTemplate;
valueOperations = redisTemplate.opsForValue();
}
public static void setExpire(int expire) {
RedisCache.expire=expire;
}
可参考
https://gitee.com/vmaps/dubbo-app/blob/master/dubbo-app-common/src/main/java/com/wangsong/common/config/RedisConfig.java https://gitee.com/vmaps/dubbo-app/blob/master/dubbo-app-common/src/main/java/com/wangsong/common/mybatis/RedisCache.java
相关文章推荐
- springboot+mybatis+redis 二级缓存问题实例详解
- spring boot + mysql +mybatis +redis(二级缓存)实例
- SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存
- 详解Spring boot使用Redis集群替换mybatis二级缓存
- Spring Boot + Mybatis + 二级缓存实例(Ehcache,Redis)
- springboot mybatis redis 二级缓存
- SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
- Spring Boot + Mybatis + Ehcache 二级缓存实例
- SpringBoot之Mybatis操作中使用Redis做缓存
- [置顶] 【二】Springboot+Mybatis+Redis实现用户信息查询缓存
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
- spring-boot整合redis作为mysql二级缓存
- springBoot中配置mybatis的二级缓存
- Spring Boot 整合 MyBatis 并启用二级缓存
- Redis集成到Spring做mybatis做二级缓存
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十五)使用Redis做集中式缓存(1)
- Redis集成到Spring做mybatis做二级缓存