net.oschina.j2cache.cache.support.redis.SpringRedisCache Maven / Gradle / Ivy
package net.oschina.j2cache.cache.support.redis;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import net.oschina.j2cache.Level2Cache;
/**
* 重新实现二级缓存
*
* @author zhangsaizz
*
*/
public class SpringRedisCache implements Level2Cache {
private String namespace = "j2cache~key";
private String region;
private RedisTemplate redisTemplate;
public SpringRedisCache(String namespace, String region, RedisTemplate redisTemplate) {
if (region == null || region.isEmpty()) {
region = "_"; // 缺省region
}
if(!StringUtils.isEmpty(namespace)) {
this.namespace = namespace;
}
this.redisTemplate = redisTemplate;
this.region = getRegionName(region);
}
private String getRegionName(String region) {
if (namespace != null && !namespace.isEmpty())
region = namespace + ":" + region;
return region;
}
@Override
public void clear() {
redisTemplate.opsForHash().delete(region);
}
@Override
public Object get(String key) {
return redisTemplate.boundHashOps(region).get(key);
}
@Override
public boolean exists(String key) {
return redisTemplate.opsForHash().hasKey(region, key);
}
@Override
public void evict(String... keys) {
for (String k : keys) {
if (!k.equals("null")) {
redisTemplate.opsForHash().delete(region, k);
} else {
redisTemplate.delete(region);
}
}
}
@Override
public Collection keys() {
Set