最近使用Spring cache,发现使用默认生成的key策略只使用了方法入参作为key,很不灵活,用到真实的项目中也不太靠谱,于是自己实现它的key生成策略。
参考官方文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html
spring 默认的key生成实现是
org.springframework.cache.interceptor.SimpleKeyGenerator
感兴趣的同学自己看下,具体我就不描述了。
自定义生成策略需要实现下面的接口: 接口对应方法参数,需要调用的目标对象实例,目标方法,目标方法入参
public interface KeyGenerator {
/**
* Generate a key for the given method and its parameters.
* @param target the target instance
* @param method the method being called
* @param params the method parameters (with any var-args expanded)
* @return a generated key
*/
Object generate(Object target, Method method, Object... params);
}
下面展示我实现的方式
import java.io.Serializable;
import java.lang.reflect.Method;
import org.springframework.cache.interceptor.KeyGenerator;
/**
* 实现spring cache的默认缓存实现策略
* @author lisuo
*
*/
public class DefaultKeyGenerator implements Serializable,KeyGenerator {
/**
*
*/
private static final long serialVersionUID = 5944569667769217577L;
@Override
public Object generate(Object target, Method method, Object... params) {
return new DefaultKey(target, method, params);
}
}
这里必须要知道的是,spring cache在判断是否命中数据时,是基于底层的缓存实现判断是否命中key的。
下面结合redis展示我的实现:
import java.io.Serializable;
import java.lang.reflect.Method;
import org.apache.commons.lang3.ArrayUtils;
/**
* Spring cache key 生成策略
* @author lisuo
*/
public class DefaultKey implements Serializable{
private static final long serialVersionUID = -8536541050699277557L;
/** 调用目标对象全类名 */
protected String targetClassName;
/** 调用目标方法名称 */
protected String methodName;
/** 调用目标参数 */
protected Object[] params;
public DefaultKey(Object target, Method method, Object[] elements) {
this.targetClassName = target.getClass().getName();
this.methodName = generatorMethodName(method);
if(ArrayUtils.isNotEmpty(elements)){
this.params = new Object[elements.length];
for(int i =0;i<elements.length;i++){
Object ele = elements[i];
this.params[i] = ele;
}
}
}
private String generatorMethodName(Method method){
StringBuilder builder = new StringBuilder(method.getName());
Class<?>[] types = method.getParameterTypes();
if(ArrayUtils.isNotEmpty(types)){
builder.append("(");
for(Class<?> type:types){
String name = type.getName();
builder.append(name+",");
}
builder.delete(builder.length()-1, builder.length());
builder.append(")");
}
return builder.toString();
}
}
上面只是缓存应用的实现,具体我们看下Redis底层缓存管理器的实现
首先是CacheManager
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.support.AbstractCacheManager;
import org.springframework.util.Assert;
import com.study.demo.util.RedisUtil;
/**
* Redis缓存管理器,实现Spring Cache接口
* @author lisuo
*
*/
public class RedisCacheManager extends AbstractCacheManager {
private RedisUtil rcache;
//缓存前缀
private String cachePrefix;
//储存缓存名称的set集合key名称
private String cacheNameSet;
public RedisCacheManager(RedisUtil rcache,String cachePrefix,String cacheNameSet) {
Assert.notNull(rcache, "Cache must not be null");
Assert.notNull(cachePrefix, "CachePrefix must not be null");
Assert.notNull(cacheNameSet, "CacheNameSet must not be null");
this.rcache = rcache;
this.cachePrefix = cachePrefix;
this.cacheNameSet = cacheNameSet;
}
@Override
protected Collection<? extends Cache> loadCaches() {
List<Cache> caches = new ArrayList<>();
Set<String> names = rcache.smembers(cacheNameSet);
if(CollectionUtils.isNotEmpty(names)){
for (String name:names) {
RedisCache cache = new RedisCache(cachePrefix+name, rcache);
caches.add(cache);
}
}
return caches;
}
@Override
protected Cache getMissingCache(String name) {
String cacheName = cachePrefix+name;
rcache.sadd(cacheNameSet, cacheName);
RedisCache cache = new RedisCache(cacheName, rcache);
return cache;
}
}
Cache的实现
import java.util.concurrent.Callable;
import org.springframework.cache.support.AbstractValueAdaptingCache;
import org.springframework.util.Assert;
import com.study.demo.util.RedisUtil;
/**
* 缓存实现
* 这个结构的缓存是不支持数据过期的
* @author lisuo
*
*/
public class RedisCache extends AbstractValueAdaptingCache{
private final String name;
private RedisUtil rcache;
public RedisCache(String name,RedisUtil rcache) {
super(true);
Assert.notNull(name, "Name must not be null");
Assert.notNull(rcache, "Cache must not be null");
this.name = name;
this.rcache = rcache;
}
@Override
public final String getName() {
return this.name;
}
@Override
public Object getNativeCache() {
return this.rcache;
}
@Override
public ValueWrapper get(Object key) {
String v = rcache.hget(name, ObjStringConverUtil.objToString(key));
if(v!=null){
Object value = ObjStringConverUtil.stringToObj(v);
ValueWrapper valueWrapper = toValueWrapper(value);
return valueWrapper;
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public <T> T get(Object key, Callable<T> valueLoader) {
Object v = this.lookup(key);
if(v!=null){
return (T) v;
}
else {
T value;
try {
value = valueLoader.call();
}
catch (Exception ex) {
throw new ValueRetrievalException(key, valueLoader, ex);
}
put(key, value);
return value;
}
}
@Override
public void put(Object key, Object value) {
rcache.hset(name, ObjStringConverUtil.objToString(key), ObjStringConverUtil.objToString(value));
}
@Override
public ValueWrapper putIfAbsent(Object key, Object value) {
String k = ObjStringConverUtil.objToString(key);
Boolean hexists = rcache.hexists(name, k);
if(hexists){
String v = rcache.hget(name, k);
if(v!=null){
ValueWrapper valueWrapper = toValueWrapper(ObjStringConverUtil.stringToObj(v));
return valueWrapper;
}
}
String val = ObjStringConverUtil.objToString(value);
rcache.hset(name, k, val);
return toValueWrapper(value);
}
@Override
public void evict(Object key) {
String k = ObjStringConverUtil.objToString(key);
rcache.hdel(name, k);
}
@Override
public void clear() {
rcache.del(name);
}
@Override
protected Object lookup(Object key) {
String v = rcache.hget(name, ObjStringConverUtil.objToString(key));
if(v!=null){
Object value = ObjStringConverUtil.stringToObj(v);
return value;
}
return null;
}
}
对象转换字符工具类
import java.io.Serializable;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.SerializationUtils;
/**
* 对象字符串互转工具类
* 序列化+Base64实现
* @author lisuo
*
*/
public class ObjStringConverUtil {
/**
* 对象转字符串
* @param obj
* @return
*/
public static String objToString(Object obj){
byte[] bs = SerializationUtils.serialize((Serializable) obj);
return Base64.encodeBase64String(bs);
}
/**
* 字符串转对象
* @param str
* @return
*/
public static Object stringToObj(String str){
byte[] bs = Base64.decodeBase64(str);
Object obj = SerializationUtils.deserialize(bs);
return obj;
}
}
Redis操作工具类
关于Redis的工具类代码比较多
具体使用介绍参看:https://my.oschina.net/lis1314/blog/881704
package com.study.demo.util;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.BitPosParams;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisCommands;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.params.geo.GeoRadiusParam;
import redis.clients.jedis.params.sortedset.ZAddParams;
import redis.clients.jedis.params.sortedset.ZIncrByParams;
/**
* Jedis 操作工具类
*
* @version 基于Jedis 2.9.0
* @author lisuo
*
*/
public class RedisUtil implements JedisCommands {
// jedis连接池
private JedisPool jedisPool;
// jedis集群
private JedisCluster jedisCluster;
// 是否为集群,默认不是集群
private boolean isCluster = false;
/**
* 连接池构建方式
* @param jedisPool jedis连接池
*/
public RedisUtil(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
/**
* 集群构建方式
* @param jedisCluster jedis集群实例
*/
public RedisUtil(JedisCluster jedisCluster) {
this.jedisCluster = jedisCluster;
isCluster = true;
}
// -----------------------------------实现脚本命令-----------------------------------
public Object eval(String script, int keyCount, String... params) {
if (isCluster) {
return jedisCluster.eval(script, keyCount, params);
} else {
Jedis jedis = jedisPool.getResource();
try {
return jedis.eval(script, keyCount, params);
} finally {
jedis.close();
}
}
}
public Object eval(String script, List<String> keys, List<String> args) {
if (isCluster) {
return jedisCluster.eval(script, keys, args);
} else {
Jedis jedis = jedisPool.getResource();
try {
return jedis.eval(script, keys, args);
} finally {
jedis.close();
}
}
}
public Object evalsha(String sha1, int keyCount, String... params) {
if (isCluster) {
return jedisCluster.evalsha(sha1, keyCount, params);
} else {
Jedis jedis = jedisPool.getResource();
try {
return jedis.evalsha(sha1, keyCount, params);
} finally {
jedis.close();
}
}
}
/**
* 获取JedisCommands实例
* @return JedisCommands
*/
private JedisCommands getJedisCommands() {
if (isCluster) {
return jedisCluster;
} else {
return jedisPool.getResource();
}
}
/**
* Callback 回调接口
* @param <T>
*/
public interface Callback<T> {
/**
* 回调函数
* @param commands
* @return
*/
public T call(JedisCommands commands);
}
/**
* 执行Redis 命令
* @param callback 回调接口
* @return
*/
public <T> T execute(Callback<T> callback) {
JedisCommands jedisCommands = getJedisCommands();
try {
return callback.call(jedisCommands);
} finally {
// 非集群下释放资源,集群源码中已实现释放资源
if (!isCluster) {
((Jedis) jedisCommands).close();
}
}
}
// 实现JedisCommands,关于@Deprecated标记的方法参看Jedis API,如果报错可能是版本过高,一些方法被废除
@Override
public String get(final String key) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.get(key);
}
});
}
@Override
public String set(final String key, final String value) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.set(key, value);
}
});
}
@Override
public Long sadd(final String key, final String... member) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.sadd(key, member);
}
});
}
@Override
public Long srem(final String key, final String... member) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.srem(key, member);
}
});
}
@Override
public Boolean sismember(final String key, final String member) {
return execute(new Callback<Boolean>() {
@Override
public Boolean call(JedisCommands commands) {
return commands.sismember(key, member);
}
});
}
@Override
public String hget(final String key, final String field) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.hget(key, field);
}
});
}
@Override
public String hmset(final String key, final Map<String, String> hash) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.hmset(key, hash);
}
});
}
@Override
public Long hset(final String key, final String field, final String value) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.hset(key, field, value);
}
});
}
@Override
public Long lpush(final String key, final String... string) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.lpush(key, string);
}
});
}
@Override
public String lpop(final String key) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.lpop(key);
}
});
}
@Override
public String set(final String key, final String value, final String nxxx, final String expx, final long time) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.set(key, value, nxxx, expx, time);
}
});
}
@Override
public String set(final String key, final String value, final String nxxx) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.set(key, value, nxxx);
}
});
}
@Override
public Boolean exists(final String key) {
return execute(new Callback<Boolean>() {
@Override
public Boolean call(JedisCommands commands) {
return commands.exists(key);
}
});
}
@Override
public Long persist(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.persist(key);
}
});
}
@Override
public String type(final String key) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.type(key);
}
});
}
@Override
public Long expire(final String key, final int seconds) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.expire(key, seconds);
}
});
}
@Override
public Long pexpire(final String key, final long milliseconds) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.pexpire(key, milliseconds);
}
});
}
@Override
public Long expireAt(final String key, final long unixTime) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.expireAt(key, unixTime);
}
});
}
@Override
public Long pexpireAt(final String key, final long millisecondsTimestamp) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.pexpireAt(key, millisecondsTimestamp);
}
});
}
@Override
public Long ttl(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.ttl(key);
}
});
}
@Override
public Long pttl(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.pttl(key);
}
});
}
@Override
public Boolean setbit(final String key, final long offset, final boolean value) {
return execute(new Callback<Boolean>() {
@Override
public Boolean call(JedisCommands commands) {
return commands.setbit(key, offset, value);
}
});
}
@Override
public Boolean setbit(final String key, final long offset, final String value) {
return execute(new Callback<Boolean>() {
@Override
public Boolean call(JedisCommands commands) {
return commands.setbit(key, offset, value);
}
});
}
@Override
public Boolean getbit(final String key, final long offset) {
return execute(new Callback<Boolean>() {
@Override
public Boolean call(JedisCommands commands) {
return commands.getbit(key, offset);
}
});
}
@Override
public Long setrange(final String key, final long offset, final String value) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.setrange(key, offset, value);
}
});
}
@Override
public String getrange(final String key, final long startOffset, final long endOffset) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.getrange(key, startOffset, endOffset);
}
});
}
@Override
public String getSet(final String key, final String value) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.getSet(key, value);
}
});
}
@Override
public Long setnx(final String key, final String value) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.setnx(key, value);
}
});
}
@Override
public String setex(final String key, final int seconds, final String value) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.setex(key, seconds, value);
}
});
}
@Override
public String psetex(final String key, final long milliseconds, final String value) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.psetex(key, milliseconds, value);
}
});
}
@Override
public Long decrBy(final String key, final long integer) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.decrBy(key, integer);
}
});
}
@Override
public Long decr(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.decr(key);
}
});
}
@Override
public Long incrBy(final String key, final long integer) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.incrBy(key, integer);
}
});
}
@Override
public Double incrByFloat(final String key, final double value) {
return execute(new Callback<Double>() {
@Override
public Double call(JedisCommands commands) {
return commands.incrByFloat(key, value);
}
});
}
@Override
public Long incr(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.incr(key);
}
});
}
@Override
public Long append(final String key, final String value) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.append(key, value);
}
});
}
@Override
public String substr(final String key, final int start, final int end) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.substr(key, start, end);
}
});
}
@Override
public Long hsetnx(final String key, final String field, final String value) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.hsetnx(key, field, value);
}
});
}
@Override
public List<String> hmget(final String key, final String... fields) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.hmget(key, fields);
}
});
}
@Override
public Long hincrBy(final String key, final String field, final long value) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.hincrBy(key, field, value);
}
});
}
@Override
public Double hincrByFloat(final String key, final String field, final double value) {
return execute(new Callback<Double>() {
@Override
public Double call(JedisCommands commands) {
return commands.hincrByFloat(key, field, value);
}
});
}
@Override
public Boolean hexists(final String key, final String field) {
return execute(new Callback<Boolean>() {
@Override
public Boolean call(JedisCommands commands) {
return commands.hexists(key, field);
}
});
}
@Override
public Long hdel(final String key, final String... field) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.hdel(key, field);
}
});
}
@Override
public Long hlen(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.hlen(key);
}
});
}
@Override
public Set<String> hkeys(final String key) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.hkeys(key);
}
});
}
@Override
public List<String> hvals(final String key) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.hvals(key);
}
});
}
@Override
public Map<String, String> hgetAll(final String key) {
return execute(new Callback<Map<String, String>>() {
@Override
public Map<String, String> call(JedisCommands commands) {
return commands.hgetAll(key);
}
});
}
@Override
public Long rpush(final String key, final String... string) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.rpush(key, string);
}
});
}
@Override
public Long llen(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.llen(key);
}
});
}
@Override
public List<String> lrange(final String key, final long start, final long end) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.lrange(key, start, end);
}
});
}
@Override
public String ltrim(final String key, final long start, final long end) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.ltrim(key, start, end);
}
});
}
@Override
public String lindex(final String key, final long index) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.lindex(key, index);
}
});
}
@Override
public String lset(final String key, final long index, final String value) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.lset(key, index, value);
}
});
}
@Override
public Long lrem(final String key, final long count, final String value) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.lrem(key, count, value);
}
});
}
@Override
public String rpop(final String key) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.rpop(key);
}
});
}
@Override
public Set<String> smembers(final String key) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.smembers(key);
}
});
}
@Override
public String spop(final String key) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.spop(key);
}
});
}
@Override
public Set<String> spop(final String key, final long count) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.spop(key, count);
}
});
}
@Override
public Long scard(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.scard(key);
}
});
}
@Override
public String srandmember(final String key) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.srandmember(key);
}
});
}
@Override
public List<String> srandmember(final String key, final int count) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.srandmember(key, count);
}
});
}
@Override
public Long strlen(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.strlen(key);
}
});
}
@Override
public Long zadd(final String key, final double score, final String member) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zadd(key, score, member);
}
});
}
@Override
public Long zadd(final String key, final double score, final String member, final ZAddParams params) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zadd(key, score, member, params);
}
});
}
@Override
public Long zadd(final String key, final Map<String, Double> scoreMembers) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zadd(key, scoreMembers);
}
});
}
@Override
public Long zadd(final String key, final Map<String, Double> scoreMembers, final ZAddParams params) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zadd(key, scoreMembers, params);
}
});
}
@Override
public Set<String> zrange(final String key, final long start, final long end) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrange(key, start, end);
}
});
}
@Override
public Long zrem(final String key, final String... member) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zrem(key, member);
}
});
}
@Override
public Double zincrby(final String key, final double score, final String member) {
return execute(new Callback<Double>() {
@Override
public Double call(JedisCommands commands) {
return commands.zincrby(key, score, member);
}
});
}
@Override
public Double zincrby(final String key, final double score, final String member, final ZIncrByParams params) {
return execute(new Callback<Double>() {
@Override
public Double call(JedisCommands commands) {
return commands.zincrby(key, score, member, params);
}
});
}
@Override
public Long zrank(final String key, final String member) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zrank(key, member);
}
});
}
@Override
public Long zrevrank(final String key, final String member) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zrevrank(key, member);
}
});
}
@Override
public Set<String> zrevrange(final String key, final long start, final long end) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrevrange(key, start, end);
}
});
}
@Override
public Set<Tuple> zrangeWithScores(final String key, final long start, final long end) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrangeWithScores(key, start, end);
}
});
}
@Override
public Set<Tuple> zrevrangeWithScores(final String key, final long start, final long end) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrevrangeWithScores(key, start, end);
}
});
}
@Override
public Long zcard(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zcard(key);
}
});
}
@Override
public Double zscore(final String key, final String member) {
return execute(new Callback<Double>() {
@Override
public Double call(JedisCommands commands) {
return commands.zscore(key, member);
}
});
}
@Override
public List<String> sort(final String key) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.sort(key);
}
});
}
@Override
public List<String> sort(final String key, final SortingParams sortingParameters) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.sort(key, sortingParameters);
}
});
}
@Override
public Long zcount(final String key, final double min, final double max) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zcount(key, min, max);
}
});
}
@Override
public Long zcount(final String key, final String min, final String max) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zcount(key, min, max);
}
});
}
@Override
public Set<String> zrangeByScore(final String key, final double min, final double max) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrangeByScore(key, min, max);
}
});
}
@Override
public Set<String> zrangeByScore(final String key, final String min, final String max) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrangeByScore(key, min, max);
}
});
}
@Override
public Set<String> zrevrangeByScore(final String key, final double max, final double min) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrevrangeByScore(key, max, min);
}
});
}
@Override
public Set<String> zrangeByScore(final String key, final double min, final double max, final int offset,
final int count) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrangeByScore(key, min, max, offset, count);
}
});
}
@Override
public Set<String> zrevrangeByScore(final String key, final String max, final String min) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrevrangeByScore(key, max, min);
}
});
}
@Override
public Set<String> zrangeByScore(final String key, final String min, final String max, final int offset,
final int count) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrangeByScore(key, min, max, offset, count);
}
});
}
@Override
public Set<String> zrevrangeByScore(final String key, final double max, final double min, final int offset,
final int count) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrevrangeByScore(key, max, min, offset, count);
}
});
}
@Override
public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrangeByScoreWithScores(key, min, max);
}
});
}
@Override
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrevrangeByScoreWithScores(key, max, min);
}
});
}
@Override
public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max, final int offset,
final int count) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrangeByScoreWithScores(key, min, max, offset, count);
}
});
}
@Override
public Set<String> zrevrangeByScore(final String key, final String max, final String min, final int offset,
final int count) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrevrangeByScore(key, max, min, offset, count);
}
});
}
@Override
public Set<Tuple> zrangeByScoreWithScores(final String key, final String min, final String max) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrangeByScoreWithScores(key, min, max);
}
});
}
@Override
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final String max, final String min) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrevrangeByScoreWithScores(key, max, min);
}
});
}
@Override
public Set<Tuple> zrangeByScoreWithScores(final String key, final String min, final String max, final int offset,
final int count) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrangeByScoreWithScores(key, min, max, offset, count);
}
});
}
@Override
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min, final int offset,
final int count) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrevrangeByScoreWithScores(key, max, min, offset, count);
}
});
}
@Override
public Set<Tuple> zrevrangeByScoreWithScores(final String key, final String max, final String min, final int offset,
final int count) {
return execute(new Callback<Set<Tuple>>() {
@Override
public Set<Tuple> call(JedisCommands commands) {
return commands.zrevrangeByScoreWithScores(key, max, min, offset, count);
}
});
}
@Override
public Long zremrangeByRank(final String key, final long start, final long end) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zremrangeByRank(key, start, end);
}
});
}
@Override
public Long zremrangeByScore(final String key, final double start, final double end) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zremrangeByScore(key, start, end);
}
});
}
@Override
public Long zremrangeByScore(final String key, final String start, final String end) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zremrangeByScore(key, start, end);
}
});
}
@Override
public Long zlexcount(final String key, final String min, final String max) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zlexcount(key, min, max);
}
});
}
@Override
public Set<String> zrangeByLex(final String key, final String min, final String max) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrangeByLex(key, min, max);
}
});
}
@Override
public Set<String> zrangeByLex(final String key, final String min, final String max, final int offset,
final int count) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrangeByLex(key, min, max, offset, count);
}
});
}
@Override
public Set<String> zrevrangeByLex(final String key, final String max, final String min) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrevrangeByLex(key, max, min);
}
});
}
@Override
public Set<String> zrevrangeByLex(final String key, final String max, final String min, final int offset,
final int count) {
return execute(new Callback<Set<String>>() {
@Override
public Set<String> call(JedisCommands commands) {
return commands.zrevrangeByLex(key, max, min, offset, count);
}
});
}
@Override
public Long zremrangeByLex(final String key, final String min, final String max) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.zremrangeByLex(key, min, max);
}
});
}
@Override
public Long linsert(final String key, final LIST_POSITION where, final String pivot, final String value) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.linsert(key, where, pivot, value);
}
});
}
@Override
public Long lpushx(final String key, final String... string) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.lpushx(key, string);
}
});
}
@Override
public Long rpushx(final String key, final String... string) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.rpushx(key, string);
}
});
}
@Deprecated
@Override
public List<String> blpop(final String arg) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.blpop(arg);
}
});
}
@Override
public List<String> blpop(final int timeout, final String key) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.blpop(timeout, key);
}
});
}
@Deprecated
@Override
public List<String> brpop(final String arg) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.brpop(arg);
}
});
}
@Override
public List<String> brpop(final int timeout, final String key) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.brpop(timeout, key);
}
});
}
@Override
public Long del(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.del(key);
}
});
}
@Override
public String echo(final String string) {
return execute(new Callback<String>() {
@Override
public String call(JedisCommands commands) {
return commands.echo(string);
}
});
}
@Override
public Long move(final String key, final int dbIndex) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.move(key, dbIndex);
}
});
}
@Override
public Long bitcount(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.bitcount(key);
}
});
}
@Override
public Long bitcount(final String key, final long start, final long end) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.bitcount(key, start, end);
}
});
}
@Override
public Long bitpos(final String key, final boolean value) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.bitpos(key, value);
}
});
}
@Override
public Long bitpos(final String key, final boolean value, final BitPosParams params) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.bitpos(key, value, params);
}
});
}
@Deprecated
@Override
public ScanResult<Entry<String, String>> hscan(final String key, final int cursor) {
return execute(new Callback<ScanResult<Entry<String, String>>>() {
@Override
public ScanResult<Entry<String, String>> call(JedisCommands commands) {
return commands.hscan(key, cursor);
}
});
}
@Deprecated
@Override
public ScanResult<String> sscan(final String key, final int cursor) {
return execute(new Callback<ScanResult<String>>() {
@Override
public ScanResult<String> call(JedisCommands commands) {
return commands.sscan(key, cursor);
}
});
}
@Deprecated
@Override
public ScanResult<Tuple> zscan(final String key, final int cursor) {
return execute(new Callback<ScanResult<Tuple>>() {
@Override
public ScanResult<Tuple> call(JedisCommands commands) {
return commands.zscan(key, cursor);
}
});
}
@Override
public ScanResult<Entry<String, String>> hscan(final String key, final String cursor) {
return execute(new Callback<ScanResult<Entry<String, String>>>() {
@Override
public ScanResult<Entry<String, String>> call(JedisCommands commands) {
return commands.hscan(key, cursor);
}
});
}
@Override
public ScanResult<Entry<String, String>> hscan(final String key, final String cursor, final ScanParams params) {
return execute(new Callback<ScanResult<Entry<String, String>>>() {
@Override
public ScanResult<Entry<String, String>> call(JedisCommands commands) {
return commands.hscan(key, cursor, params);
}
});
}
@Override
public ScanResult<String> sscan(final String key, final String cursor) {
return execute(new Callback<ScanResult<String>>() {
@Override
public ScanResult<String> call(JedisCommands commands) {
return commands.sscan(key, cursor);
}
});
}
@Override
public ScanResult<String> sscan(final String key, final String cursor, final ScanParams params) {
return execute(new Callback<ScanResult<String>>() {
@Override
public ScanResult<String> call(JedisCommands commands) {
return commands.sscan(key, cursor, params);
}
});
}
@Override
public ScanResult<Tuple> zscan(final String key, final String cursor) {
return execute(new Callback<ScanResult<Tuple>>() {
@Override
public ScanResult<Tuple> call(JedisCommands commands) {
return commands.zscan(key, cursor);
}
});
}
@Override
public ScanResult<Tuple> zscan(final String key, final String cursor, final ScanParams params) {
return execute(new Callback<ScanResult<Tuple>>() {
@Override
public ScanResult<Tuple> call(JedisCommands commands) {
return commands.zscan(key, cursor, params);
}
});
}
@Override
public Long pfadd(final String key, final String... elements) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.pfadd(key, elements);
}
});
}
@Override
public long pfcount(final String key) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.pfcount(key);
}
});
}
@Override
public Long geoadd(final String key, final double longitude, final double latitude, final String member) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.geoadd(key, longitude, latitude, member);
}
});
}
@Override
public Long geoadd(final String key, final Map<String, GeoCoordinate> memberCoordinateMap) {
return execute(new Callback<Long>() {
@Override
public Long call(JedisCommands commands) {
return commands.geoadd(key, memberCoordinateMap);
}
});
}
@Override
public Double geodist(final String key, final String member1, final String member2) {
return execute(new Callback<Double>() {
@Override
public Double call(JedisCommands commands) {
return commands.geodist(key, member1, member2);
}
});
}
@Override
public Double geodist(final String key, final String member1, final String member2, final GeoUnit unit) {
return execute(new Callback<Double>() {
@Override
public Double call(JedisCommands commands) {
return commands.geodist(key, member1, member2, unit);
}
});
}
@Override
public List<String> geohash(final String key, final String... members) {
return execute(new Callback<List<String>>() {
@Override
public List<String> call(JedisCommands commands) {
return commands.geohash(key, members);
}
});
}
@Override
public List<GeoCoordinate> geopos(final String key, final String... members) {
return execute(new Callback<List<GeoCoordinate>>() {
@Override
public List<GeoCoordinate> call(JedisCommands commands) {
return commands.geopos(key, members);
}
});
}
@Override
public List<GeoRadiusResponse> georadius(final String key, final double longitude, final double latitude,
final double radius, final GeoUnit unit) {
return execute(new Callback<List<GeoRadiusResponse>>() {
@Override
public List<GeoRadiusResponse> call(JedisCommands commands) {
return commands.georadius(key, longitude, latitude, radius, unit);
}
});
}
@Override
public List<GeoRadiusResponse> georadius(final String key, final double longitude, final double latitude,
final double radius, final GeoUnit unit, final GeoRadiusParam param) {
return execute(new Callback<List<GeoRadiusResponse>>() {
@Override
public List<GeoRadiusResponse> call(JedisCommands commands) {
return commands.georadius(key, longitude, latitude, radius, unit, param);
}
});
}
@Override
public List<GeoRadiusResponse> georadiusByMember(final String key, final String member, final double radius,
final GeoUnit unit) {
return execute(new Callback<List<GeoRadiusResponse>>() {
@Override
public List<GeoRadiusResponse> call(JedisCommands commands) {
return commands.georadiusByMember(key, member, radius, unit);
}
});
}
@Override
public List<GeoRadiusResponse> georadiusByMember(final String key, final String member, final double radius,
final GeoUnit unit, final GeoRadiusParam param) {
return execute(new Callback<List<GeoRadiusResponse>>() {
@Override
public List<GeoRadiusResponse> call(JedisCommands commands) {
return commands.georadiusByMember(key, member, radius, unit, param);
}
});
}
@Override
public List<Long> bitfield(final String key, final String... arguments) {
return execute(new Callback<List<Long>>() {
@Override
public List<Long> call(JedisCommands commands) {
return commands.bitfield(key, arguments);
}
});
}
}
整合配置:Redis配置
package com.study.demo.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.study.demo.util.RedisUtil;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Redis 配置(单点)
* @author lisuo
*
*/
@Configuration
public class RedisConfiguration {
@Bean(name = "redisPool")
public JedisPool jedisPool(@Autowired @Qualifier("redisPoolConfig") JedisPoolConfig config,
@Value("${redis.host1}") String host, @Value("${redis.port}") int port) {
return new JedisPool(config, host, port);
}
@Bean(name = "redisPoolConfig")
public JedisPoolConfig jedisPoolConfig(@Value("${redis.pool.maxIdle}") int maxTotal,
@Value("${redis.pool.maxIdle}") int maxIdle, @Value("${redis.pool.maxWaitMillis}") int maxWaitMillis) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
return config;
}
@Bean(name = "redisUtil")
public RedisUtil redisUtil(@Autowired @Qualifier("redisPool") JedisPool jedisPool) {
RedisUtil config = new RedisUtil(jedisPool);
return config;
}
}
缓存整合配置
package com.study.demo.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Configuration;
import com.study.demo.cache.DefaultKeyGenerator;
import com.study.demo.cache.RedisCacheManager;
import com.study.demo.util.RedisUtil;
@Configuration
@EnableCaching
public class SpringCacheConfiguraction extends CachingConfigurerSupport{
@Autowired
private RedisUtil redisUtil;
@Override
public CacheManager cacheManager() {
RedisCacheManager manager = new RedisCacheManager(redisUtil, "study:cache:", "study:cacheNameSet");
return manager;
}
@Override
public KeyGenerator keyGenerator() {
return new DefaultKeyGenerator();
}
}