com.qingzhuge.framework.cache.lettuce.LettuceProvider Maven / Gradle / Ivy
package com.qingzhuge.framework.cache.lettuce;
import com.qingzhuge.framework.base.cache.support.CacheObject;
import com.qingzhuge.framework.cache.Cache;
import com.qingzhuge.framework.cache.CacheChannel;
import com.qingzhuge.framework.cache.CacheExpiredListener;
import com.qingzhuge.framework.cache.Level2Cache;
import com.qingzhuge.framework.cache.autoconfigure.J2CacheProperties;
import com.qingzhuge.framework.cache.provider.CacheProvider;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author : zero.xiao
* datetime :2018-06-03 20:45
* 二级缓存,使用 Lettuce 进行 Redis 的操作
*/
public class LettuceProvider implements CacheProvider {
@Resource
private RedisTemplate redisTemplate;
private String namespace;
private final ConcurrentHashMap regions = new ConcurrentHashMap<>();
@Override
public String name() {
return "lettuce";
}
@Override
public int level() {
return CacheObject.LEVEL_2;
}
@Override
public Cache buildCache(String region, CacheExpiredListener listener) {
return regions.computeIfAbsent(this.namespace + ":" + region, v -> new LettuceCache(this.namespace, region, redisTemplate));
}
@Override
public Cache buildCache(String region, long timeToLiveInSeconds, CacheExpiredListener listener) {
return buildCache(region, listener);
}
@Override
public void start(J2CacheProperties config) {
this.namespace = config.getNamespace();
}
@Override
public void stop() {
regions.clear();
}
@Override
public Collection regions() {
return Collections.emptyList();
}
}