redis客户端之jedis 操作工具类
包括连接池的使用及 jedis对string 、set、sortset、list、hash的所有常规操作。
redis.properties文件:
#\u63A7\u5236\u4E00\u4E2Apool\u53EF\u5206\u914D\u591A\u5C11\u4E2Ajedis\u5B9E\u4F8B\uFF0C\u901A\u8FC7pool.getResource()\u6765\u83B7\u53D6\uFF1B\u5982\u679C\u8D4B\u503C\u4E3A-1\uFF0C\u5219\u8868\u793A\u4E0D\u9650\u5236\uFF1B\u5982\u679Cpool\u5DF2\u7ECF\u5206\u914D\u4E86maxActive\u4E2Ajedis\u5B9E\u4F8B\uFF0C\u5219\u6B64\u65F6pool\u7684\u72B6\u6001\u5C31\u6210exhausted\u4E86\uFF0C\u5728JedisPoolConfig redis.pool.maxActive=1024 #\u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle\u7684jedis\u5B9E\u4F8B\uFF1B redis.pool.maxIdle=200 #\u8868\u793A\u5F53borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B redis.pool.maxWait=1000 #\u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Calidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684\uFF1B redis.pool.testOnBorrow=true #\u5728return\u7ED9pool\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B redis.pool.testOnReturn=true #ip redis.ip=yourip #port redis.port=6379 #password redis.password=yourword
-
package com.test.util;
-
-
import java.util.List;
-
import java.util.Map;
-
import java.util.Set;
-
-
import org.apache.log4j.Logger;
-
-
import com.test.constants.JRedisPoolConfig;
-
-
import redis.clients.jedis.Jedis;
-
import redis.clients.jedis.JedisPool;
-
import redis.clients.jedis.JedisPoolConfig;
-
import redis.clients.jedis.SortingParams;
-
import redis.clients.jedis.BinaryClient.LIST_POSITION;
-
import redis.clients.util.SafeEncoder;
-
-
/**
-
* @author Mr.hu
-
* @version crateTime:2013-10-30 下午5:41:30
-
* Class Explain:JedisUtil
-
*/
-
public
class JedisUtil {
-
-
private Logger log = Logger.getLogger(
this.getClass());
-
/**缓存生存时间 */
-
private
final
int expire =
60000;
-
/** 操作Key的方法 */
-
public Keys KEYS;
-
/** 对存储结构为String类型的操作 */
-
public Strings STRINGS;
-
/** 对存储结构为List类型的操作 */
-
public Lists LISTS;
-
/** 对存储结构为Set类型的操作 */
-
public Sets SETS;
-
/** 对存储结构为HashMap类型的操作 */
-
public Hash HASH;
-
/** 对存储结构为Set(排序的)类型的操作 */
-
public SortSet SORTSET;
-
private
static JedisPool jedisPool =
null;
-
-
private JedisUtil() {
-
-
}
-
static {
-
JedisPoolConfig config =
new JedisPoolConfig();
-
config.setMaxActive(JRedisPoolConfig.MAX_ACTIVE);
-
config.setMaxIdle(JRedisPoolConfig.MAX_IDLE);
-
config.setMaxWait(JRedisPoolConfig.MAX_WAIT);
-
config.setTestOnBorrow(JRedisPoolConfig.TEST_ON_BORROW);
-
config.setTestOnReturn(JRedisPoolConfig.TEST_ON_RETURN);
-
//redis如果设置了密码:
-
jedisPool =
new JedisPool(config, JRedisPoolConfig.REDIS_IP,
-
JRedisPoolConfig.REDIS_PORT,
-
10000,JRedisPoolConfig.REDIS_PASSWORD);
-
//redis未设置了密码:
-
// jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP,
-
// JRedisPoolConfig.REDIS_PORT);
-
}
-
-
public JedisPool getPool() {
-
return jedisPool;
-
}
-
-
/**
-
* 从jedis连接池中获取获取jedis对象
-
* @return
-
*/
-
public Jedis getJedis() {
-
return jedisPool.getResource();
-
}
-
-
-
private
static
final JedisUtil jedisUtil =
new JedisUtil();
-
-
-
/**
-
* 获取JedisUtil实例
-
* @return
-
*/
-
public static JedisUtil getInstance() {
-
return jedisUtil;
-
}
-
-
/**
-
* 回收jedis
-
* @param jedis
-
*/
-
public void returnJedis(Jedis jedis) {
-
jedisPool.returnResource(jedis);
-
}
-
-
-
/**
-
* 设置过期时间
-
* @author ruan 2013-4-11
-
* @param key
-
* @param seconds
-
*/
-
public void expire(String key, int seconds) {
-
if (seconds <=
0) {
-
return;
-
}
-
Jedis jedis = getJedis();
-
jedis.expire(key, seconds);
-
returnJedis(jedis);
-
}
-
-
/**
-
* 设置默认过期时间
-
* @author ruan 2013-4-11
-
* @param key
-
*/
-
public void expire(String key) {
-
expire(key, expire);
-
}
-
-
-
//*******************************************Keys*******************************************//
-
public
class Keys {
-
-
/**
-
* 清空所有key
-
*/
-
public String flushAll() {
-
Jedis jedis = getJedis();
-
String stata = jedis.flushAll();
-
returnJedis(jedis);
-
return stata;
-
}
-
-
/**
-
* 更改key
-
* @param String oldkey
-
* @param String newkey
-
* @return 状态码
-
* */
-
public String rename(String oldkey, String newkey) {
-
return rename(SafeEncoder.encode(oldkey),
-
SafeEncoder.encode(newkey));
-
}
-
-
/**
-
* 更改key,仅当新key不存在时才执行
-
* @param String oldkey
-
* @param String newkey
-
* @return 状态码
-
* */
-
public long renamenx(String oldkey, String newkey) {
-
Jedis jedis = getJedis();
-
long status = jedis.renamenx(oldkey, newkey);
-
returnJedis(jedis);
-
return status;
-
}
-
-
/**
-
* 更改key
-
* @param String oldkey
-
* @param String newkey
-
* @return 状态码
-
* */
-
public String rename(byte[] oldkey, byte[] newkey) {
-
Jedis jedis = getJedis();
-
String status = jedis.rename(oldkey, newkey);
-
returnJedis(jedis);
-
return status;
-
}
-
-
/**
-
* 设置key的过期时间,以秒为单位
-
* @param String key
-
* @param 时间,已秒为单位
-
* @return 影响的记录数
-
* */
-
public long expired(String key, int seconds) {
-
Jedis jedis = getJedis();
-
long count = jedis.expire(key, seconds);
-
returnJedis(jedis);
-
return count;
-
}
-
-
/**
-
* 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。
-
* @param String key
-
* @param 时间,已秒为单位
-
* @return 影响的记录数
-
* */
-
public long expireAt(String key, long timestamp) {
-
Jedis jedis = getJedis();
-
long count = jedis.expireAt(key, timestamp);
-
returnJedis(jedis);
-
return count;
-
}
-
-
/**
-
* 查询key的过期时间
-
* @param String key
-
* @return 以秒为单位的时间表示
-
* */
-
public long ttl(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis=getJedis();
-
long len = sjedis.ttl(key);
-
returnJedis(sjedis);
-
return len;
-
}
-
-
/**
-
* 取消对key过期时间的设置
-
* @param key
-
* @return 影响的记录数
-
* */
-
public long persist(String key) {
-
Jedis jedis = getJedis();
-
long count = jedis.persist(key);
-
returnJedis(jedis);
-
return count;
-
}
-
-
/**
-
* 删除keys对应的记录,可以是多个key
-
* @param String ... keys
-
* @return 删除的记录数
-
* */
-
public long del(String... keys) {
-
Jedis jedis = getJedis();
-
long count = jedis.del(keys);
-
returnJedis(jedis);
-
return count;
-
}
-
-
/**
-
* 删除keys对应的记录,可以是多个key
-
* @param String .. keys
-
* @return 删除的记录数
-
* */
-
public long del(byte[]... keys) {
-
Jedis jedis = getJedis();
-
long count = jedis.del(keys);
-
returnJedis(jedis);
-
return count;
-
}
-
-
/**
-
* 判断key是否存在
-
* @param String key
-
* @return boolean
-
* */
-
public boolean exists(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis=getJedis();
-
boolean exis = sjedis.exists(key);
-
returnJedis(sjedis);
-
return exis;
-
}
-
-
/**
-
* 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法
-
* @param String key
-
* @return List<String> 集合的全部记录
-
* **/
-
public List<String> sort(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis=getJedis();
-
List<String> list = sjedis.sort(key);
-
returnJedis(sjedis);
-
return list;
-
}
-
-
/**
-
* 对List,Set,SortSet进行排序或limit
-
* @param String key
-
* @param SortingParams parame 定义排序类型或limit的起止位置.
-
* @return List<String> 全部或部分记录
-
* **/
-
public List<String> sort(String key, SortingParams parame) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis=getJedis();
-
List<String> list = sjedis.sort(key, parame);
-
returnJedis(sjedis);
-
return list;
-
}
-
-
/**
-
* 返回指定key存储的类型
-
* @param String key
-
* @return String string|list|set|zset|hash
-
* **/
-
public String type(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis=getJedis();
-
String type = sjedis.type(key);
-
returnJedis(sjedis);
-
return type;
-
}
-
-
/**
-
* 查找所有匹配给定的模式的键
-
* @param String key的表达式,*表示多个,?表示一个
-
* */
-
public Set<String> keys(String pattern) {
-
Jedis jedis = getJedis();
-
Set<String> set = jedis.keys(pattern);
-
returnJedis(jedis);
-
return set;
-
}
-
}
-
-
//*******************************************Sets*******************************************//
-
public
class Sets {
-
-
/**
-
* 向Set添加一条记录,如果member已存在返回0,否则返回1
-
* @param String key
-
* @param String member
-
* @return 操作码,0或1
-
* */
-
public long sadd(String key, String member) {
-
Jedis jedis = getJedis();
-
long s = jedis.sadd(key, member);
-
returnJedis(jedis);
-
return s;
-
}
-
-
public long sadd(byte[] key, byte[] member) {
-
Jedis jedis = getJedis();
-
long s = jedis.sadd(key, member);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 获取给定key中元素个数
-
* @param String key
-
* @return 元素个数
-
* */
-
public long scard(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
long len = sjedis.scard(key);
-
returnJedis(sjedis);
-
return len;
-
}
-
-
/**
-
* 返回从第一组和所有的给定集合之间的差异的成员
-
* @param String ... keys
-
* @return 差异的成员集合
-
* */
-
public Set<String> sdiff(String... keys) {
-
Jedis jedis = getJedis();
-
Set<String> set = jedis.sdiff(keys);
-
returnJedis(jedis);
-
return set;
-
}
-
-
/**
-
* 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
-
* @param String newkey 新结果集的key
-
* @param String ... keys 比较的集合
-
* @return 新集合中的记录数
-
* **/
-
public long sdiffstore(String newkey, String... keys) {
-
Jedis jedis = getJedis();
-
long s = jedis.sdiffstore(newkey, keys);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set
-
* @param String ... keys
-
* @return 交集成员的集合
-
* **/
-
public Set<String> sinter(String... keys) {
-
Jedis jedis = getJedis();
-
Set<String> set = jedis.sinter(keys);
-
returnJedis(jedis);
-
return set;
-
}
-
-
/**
-
* 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
-
* @param String newkey 新结果集的key
-
* @param String ... keys 比较的集合
-
* @return 新集合中的记录数
-
* **/
-
public long sinterstore(String newkey, String... keys) {
-
Jedis jedis = getJedis();
-
long s = jedis.sinterstore(newkey, keys);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 确定一个给定的值是否存在
-
* @param String key
-
* @param String member 要判断的值
-
* @return 存在返回1,不存在返回0
-
* **/
-
public boolean sismember(String key, String member) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
boolean s = sjedis.sismember(key, member);
-
returnJedis(sjedis);
-
return s;
-
}
-
-
/**
-
* 返回集合中的所有成员
-
* @param String key
-
* @return 成员集合
-
* */
-
public Set<String> smembers(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
Set<String> set = sjedis.smembers(key);
-
returnJedis(sjedis);
-
return set;
-
}
-
-
public Set<
byte[]> smembers(
byte[] key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
Set<
byte[]> set = sjedis.smembers(key);
-
returnJedis(sjedis);
-
return set;
-
}
-
-
/**
-
* 将成员从源集合移出放入目标集合 <br/>
-
* 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0<br/>
-
* 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除
-
* @param String srckey 源集合
-
* @param String dstkey 目标集合
-
* @param String member 源集合中的成员
-
* @return 状态码,1成功,0失败
-
* */
-
public long smove(String srckey, String dstkey, String member) {
-
Jedis jedis = getJedis();
-
long s = jedis.smove(srckey, dstkey, member);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 从集合中删除成员
-
* @param String key
-
* @return 被删除的成员
-
* */
-
public String spop(String key) {
-
Jedis jedis = getJedis();
-
String s = jedis.spop(key);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 从集合中删除指定成员
-
* @param String key
-
* @param String member 要删除的成员
-
* @return 状态码,成功返回1,成员不存在返回0
-
* */
-
public long srem(String key, String member) {
-
Jedis jedis = getJedis();
-
long s = jedis.srem(key, member);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 合并多个集合并返回合并后的结果,合并后的结果集合并不保存<br/>
-
* @param String ... keys
-
* @return 合并后的结果集合
-
* @see sunionstore
-
* */
-
public Set<String> sunion(String... keys) {
-
Jedis jedis = getJedis();
-
Set<String> set = jedis.sunion(keys);
-
returnJedis(jedis);
-
return set;
-
}
-
-
/**
-
* 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖
-
* @param String newkey 新集合的key
-
* @param String ... keys 要合并的集合
-
* **/
-
public long sunionstore(String newkey, String... keys) {
-
Jedis jedis = getJedis();
-
long s = jedis.sunionstore(newkey, keys);
-
returnJedis(jedis);
-
return s;
-
}
-
}
-
-
//*******************************************SortSet*******************************************//
-
public
class SortSet {
-
-
/**
-
* 向集合中增加一条记录,如果这个值已存在,这个值对应的权重将被置为新的权重
-
* @param String key
-
* @param double score 权重
-
* @param String member 要加入的值,
-
* @return 状态码 1成功,0已存在member的值
-
* */
-
public long zadd(String key, double score, String member) {
-
Jedis jedis = getJedis();
-
long s = jedis.zadd(key, score, member);
-
returnJedis(jedis);
-
return s;
-
}
-
-
public long zadd(String key, Map<Double, String> scoreMembers) {
-
Jedis jedis = getJedis();
-
long s = jedis.zadd(key, scoreMembers);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 获取集合中元素的数量
-
* @param String key
-
* @return 如果返回0则集合不存在
-
* */
-
public long zcard(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
long len = sjedis.zcard(key);
-
returnJedis(sjedis);
-
return len;
-
}
-
-
/**
-
* 获取指定权重区间内集合的数量
-
* @param String key
-
* @param double min 最小排序位置
-
* @param double max 最大排序位置
-
* */
-
public long zcount(String key, double min, double max) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
long len = sjedis.zcount(key, min, max);
-
returnJedis(sjedis);
-
return len;
-
}
-
-
/**
-
* 获得set的长度
-
*
-
* @param key
-
* @return
-
*/
-
public long zlength(String key) {
-
long len =
0;
-
Set<String> set = zrange(key,
0, -
1);
-
len = set.size();
-
return len;
-
}
-
-
/**
-
* 权重增加给定值,如果给定的member已存在
-
* @param String key
-
* @param double score 要增的权重
-
* @param String member 要插入的值
-
* @return 增后的权重
-
* */
-
public double zincrby(String key, double score, String member) {
-
Jedis jedis = getJedis();
-
double s = jedis.zincrby(key, score, member);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 返回指定位置的集合元素,0为第一个元素,-1为最后一个元素
-
* @param String key
-
* @param int start 开始位置(包含)
-
* @param int end 结束位置(包含)
-
* @return Set<String>
-
* */
-
public Set<String> zrange(String key, int start, int end) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
Set<String> set = sjedis.zrange(key, start, end);
-
returnJedis(sjedis);
-
return set;
-
}
-
-
/**
-
* 返回指定权重区间的元素集合
-
* @param String key
-
* @param double min 上限权重
-
* @param double max 下限权重
-
* @return Set<String>
-
* */
-
public Set<String> zrangeByScore(String key, double min, double max) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
Set<String> set = sjedis.zrangeByScore(key, min, max);
-
returnJedis(sjedis);
-
return set;
-
}
-
-
/**
-
* 获取指定值在集合中的位置,集合排序从低到高
-
* @see zrevrank
-
* @param String key
-
* @param String member
-
* @return long 位置
-
* */
-
public long zrank(String key, String member) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
long index = sjedis.zrank(key, member);
-
returnJedis(sjedis);
-
return index;
-
}
-
-
/**
-
* 获取指定值在集合中的位置,集合排序从高到低
-
* @see zrank
-
* @param String key
-
* @param String member
-
* @return long 位置
-
* */
-
public long zrevrank(String key, String member) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
long index = sjedis.zrevrank(key, member);
-
returnJedis(sjedis);
-
return index;
-
}
-
-
/**
-
* 从集合中删除成员
-
* @param String key
-
* @param String member
-
* @return 返回1成功
-
* */
-
public long zrem(String key, String member) {
-
Jedis jedis = getJedis();
-
long s = jedis.zrem(key, member);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 删除
-
* @param key
-
* @return
-
*/
-
public long zrem(String key) {
-
Jedis jedis = getJedis();
-
long s = jedis.del(key);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 删除给定位置区间的元素
-
* @param String key
-
* @param int start 开始区间,从0开始(包含)
-
* @param int end 结束区间,-1为最后一个元素(包含)
-
* @return 删除的数量
-
* */
-
public long zremrangeByRank(String key, int start, int end) {
-
Jedis jedis = getJedis();
-
long s = jedis.zremrangeByRank(key, start, end);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 删除给定权重区间的元素
-
* @param String key
-
* @param double min 下限权重(包含)
-
* @param double max 上限权重(包含)
-
* @return 删除的数量
-
* */
-
public long zremrangeByScore(String key, double min, double max) {
-
Jedis jedis = getJedis();
-
long s = jedis.zremrangeByScore(key, min, max);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 获取给定区间的元素,原始按照权重由高到低排序
-
* @param String key
-
* @param int start
-
* @param int end
-
* @return Set<String>
-
* */
-
public Set<String> zrevrange(String key, int start, int end) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
Set<String> set = sjedis.zrevrange(key, start, end);
-
returnJedis(sjedis);
-
return set;
-
}
-
-
/**
-
* 获取给定值在集合中的权重
-
* @param String key
-
* @param memeber
-
* @return double 权重
-
* */
-
public double zscore(String key, String memebr) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
Double score = sjedis.zscore(key, memebr);
-
returnJedis(sjedis);
-
if (score !=
null)
-
return score;
-
return
0;
-
}
-
}
-
-
//*******************************************Hash*******************************************//
-
public
class Hash {
-
-
/**
-
* 从hash中删除指定的存储
-
* @param String key
-
* @param String fieid 存储的名字
-
* @return 状态码,1成功,0失败
-
* */
-
public long hdel(String key, String fieid) {
-
Jedis jedis = getJedis();
-
long s = jedis.hdel(key, fieid);
-
returnJedis(jedis);
-
return s;
-
}
-
-
public long hdel(String key) {
-
Jedis jedis = getJedis();
-
long s = jedis.del(key);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 测试hash中指定的存储是否存在
-
* @param String key
-
* @param String fieid 存储的名字
-
* @return 1存在,0不存在
-
* */
-
public boolean hexists(String key, String fieid) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
boolean s = sjedis.hexists(key, fieid);
-
returnJedis(sjedis);
-
return s;
-
}
-
-
/**
-
* 返回hash中指定存储位置的值
-
*
-
* @param String key
-
* @param String fieid 存储的名字
-
* @return 存储对应的值
-
* */
-
public String hget(String key, String fieid) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
String s = sjedis.hget(key, fieid);
-
returnJedis(sjedis);
-
return s;
-
}
-
-
public
byte[] hget(
byte[] key,
byte[] fieid) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
byte[] s = sjedis.hget(key, fieid);
-
returnJedis(sjedis);
-
return s;
-
}
-
-
/**
-
* 以Map的形式返回hash中的存储和值
-
* @param String key
-
* @return Map<Strinig,String>
-
* */
-
public Map<String, String> hgetAll(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
Map<String, String> map = sjedis.hgetAll(key);
-
returnJedis(sjedis);
-
return map;
-
}
-
-
/**
-
* 添加一个对应关系
-
* @param String key
-
* @param String fieid
-
* @param String value
-
* @return 状态码 1成功,0失败,fieid已存在将更新,也返回0
-
* **/
-
public long hset(String key, String fieid, String value) {
-
Jedis jedis = getJedis();
-
long s = jedis.hset(key, fieid, value);
-
returnJedis(jedis);
-
return s;
-
}
-
-
public long hset(String key, String fieid, byte[] value) {
-
Jedis jedis = getJedis();
-
long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 添加对应关系,只有在fieid不存在时才执行
-
* @param String key
-
* @param String fieid
-
* @param String value
-
* @return 状态码 1成功,0失败fieid已存
-
* **/
-
public long hsetnx(String key, String fieid, String value) {
-
Jedis jedis = getJedis();
-
long s = jedis.hsetnx(key, fieid, value);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 获取hash中value的集合
-
*
-
* @param String
-
* key
-
* @return List<String>
-
* */
-
public List<String> hvals(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
List<String> list = sjedis.hvals(key);
-
returnJedis(sjedis);
-
return list;
-
}
-
-
/**
-
* 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型
-
* @param String key
-
* @param String fieid 存储位置
-
* @param String long value 要增加的值,可以是负数
-
* @return 增加指定数字后,存储位置的值
-
* */
-
public long hincrby(String key, String fieid, long value) {
-
Jedis jedis = getJedis();
-
long s = jedis.hincrBy(key, fieid, value);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 返回指定hash中的所有存储名字,类似Map中的keySet方法
-
* @param String key
-
* @return Set<String> 存储名称的集合
-
* */
-
public Set<String> hkeys(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
Set<String> set = sjedis.hkeys(key);
-
returnJedis(sjedis);
-
return set;
-
}
-
-
/**
-
* 获取hash中存储的个数,类似Map中size方法
-
* @param String key
-
* @return long 存储的个数
-
* */
-
public long hlen(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
long len = sjedis.hlen(key);
-
returnJedis(sjedis);
-
return len;
-
}
-
-
/**
-
* 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
-
* @param String key
-
* @param String ... fieids 存储位置
-
* @return List<String>
-
* */
-
public List<String> hmget(String key, String... fieids) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
List<String> list = sjedis.hmget(key, fieids);
-
returnJedis(sjedis);
-
return list;
-
}
-
-
public List<
byte[]> hmget(
byte[] key,
byte[]... fieids) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
List<
byte[]> list = sjedis.hmget(key, fieids);
-
returnJedis(sjedis);
-
return list;
-
}
-
-
/**
-
* 添加对应关系,如果对应关系已存在,则覆盖
-
* @param Strin key
-
* @param Map <String,String> 对应关系
-
* @return 状态,成功返回OK
-
* */
-
public String hmset(String key, Map<String, String> map) {
-
Jedis jedis = getJedis();
-
String s = jedis.hmset(key, map);
-
returnJedis(jedis);
-
return s;
-
}
-
-
/**
-
* 添加对应关系,如果对应关系已存在,则覆盖
-
* @param Strin key
-
* @param Map <String,String> 对应关系
-
* @return 状态,成功返回OK
-
* */
-
public String hmset(byte[] key, Map<byte[], byte[]> map) {
-
Jedis jedis = getJedis();
-
String s = jedis.hmset(key, map);
-
returnJedis(jedis);
-
return s;
-
}
-
-
}
-
-
-
//*******************************************Strings*******************************************//
-
public
class Strings {
-
/**
-
* 根据key获取记录
-
* @param String key
-
* @return 值
-
* */
-
public String get(String key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
String value = sjedis.get(key);
-
returnJedis(sjedis);
-
return value;
-
}
-
-
/**
-
* 根据key获取记录
-
* @param byte[] key
-
* @return 值
-
* */
-
public
byte[] get(
byte[] key) {
-
//ShardedJedis sjedis = getShardedJedis();
-
Jedis sjedis = getJedis();
-
byte[] value = sjedis.get(key);
-
returnJedis(sjedis);
-
return value;
-
}
-
-
/**
-
* 添加有过期时间的记录
-
*
-
* @param String key
-
* @param int seconds 过期时间,以秒为单位
-
* @param String value
-
* @return String 操作状态
-
* */
-
public String setEx(String key, int seconds, String value) {
-
Jedis jedis = getJedis();
-
String str = jedis.setex(key, seconds, value);
-
returnJedis(jedis);
-
return str;
-
}
-
-
/**
-
* 添加有过期时间的记录
-
*
-
* @param String key
-
* @param int seconds 过期时间,以秒为单位
-
* @param String value
-
* @return String 操作状态
-
* */
-
public String setEx(byte[] key, int seconds, byte[] value) {
-
Jedis jedis = getJedis();
-
String str = jedis.setex(key, seconds, value);
-
returnJedis(jedis);
-
return str;
-
}
-
-
/**
-
* 添加一条记录,仅当给定的key不存在时才插入
-
* @param String key
-
* @param String value
-
* @return long 状态码,1插入成功且key不存在,0未插入,key存在
-
* */
-
public long setnx(String key, String value) {
-
Jedis jedis = getJedis();
-
long str = jedis.setnx(key, value);
-
returnJedis(jedis);
-
return str;
-
}
-
-
/**
-
* 添加记录,如果记录已存在将覆盖原有的value
-
* @param String key
-
* @param String value
-
* @return 状态码
-
* */
-
public String set(String key, String value) {
-
return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
-
}
-
-
/**
-
* 添加记录,如果记录已存在将覆盖原有的value
-
* @param String key
-
* @param String value
-
* @return 状态码
-
* */
-
public String set(String key, byte[] value) {
-
return set(SafeEncoder.encode(key), value);
-
}
-
-
/**
-
* 添加记录,如果记录已存在将覆盖原有的value
-
* @param byte[] key
-
* @param byte[] value
-
* @return 状态码
-
* */
-
public String set(byte[] key, byte[] value) {
-
Jedis jedis = getJedis();
-
String status = jedis.set(key, value);
-
returnJedis(jedis);
-
return status;
-
}
-