1、自定义redis接口
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface Redis {
/********************************通用API************************************/
/**
* 删除值
* @param key
* @return 影响结果
*/
public long delete(String key);
/**
* 设置key的过时时间
* @param key
* @param seconds 秒数
* @return 影响行数
*/
public long setExpire(String key, int seconds);
/**
* 查找keys
* @param key
* @return
*/
public Set<String> keys(String pattern);
/**
* 值是否存在
* @param key
* @return
*/
public boolean exists(String key);
/**
* 关闭
*/
public void close();
/********************************String型数据结构API(数据结构不一致会报错)************************************/
/**
* 获取值
* @param key
* @return
*/
public String get(String key);
/**
* set操作会移除key的超时时间
* @param key
* @param valuen
* @return key对应的新的value
*/
public String set(String key, String value);
/**
* 如果值不存在,则set
* @param key
* @param valuen
* @return key对应的新的value
*/
public long setIfNotExist(String key, String value);
/**
* 当前key的值+ 1;
* 如果值不存在,则返回1;
* 如果已有值为非数字,则会抛出异常;
* @param key
* @return newValue
*/
public long incr(String key);
/**
* 当前key的值+ n;
* 如果值不存在,则保存并返回n;
* 如果已有值为非数字,则会抛出异常;
* @param key
* @param inc
* @return newValue
*/
public long incrBy(String key, long inc);
/**
* 获取数字型的值
* 如果不存在,则返回null
* @param key
* @return
*/
public Long getLong(String key);
/********************************hash型数据结构API(数据结构不一致会报错)************************************/
/**
* 按hash方式存值
* @param key
* @param field
* @param value
* @return 0:field已存在且被更新 , 1:field作为新的属性被添加
*/
public Long hset(String key, String field, String value);
/**
* 按hash方式存值,如果field不存在,则set
* @param key
* @param field
* @param value
* @return 0:field已存在且被更新 , 1:field作为新的属性被添加
*/
public Long hsetIfNotExist(String key, String field, String value);
/**
* 按hash方式存值
* @param key
* @param field
* @param value
* @return
*/
public String hmset(String key, Map<String, String> hashMap);
/**
* 按hash方式取值
* @param key
* @param field
* @param value
* @return
*/
public String hget(String key, String field);
/**
* 按hash方式取值
* @param key
* @param field
* @param value
* @return
*/
public List<String> hmget(String key, String... fields);
/**
* 按hash方式取值
* @param key
* @return
*/
public Map<String, String> hgetAll(String key);
/**
* 获取hash类型的keys
* @param key
* @return
*/
public Set<String> hkeys(String key);
/**
* 获取hash类型的vals
* @param key
* @return
*/
public List<String> hvals(String key);
/**
* hash类型的field是否存在
* @param key
* @return
*/
public boolean hexists(String key, String field);
/**
* 删除hash类型的field
* @param key
* @return 影响行数
*/
public long hdel(String key, String field);
/**
* 删除hash类型的fields
* @param key
* @return
*/
public long hdel(String key, String... fields);
/**
* 获取hash类型的容器大小
* @param key
* @return
*/
public long hlen(String key);
/**
* 增加field的值,如果value不是数值型,则会报错
* @param key
* @param field
* @param val
* @return 增加过后的值
*/
public long hincBy(String key, String field, long value);
/**
* 增加field的值,如果value不是数值型,则会报错
* @param key
* @param field
* @param val
* @return
*/
public double hincByFloat(String key, String field, double value);
/********************************list类型数据结构API(数据结构不一致会报错)************************************/
/**
* 在名称为key的list尾添加一个值为value的元素
* @param key
* @param value
* @return 当前list的大小
*/
public long rpush(String key, String value);
/**
* 在名称为key的list尾添加值为values的元素集合
* @param key
* @param value
* @return 当前list的大小
*/
public long rpush(String key, String... values);
/**
*在名称为key的list头添加一个值为value的 元素
* @param key
* @param value
* @return 当前list的大小
*/
public long lpush(String key, String value);
/**
*在名称为key的list头添加值为values的元素集合
* @param key
* @param value
* @return 当前list的大小
*/
public long lpush(String key, String... value);
/**
* 返回名称为key的list的长度
* @param key
* @return
*/
public long llen(String key);
/**
* 返回名称为key的list所有元素
* @param key
* @return
*/
public List<String> lgetAll(String key);
/**
* 返回名称为key的list中start至end之间的元素(下标从0开始,下同)
* @param key
* @param start>=0
* @param end>=0
* @param 0<end-start<=1000;
* @return
*/
public List<String> lrange(String key, long start, long end);
/**
* 截取名称为key的list,保留start至end之间的元素(如果木有,则截为空)
* @param key
* @param start>0
* @param end>0
* @param 0<end-start<=1000;
* @return
*/
public String ltrim(String key, long start, long end);
/**
* 返回名称为key的list中index位置的元素
* 超出范围则返回空
* @param key
* @param index
* @return
*/
public String lindex(String key, long index);
/**
* 给名称为key的list中index位置的元素赋值为value
* 超出范围则会报错
* @param key
* @param index
* @param value
* @return
*/
public String lset(String key, long index, String value);
/**
* 返回并删除名称为key的list中的首元素
* @param key
* @return
*/
public String lpop(String key);
/**
* 返回并删除名称为key的list中的尾元素
* @param key
* @return
*/
public String rpop(String key);
/**
* 删除count个名称为key的list中值为value的元素。
* count为0,删除所有值为value的元素。
* count>0,从头至尾删除count个值为value的元素。
* count<0从尾到头删除|count|个值为value的元素。
* @param key
* @param count
* @param value
* @return
*/
public long lremove(String key, int count, String value);
/**
* 返回并删除名称为srckey的list的尾元素,并将该元素添加到名称为dstkey的list的头部
* 适用于[安全队列]的场景
* @param srckey
* @param dstkey
* @return
*/
public String rpoplpush(String srckey, String dstkey);
/********************************set类型数据结构API(数据结构不一致会报错)************************************/
/**
* 向名称为key的set中添加元素member
* @param key
* @param member
* @return 影响记录数
*/
public long sadd(String key, String member);
/**
* 向名称为key的set中添加元素members
* @param key
* @param members
* @return
*/
public long sadd(String key, String... members);
/**
* 返回名称为key的set的所有元素
* @param key
* @return
*/
public Set<String> smembers(String key);
/**
* 随机返回名称为key的set的一个元素
* @param key
* @return
*/
public String srandmember(String key);
/**
* 随机返回并删除名称为key的set中一个元素
* @param key
* @return
*/
public String spop(String key);
/**
* 返回名称为key的set的基数
* @param key
* @return
*/
public long scard(String key);
/**
* 测试member是否是名称为key的set的元素
* @param key
* @param member
* @return
*/
public boolean sismember(String key, String member);
/**
* 删除名称为key的set中的元素member
* @param key
* @param member
* @return 影响行数
*/
public long sremove(String key, String member);
/**
* 删除名称为key的set中的元素members
* @param key
* @param member
* @return 影响行数
*/
public long sremove(String key, String... member);
/**
* 将member元素从名称为srckey的集合移到名称为dstkey的集合
* @param srckey
* @param dstkey
* @param member
* @return 影响行数
*/
public long smove(String srckey, String dstkey, String member);
/**
* 求交集
* @param keys
* @return
*/
public Set<String> sinter(String... keys);
/**
* 求交集并将交集保存到dstkey的集合
* @param dstkey
* @param keys
* @return 影响行数
*/
public long sinterstore(String dstkey, String... keys);
/**
* 求并集
* @param keys
* @return
*/
public Set<String> sunion(String... keys);
/**
* 求并集并将并集保存到dstkey的集合
* @param dstkey
* @param keys
* @return 影响行数
*/
public long sunionstore(String dstkey, String... keys);
/**
* 求差集
* @param keys
* @return
*/
public Set<String> sdiff(String... keys);
/**
* 求差集并将差集保存到dstkey的集合
* @param keys
* @return 影响行数
*/
public long sdiffstore(String dstkey, String... keys);
/**************zset(sorted set)类型数据结构API(数据结构不一致会报错)****************/
/***************1)score:排序权重; 2)rank:排序顺序索引 ****************************/
/**
* 向名称为key的zset中添加元素member,score用于排序。如果该元素已经存在,则根据score更新该元素的顺序。
* @param key
* @param score
* @param member
* @return 影响行数
*/
public long zadd(String key, double score, String member);
/**
* 如果在名称为key的zset中已经存在元素member,则该元素的score增加increment;
* 否则向集合中添加该元素,其score的值为increment
* @param key
* @param increment
* @param member
* @return 新的权值
*/
public double zincrby(String key, double score, String member);
/**
* 返回名称为key的zset中元素element的score
* @param key
* @param element
* @return
*/
public Double zscore(String key, String member);
/**
* 返回zset的大小
* @param key
* @return
*/
public long zcard(String key);
/**
* 从小到大排序的排序索引(从0开始),没有则返回null
* @param key
* @param member
* @return
*/
public Long zrank(String key, String member);
/**
* 从大到小排序的排序索引(从0开始),没有则返回null
* @param key
* @param member
* @return
*/
public Long zrevrank(String key, String member);
/**
* 从小到大的排序方式返回key(0~-1会返回全部)
* @param key
* @param start
* @param end
* @return
*/
public Set<String> zrange(String key, long start, long end);
/**
* 从小到大的排序方式返回全部元素
* @param key
* @param start
* @param end
* @return
*/
public Set<String> zrangeAll(String key);
/**
* 从大到小的排序方式返回key
* @param key
* @param start
* @param end
* @return
*/
public Set<String> zrevrange(String key, long start, long end);
/**
* 从大到小的排序方式返回全部元素
* @param key
* @param start
* @param end
* @return
*/
public Set<String> zrevrangeAll(String key);
/**
* 从小到大返回名称为key的zset中score >= min且score <= max的所有元素
* @param key
* @param min
* @param max
* @return
*/
public Set<String> zrangeByScore(String key, double minScore, double maxScore);
/**
* 从大到小返回名称为key的zset中score >= min且score <= max的所有元素
* @param key
* @param min
* @param max
* @return
*/
public Set<String> zrevrangeByScore(String key, double minScore, double maxScore);
/**
* 删除名称为key的zset中的元素member
* @param key
* @param member
* @return 影响行数
*/
public long zremove(String key, String member);
/**
* 删除名称为key的zset中的元素members
* @param key
* @param member
* @return
*/
public long zremove(String key, String... members);
/**
* 删除名称为key的zset中rank >= min且rank <= max的所有元素
* @param key
* @param min
* @param max
*/
public long zremoveRangeByRank(String key, long start, long end);
/**
* 删除名称为key的zset中score >= min且score <= max的所有元素
* @param key
* @param min
* @param max
* @return
*/
public long zremoveRangeByScore(String key, double minScore, double maxScore);
}
二、 实现类
2.1 单点实现类SingleRedis
package cn.com.frame.redis.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cn.com.frame.redis.Redis;
import cn.com.frame.redis.RedisPoolCofig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class SingleRedis implements Redis{
private JedisPool pool;
public SingleRedis(HostAndPort hostAndPort) {
JedisPoolConfig jedisPoolConfig = RedisPoolCofig.getDefault();
this.pool = new JedisPool(jedisPoolConfig,hostAndPort.getHost(),hostAndPort.getPort());
}
public SingleRedis(HostAndPort hostAndPort,JedisPoolConfig jedisPoolConfig){
this.pool = new JedisPool(jedisPoolConfig,hostAndPort.getHost(),hostAndPort.getPort());
}
private Jedis getJedis() {
return pool.getResource();
}
@Override
public void close() {
if (pool != null) {
pool.destroy();
}
}
@Override
public String get(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.get(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> keys(String pattern) {
Jedis jedis = this.getJedis();
try {
return jedis.keys(pattern);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String set(String key, String value) {
Jedis jedis = this.getJedis();
try {
return jedis.set(key, value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long setIfNotExist(String key, String value) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.setnx(key, value);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long setExpire(String key, int seconds) {
Jedis jedis = this.getJedis();
try {
Long val = jedis.expire(key, seconds);
return val == null ? 0 : val;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public boolean exists(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.exists(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long incr(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.incr(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long incrBy(String key, long inc) {
Jedis jedis = this.getJedis();
try {
return jedis.incrBy(key, inc);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long getLong(String key) {
Jedis jedis = this.getJedis();
try {
String value = jedis.get(key);
if (value == null) {
return null;
}
return Long.valueOf(value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long delete(String key) {
Jedis jedis = this.getJedis();
try {
Long deleteResult = jedis.del(key);
return deleteResult == null ? 0 : deleteResult;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long hset(String key, String field, String value) {
Jedis jedis = this.getJedis();
try {
return jedis.hset(key, field, value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long hsetIfNotExist(String key, String field, String value) {
Jedis jedis = this.getJedis();
try {
return jedis.hsetnx(key, field, value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String hmset(String key, Map<String, String> hashMap) {
Jedis jedis = this.getJedis();
try {
return jedis.hmset(key, hashMap);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String hget(String key, String field) {
Jedis jedis = this.getJedis();
try {
return jedis.hget(key, field);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public List<String> hmget(String key, String... fields) {
Jedis jedis = this.getJedis();
try {
return jedis.hmget(key, fields);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Map<String, String> hgetAll(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.hgetAll(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> hkeys(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.hkeys(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public List<String> hvals(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.hvals(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public boolean hexists(String key, String field) {
Jedis jedis = this.getJedis();
try {
return jedis.hexists(key, field);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long hdel(String key, String field) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.hdel(key, field);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long hdel(String key, String... fields) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.hdel(key, fields);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long hlen(String key) {
Jedis jedis = this.getJedis();
try {
Long len = jedis.hlen(key);
return len == null ? 0 : len;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long hincBy(String key, String field, long value) {
Jedis jedis = this.getJedis();
try {
Long val = jedis.hincrBy(key, field, value);
return val == null ? 0 : val;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public double hincByFloat(String key, String field, double value) {
Jedis jedis = this.getJedis();
try {
Double val = jedis.hincrByFloat(key, field, value);
return val == null ? 0 : val;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long rpush(String key, String value) {
Jedis jedis = this.getJedis();
try {
Long newLength = jedis.rpush(key, value);
return newLength == null ? 0 : newLength;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long rpush(String key, String... values) {
Jedis jedis = this.getJedis();
try {
Long newLength = jedis.rpush(key, values);
return newLength == null ? 0 : newLength;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long lpush(String key, String value) {
Jedis jedis = this.getJedis();
try {
Long newLength = jedis.lpush(key, value);
return newLength == null ? 0 : newLength;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long lpush(String key, String... value) {
Jedis jedis = this.getJedis();
try {
Long newLength = jedis.lpush(key, value);
return newLength == null ? 0 : newLength;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long llen(String key) {
Jedis jedis = this.getJedis();
try {
Long length = jedis.llen(key);
return length == null ? 0 : length;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public List<String> lgetAll(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.lrange(key, 0, -1);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public List<String> lrange(String key, long start, long end) {
if (start < 0 || end < 0 || start > end || end - start > 1000) {
return new ArrayList<String>();
}
Jedis jedis = this.getJedis();
try {
return jedis.lrange(key, start, end);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String ltrim(String key, long start, long end) {
if (start < 0 || end < 0 || start > end) {
return "illegal param";
}
Jedis jedis = this.getJedis();
try {
return jedis.ltrim(key, start, end);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String lindex(String key, long index) {
Jedis jedis = this.getJedis();
try {
return jedis.lindex(key, index);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String lset(String key, long index, String value) {
Jedis jedis = this.getJedis();
try {
return jedis.lset(key, index, value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String lpop(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.lpop(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String rpop(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.rpop(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long lremove(String key, int count, String value) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.lrem(key, count, value);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String rpoplpush(String srckey, String dstkey) {
Jedis jedis = this.getJedis();
try {
return jedis.rpoplpush(srckey, dstkey);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sadd(String key, String member) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.sadd(key, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sadd(String key, String... members) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.sadd(key, members);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> smembers(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.smembers(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String srandmember(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.srandmember(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String spop(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.spop(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long scard(String key) {
Jedis jedis = this.getJedis();
try {
Long length = jedis.scard(key);
return length == null ? 0 : length;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public boolean sismember(String key, String member) {
Jedis jedis = this.getJedis();
try {
return jedis.sismember(key, member);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sremove(String key, String member) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.srem(key, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sremove(String key, String... member) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.srem(key, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long smove(String srckey, String dstkey, String member) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.smove(srckey, dstkey, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> sinter(String... keys) {
Jedis jedis = this.getJedis();
try {
return jedis.sinter(keys);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sinterstore(String dstkey, String... keys) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.sinterstore(dstkey, keys);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> sunion(String... keys) {
Jedis jedis = this.getJedis();
try {
return jedis.sunion(keys);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sunionstore(String dstkey, String... keys) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.sunionstore(dstkey, keys);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> sdiff(String... keys) {
Jedis jedis = this.getJedis();
try {
return jedis.sdiff(keys);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sdiffstore(String dstkey, String... keys) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.sdiffstore(dstkey, keys);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zadd(String key, double score, String member) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.zadd(key, score, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public double zincrby(String key, double score, String member) {
Jedis jedis = this.getJedis();
try {
Double newScoreValue = jedis.zincrby(key, score, member);
return newScoreValue == null ? 0 : newScoreValue;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Double zscore(String key, String member) {
Jedis jedis = this.getJedis();
try {
return jedis.zscore(key, member);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zcard(String key) {
Jedis jedis = this.getJedis();
try {
Long len = jedis.zcard(key);
return len == null ? 0 : len;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long zrank(String key, String member) {
Jedis jedis = this.getJedis();
try {
return jedis.zrank(key, member);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long zrevrank(String key, String member) {
Jedis jedis = this.getJedis();
try {
return jedis.zrevrank(key, member);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrange(String key, long start, long end) {
Jedis jedis = this.getJedis();
try {
return jedis.zrange(key, start, end);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrangeAll(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.zrange(key, 0, -1);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrevrange(String key, long start, long end) {
Jedis jedis = this.getJedis();
try {
return jedis.zrevrange(key, start, end);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrevrangeAll(String key) {
Jedis jedis = this.getJedis();
try {
return jedis.zrevrange(key, 0, -1);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrangeByScore(String key, double minScore, double maxScore) {
Jedis jedis = this.getJedis();
try {
return jedis.zrangeByScore(key, minScore, maxScore);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrevrangeByScore(String key, double minScore, double maxScore) {
Jedis jedis = this.getJedis();
try {
return jedis.zrevrangeByScore(key, maxScore, minScore);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zremove(String key, String member) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.zrem(key, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zremove(String key, String... members) {
Jedis jedis = this.getJedis();
try {
Long effectRows = jedis.zrem(key, members);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zremoveRangeByRank(String key, long start, long end) {
Jedis jedis = this.getJedis();
try {
Long results = jedis.zremrangeByRank(key, start, end);
return results == null ? 0 : results;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zremoveRangeByScore(String key, double minScore, double maxScore) {
Jedis jedis = this.getJedis();
try {
Long results = jedis.zremrangeByScore(key, minScore, maxScore);
return results == null ? 0 : results;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
}
2.2、分布式redis实现类SharedRedis
package cn.com.frame.redis.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import cn.com.frame.redis.Redis;
import cn.com.frame.redis.RedisPoolCofig;
public class SharedRedis implements Redis {
private ShardedJedisPool shardedJedisPool;
private SharedRedis(List<HostAndPort> hostAndPorts) {
List<JedisShardInfo> jdsInfoList = new ArrayList<JedisShardInfo>();
for (HostAndPort hostAndPort : hostAndPorts) {
jdsInfoList.add(new JedisShardInfo(hostAndPort.getHost(), hostAndPort.getPort()));
}
JedisPoolConfig config = RedisPoolCofig.getDefault();
shardedJedisPool = new ShardedJedisPool(config, jdsInfoList);
}
private SharedRedis(List<HostAndPort> hostAndPorts, JedisPoolConfig config) {
List<JedisShardInfo> jdsInfoList = new ArrayList<JedisShardInfo>();
for (HostAndPort hostAndPort : hostAndPorts) {
jdsInfoList.add(new JedisShardInfo(hostAndPort.getHost(), hostAndPort.getPort()));
}
shardedJedisPool = new ShardedJedisPool(config, jdsInfoList);
}
private ShardedJedis getJedis() {
return shardedJedisPool.getResource();
}
@Override
public void close() {
if (shardedJedisPool != null) {
shardedJedisPool.destroy();
}
}
@Override
public String get(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.get(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> keys(String pattern) {
return null;
}
@Override
public String set(String key, String value) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.set(key, value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long setIfNotExist(String key, String value) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.setnx(key, value);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long setExpire(String key, int seconds) {
ShardedJedis jedis = this.getJedis();
try {
Long val = jedis.expire(key, seconds);
return val == null ? 0 : val;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public boolean exists(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.exists(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long incr(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.incr(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long incrBy(String key, long inc) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.incrBy(key, inc);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long getLong(String key) {
ShardedJedis jedis = this.getJedis();
try {
String value = jedis.get(key);
if (value == null) {
return null;
}
return Long.valueOf(value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long delete(String key) {
ShardedJedis jedis = this.getJedis();
try {
Long deleteResult = jedis.del(key);
return deleteResult == null ? 0 : deleteResult;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long hset(String key, String field, String value) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.hset(key, field, value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long hsetIfNotExist(String key, String field, String value) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.hsetnx(key, field, value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String hmset(String key, Map<String, String> hashMap) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.hmset(key, hashMap);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String hget(String key, String field) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.hget(key, field);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public List<String> hmget(String key, String... fields) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.hmget(key, fields);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Map<String, String> hgetAll(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.hgetAll(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> hkeys(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.hkeys(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public List<String> hvals(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.hvals(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public boolean hexists(String key, String field) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.hexists(key, field);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long hdel(String key, String field) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.hdel(key, field);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long hdel(String key, String... fields) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.hdel(key, fields);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long hlen(String key) {
ShardedJedis jedis = this.getJedis();
try {
Long len = jedis.hlen(key);
return len == null ? 0 : len;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long hincBy(String key, String field, long value) {
ShardedJedis jedis = this.getJedis();
try {
Long val = jedis.hincrBy(key, field, value);
return val == null ? 0 : val;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public double hincByFloat(String key, String field, double value) {
ShardedJedis jedis = this.getJedis();
try {
Double val = jedis.hincrByFloat(key, field, value);
return val == null ? 0 : val;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long rpush(String key, String value) {
ShardedJedis jedis = this.getJedis();
try {
Long newLength = jedis.rpush(key, value);
return newLength == null ? 0 : newLength;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long rpush(String key, String... values) {
ShardedJedis jedis = this.getJedis();
try {
Long newLength = jedis.rpush(key, values);
return newLength == null ? 0 : newLength;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long lpush(String key, String value) {
ShardedJedis jedis = this.getJedis();
try {
Long newLength = jedis.lpush(key, value);
return newLength == null ? 0 : newLength;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long lpush(String key, String... value) {
ShardedJedis jedis = this.getJedis();
try {
Long newLength = jedis.lpush(key, value);
return newLength == null ? 0 : newLength;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long llen(String key) {
ShardedJedis jedis = this.getJedis();
try {
Long length = jedis.llen(key);
return length == null ? 0 : length;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public List<String> lgetAll(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.lrange(key, 0, -1);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public List<String> lrange(String key, long start, long end) {
if (start < 0 || end < 0 || start > end || end - start > 1000) {
return new ArrayList<String>();
}
ShardedJedis jedis = this.getJedis();
try {
return jedis.lrange(key, start, end);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String ltrim(String key, long start, long end) {
if (start < 0 || end < 0 || start > end) {
return "illegal param";
}
ShardedJedis jedis = this.getJedis();
try {
return jedis.ltrim(key, start, end);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String lindex(String key, long index) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.lindex(key, index);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String lset(String key, long index, String value) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.lset(key, index, value);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String lpop(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.lpop(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String rpop(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.rpop(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long lremove(String key, int count, String value) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.lrem(key, count, value);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String rpoplpush(String srckey, String dstkey) {
return null;
}
@Override
public long sadd(String key, String member) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.sadd(key, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sadd(String key, String... members) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.sadd(key, members);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> smembers(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.smembers(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String srandmember(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.srandmember(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public String spop(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.spop(key);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long scard(String key) {
ShardedJedis jedis = this.getJedis();
try {
Long length = jedis.scard(key);
return length == null ? 0 : length;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public boolean sismember(String key, String member) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.sismember(key, member);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sremove(String key, String member) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.srem(key, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long sremove(String key, String... member) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.srem(key, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long smove(String srckey, String dstkey, String member) {
return 0;
}
@Override
public Set<String> sinter(String... keys) {
return null;
}
@Override
public long sinterstore(String dstkey, String... keys) {
return 0;
}
@Override
public Set<String> sunion(String... keys) {
return null;
}
@Override
public long sunionstore(String dstkey, String... keys) {
return 0;
}
@Override
public Set<String> sdiff(String... keys) {
return null;
}
@Override
public long sdiffstore(String dstkey, String... keys) {
return 0;
}
@Override
public long zadd(String key, double score, String member) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.zadd(key, score, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public double zincrby(String key, double score, String member) {
ShardedJedis jedis = this.getJedis();
try {
Double newScoreValue = jedis.zincrby(key, score, member);
return newScoreValue == null ? 0 : newScoreValue;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Double zscore(String key, String member) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.zscore(key, member);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zcard(String key) {
ShardedJedis jedis = this.getJedis();
try {
Long len = jedis.zcard(key);
return len == null ? 0 : len;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long zrank(String key, String member) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.zrank(key, member);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Long zrevrank(String key, String member) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.zrevrank(key, member);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrange(String key, long start, long end) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.zrange(key, start, end);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrangeAll(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.zrange(key, 0, -1);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrevrange(String key, long start, long end) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.zrevrange(key, start, end);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrevrangeAll(String key) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.zrevrange(key, 0, -1);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrangeByScore(String key, double minScore, double maxScore) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.zrangeByScore(key, minScore, maxScore);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public Set<String> zrevrangeByScore(String key, double minScore, double maxScore) {
ShardedJedis jedis = this.getJedis();
try {
return jedis.zrevrangeByScore(key, maxScore, minScore);
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zremove(String key, String member) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.zrem(key, member);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zremove(String key, String... members) {
ShardedJedis jedis = this.getJedis();
try {
Long effectRows = jedis.zrem(key, members);
return effectRows == null ? 0 : effectRows;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zremoveRangeByRank(String key, long start, long end) {
ShardedJedis jedis = this.getJedis();
try {
Long results = jedis.zremrangeByRank(key, start, end);
return results == null ? 0 : results;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
@Override
public long zremoveRangeByScore(String key, double minScore, double maxScore) {
ShardedJedis jedis = this.getJedis();
try {
Long results = jedis.zremrangeByScore(key, minScore, maxScore);
return results == null ? 0 : results;
} finally {
if (null != jedis) {
jedis.close();
}
}
}
}