您的位置:首页 > 数据库 > Redis

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: