FileUtil只是读取文件内容类,用其他的也可以,能读取就好!
package com.yt.util;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.SortingParams;
import redis.clients.util.SafeEncoder;
/**
* reids工具类,使用的是线程池
* @author tyg
* @date 2017年6月22日上午11:59:26
*/
public class JedisUtils {
//默认缓存时间一个小时(单位:秒)
private static final int expire = 3600;
//连接池
private static JedisPool jedisPool;
/**
* 初始化数据
*/
static {
// 获取配置文件的内容
Properties p = FileUtil.getPropertiesValue("redis.properties");
// 地址
String hostName = p.getProperty("spring.redis.host");
// 端口
Integer port = Integer.valueOf(p.getProperty("spring.redis.port"));
// 密码
String pwd = p.getProperty("spring.redis.password");
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.valueOf(p.getProperty("spring.redis.pool.max-idle")));
config.setMinIdle(Integer.valueOf(p.getProperty("spring.redis.pool.min-idle")));
config.setMaxTotal(Integer.valueOf(p.getProperty("spring.redis.pool.max-total")));
config.setMaxWaitMillis(Long.valueOf(p.getProperty("spring.redis.pool.maxWaitMillis")));
config.setTestOnBorrow(Boolean.valueOf(p.getProperty("spring.redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(p.getProperty("spring.redis.pool.testOnReturn")));
jedisPool = new JedisPool(config, hostName, port, 5000, pwd);
}
/**
* 从jedis连接池中获取获取jedis对象
* @return
* @return Jedis
* @author tyg
* @date 2017年6月22日上午11:59:06
*/
public static Jedis getJedis() {
return jedisPool.getResource();
}
/**
* 将redis连接退货到连接池
* @return void
* @author tyg
* @date 2017年6月16日下午3:45:45
*/
@SuppressWarnings("deprecation")
public static void returnResource(Jedis jedis) {
try{
if(jedis != null){
jedisPool.returnResourceObject(jedis);
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 释放redis对象
* @return void
* @author tyg
* @date 2017年6月16日下午3:45:45
*/
@SuppressWarnings("deprecation")
public static void returnBrokenResource(Jedis jedis) {
if(jedis != null && jedisPool != null){
jedisPool.returnBrokenResource(jedis);
}
}
/**
* 设置过期时间
* @param key
* @param seconds 秒
* @return void
* @author tyg
* @throws Exception
* @date 2017年6月22日下午12:00:28
*/
public static void expire(String key, int seconds) {
if (seconds <= 0) {
return;
}
Jedis jedis = getJedis();
try {
jedis.expire(key, seconds);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
}
/**
* 设置默认过期时间(一个小时)
* @param key
* @return void
* @author tyg
* @throws Exception
* @date 2017年6月22日下午12:01:02
*/
public static void expire(String key) {
expire(key, expire);
}
/* *******************************************Keys*******************************************/
/**
* 清空所有key
* @return
* @return String
* @author tyg
* @throws Exception
* @date 2017年6月22日下午12:01:58
*/
public static String flushAll() {
String stata = null;
Jedis jedis = getJedis();
try{
stata = jedis.flushAll();
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return stata;
}
/**
* 更改key
* @param oldkey
* @param newkey
* @return
* @return 状态码
* @author tyg
* @date 2017年6月22日下午12:02:06
*/
public static String rename(String oldkey, String newkey) {
return rename(SafeEncoder.encode(oldkey),
SafeEncoder.encode(newkey));
}
/**
* 更改key,仅当新key不存在时才执行
* @param oldkey
* @param newkey
* @return
* @return 状态码
* @author tyg
* @date 2017年6月22日下午1:52:08
*/
public static long renamenx(String oldkey, String newkey) {
long status = 0;
Jedis jedis = getJedis();
try {
status = jedis.renamenx(oldkey, newkey);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return status;
}
/**
* 更改key
* @param oldkey
* @param newkey
* @return
* @return 状态码
* @author tyg
* @date 2017年6月22日下午1:52:20
*/
public static String rename(byte[] oldkey, byte[] newkey) {
String status = null;
Jedis jedis = getJedis();
try {
status = jedis.rename(oldkey, newkey);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return status;
}
/**
* 设置key的过期时间,以秒为单位
* @param key
* @param seconds 时间,以秒为单位
* @return
* @return 影响的记录数
* @author tyg
* @date 2017年6月22日下午1:52:56
*/
public static long expired(String key, int seconds) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.expire(key, seconds);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。
* @param key
* @param timestamp 时间,以秒为单位
* @return
* @return 影响的记录数
* @author tyg
* @date 2017年6月22日下午1:53:24
*/
public static long expireAt(String key, long timestamp) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.expireAt(key, timestamp);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 查询key的过期时间
* @param key
* @return
* @return 以秒为单位的时间表示
* @author tyg
* @date 2017年6月22日下午1:53:52
*/
public static long ttl(String key) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.ttl(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/**
* 取消对key过期时间的设置
* @param key
* @return
* @return 影响的记录数
* @author tyg
* @date 2017年6月22日下午1:54:14
*/
public static long persist(String key) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.persist(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 删除keys对应的记录,可以是多个key
* @param keys
* @return
* @return 删除的记录数
* @author tyg
* @date 2017年6月22日下午1:54:24
*/
public static long del(String... keys) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.del(keys);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 删除keys对应的记录,可以是多个key
* @param keys
* @return
* @return 删除的记录数
* @author tyg
* @date 2017年6月22日下午1:54:36
*/
public static long del(byte[]... keys) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.del(keys);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 判断key是否存在
* @param key
* @return
* @return boolean
* @author tyg
* @date 2017年6月22日下午1:54:50
*/
public static boolean exists(String key) {
boolean exis = false;
Jedis jedis = getJedis();
try {
exis = jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return exis;
}
/**
* 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法
* @param key
* @return
* @return List<String> 集合的全部记录
* @author tyg
* @date 2017年6月22日下午1:55:01
*/
public static List<String> sort(String key) {
List<String> list = null;
Jedis jedis = getJedis();
try {
list = jedis.sort(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return list;
}
/**
* 对List,Set,SortSet进行排序或limit
* @param key
* @param parame 定义排序类型或limit的起止位置.
* @return
* @return List<String> 全部或部分记录
* @author tyg
* @date 2017年6月22日下午1:55:17
*/
public static List<String> sort(String key, SortingParams parame) {
List<String> list = null;
Jedis jedis = getJedis();
try {
list = jedis.sort(key, parame);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return list;
}
/**
* 返回指定key存储的类型
* @param key
* @return
* @return String string|list|set|zset|hash
* @author tyg
* @date 2017年6月22日下午1:55:43
*/
public static String type(String key) {
String type = null;
Jedis jedis = getJedis();
try {
type = jedis.type(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return type;
}
/**
* 查找所有匹配给定的模式的键
* @param pattern key的表达式,*表示多个,?表示一个
* @return
* @return Set<String>
* @author tyg
* @date 2017年6月22日下午1:55:56
*/
public static Set<String> keys(String pattern) {
Set<String> set = null;
Jedis jedis = getJedis();
try {
set = jedis.keys(pattern);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/* ******************************************Sets*******************************************/
/**
* 向Set添加一条记录,如果member已存在返回0,否则返回1
* @param key
* @param member
* @return
* @return long 操作码,0或1
* @author tyg
* @date 2017年6月22日下午1:56:24
*/
public static long sadd(String key, String member) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.sadd(key, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 向Set添加一条记录,如果member已存在返回0,否则返回1
* @param key
* @param member
* @return
* @return long 操作码,0或1
* @author tyg
* @date 2017年6月22日下午1:56:24
*/
public static long sadd(byte[] key, byte[] member) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.sadd(key, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 获取给定key中元素个数
* @param key
* @return
* @return long 元素个数
* @author tyg
* @date 2017年6月22日下午1:57:01
*/
public static long scard(String key) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.scard(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/**
* 返回从第一组和所有的给定集合之间的差异的成员
* @param keys
* @return
* @return Set<String> 差异的成员集合
* @author tyg
* @date 2017年6月22日下午1:57:12
*/
public static Set<String> sdiff(String... keys) {
Set<String> set = null;
Jedis jedis = getJedis();
try {
set = jedis.sdiff(keys);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/**
* 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖
* @param newkey 新结果集的key
* @param keys 比较的集合
* @return
* @return long 新集合中的记录数
* @author tyg
* @date 2017年6月22日下午1:57:30
*/
public static long sdiffstore(String newkey, String... keys) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.sdiffstore(newkey, keys);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set
* @param keys
* @return
* @return Set<String> 交集成员的集合
* @author tyg
* @date 2017年6月22日下午1:57:59
*/
public static Set<String> sinter(String... keys) {
Set<String> set = null;
Jedis jedis = getJedis();
try {
set = jedis.sinter(keys);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/**
* 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖
* @param newkey 新结果集的key
* @param keys 比较的集合
* @return
* @return long 新集合中的记录数
* @author tyg
* @date 2017年6月22日下午1:58:11
*/
public static long sinterstore(String newkey, String... keys) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.sinterstore(newkey, keys);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 合并多个集合并返回合并后的结果,合并后的结果集合并不保存
* @param keys
* @return
* @return Set<String> 合并后的结果集合
* @author tyg
* @date 2017年6月22日下午2:00:24
*/
public static Set<String> sunion(String... keys) {
Set<String> set = null;
Jedis jedis = getJedis();
try {
set = jedis.sunion(keys);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/**
* 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖
* @param newkey 新集合的key
* @param keys 要合并的集合
* @return
* @return long 新集合的数量
* @author tyg
* @date 2017年6月22日下午2:00:37
*/
public static long sunionstore(String newkey, String... keys) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.sunionstore(newkey, keys);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 确定一个给定的值是否存在
* @param key
* @param member 要判断的值
* @return
* @return boolean 存在返回1,不存在返回0
* @author tyg
* @date 2017年6月22日下午1:58:42
*/
public static boolean sismember(String key, String member) {
boolean s = false;
Jedis jedis = getJedis();
try {
s = jedis.sismember(key, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 返回集合中的所有成员
* @param key
* @return
* @return Set<String> 成员集合
* @author tyg
* @date 2017年6月22日下午1:59:01
*/
public static Set<String> smembers(String key) {
Set<String> set = null;
Jedis jedis = getJedis();
try {
set = jedis.smembers(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/**
* 返回集合中的所有成员
* @param key
* @return
* @return Set<String> 成员集合
* @author tyg
* @date 2017年6月22日下午1:59:01
*/
public static Set<byte[]> smembers(byte[] key) {
Set<byte[]> set = null;
Jedis jedis = getJedis();
try {
set = jedis.smembers(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/**
* 将成员从源集合移出放入目标集合
* 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0
* 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除
* @param srckey 源集合
* @param dstkey 目标集合
* @param member 源集合中的成员
* @return
* @return long 状态码,1成功,0失败
* @author tyg
* @date 2017年6月22日下午1:59:20
*/
public static long smove(String srckey, String dstkey, String member) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.smove(srckey, dstkey, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 从集合中删除成员
* @param key
* @return
* @return String 被删除的成员
* @author tyg
* @date 2017年6月22日下午1:59:58
*/
public static String spop(String key) {
String s = null;
Jedis jedis = getJedis();
try {
s = jedis.spop(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 从集合中删除指定成员
* @param key
* @param member 要删除的成员
* @return
* @return long 状态码,成功返回1,成员不存在返回0
* @author tyg
* @date 2017年6月22日下午2:00:09
*/
public static long srem(String key, String member) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.srem(key, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/* ******************************************SortSet*******************************************/
/**
* 向集合中增加一条记录,如果这个值已存在,这个值对应的权重将被置为新的权重
* @param key
* @param score 权重
* @param member 要加入的值
* @return
* @return long 状态码 1成功,0已存在member的值
* @author tyg
* @date 2017年6月22日下午2:01:36
*/
public static long zadd(String key, double score, String member) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.zadd(key, score, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 向集合中增加多条条记录
* @param key
* @param scoreMembers
* @return
* @return long
* @author tyg
* @date 2017年6月22日下午2:02:07
*/
public static long zadd(String key, Map<String, Double> scoreMembers) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.zadd(key, scoreMembers);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 获取集合中元素的数量
* @param key
* @return
* @return long 如果返回0则集合不存在
* @author tyg
* @date 2017年6月22日下午2:03:35
*/
public static long zcard(String key) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.zcard(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/**
* 获取指定权重区间内集合的数量
* @param key
* @param min 最小排序位置
* @param max 最大排序位置
* @return
* @return long
* @author tyg
* @date 2017年6月22日下午2:03:45
*/
public static long zcount(String key, double min, double max) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.zcount(key, min, max);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/**
* 获得set的长度
* @param key
* @return
* @return long set的长度
* @author tyg
* @date 2017年6月22日下午2:04:09
*/
public static long zlength(String key) {
long len = 0;
Set<String> set = zrange(key, 0, -1);
len = set == null ? len : set.size();
return len;
}
/**
* 权重增加给定值,如果给定的member已存在
* @param key
* @param score 要增的权重
* @param member 要插入的值
* @return
* @return double 增后的权重
* @author tyg
* @date 2017年6月22日下午2:04:24
*/
public static double zincrby(String key, double score, String member) {
double s = 0;
Jedis jedis = getJedis();
try {
s = jedis.zincrby(key, score, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 返回指定位置的集合元素,0为第一个元素,-1为最后一个元素
* @param key
* @param start 开始位置(包含)
* @param end 结束位置(包含)
* @return
* @return Set<String> 指定的元素集合
* @author tyg
* @date 2017年6月22日下午2:04:54
*/
public static Set<String> zrange(String key, int start, int end) {
Set<String> set = null;
Jedis jedis = getJedis();
try {
set = jedis.zrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/**
* 返回指定权重区间的元素集合
* @param key
* @param min 上限权重
* @param max 下限权重
* @return
* @return Set<String> 指定权重区间的元素集合
* @author tyg
* @date 2017年6月22日下午2:06:22
*/
public static Set<String> zrangeByScore(String key, double min, double max) {
Set<String> set = null;
Jedis jedis = getJedis();
try {
set = jedis.zrangeByScore(key, min, max);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/**
* 获取指定值在集合中的位置,集合排序从低到高
* @param key
* @param member
* @return
* @return long 位置
* @author tyg
* @date 2017年6月22日下午2:07:03
*/
public static long zrank(String key, String member) {
long index = 0;
Jedis jedis = getJedis();
try {
index = jedis.zrank(key, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return index;
}
/**
* 获取指定值在集合中的位置,集合排序从高到低
* @param key
* @param member
* @return
* @return long 位置
* @author tyg
* @date 2017年6月22日下午2:07:49
*/
public static long zrevrank(String key, String member) {
long index = 0;
Jedis jedis = getJedis();
try {
index = jedis.zrevrank(key, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return index;
}
/**
* 从集合中删除成员
* @param key
* @param member
* @return
* @return long 返回1成功
* @author tyg
* @date 2017年6月22日下午2:08:18
*/
public static long zrem(String key, String member) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.zrem(key, member);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 删除
* @param key
* @return
* @return long
* @author tyg
* @date 2017年6月22日下午2:08:30
*/
public static long del(String key) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 删除给定位置区间的元素
* @param key
* @param start 开始区间,从0开始(包含)
* @param end 结束区间,-1为最后一个元素(包含)
* @return
* @return long 删除的数量
* @author tyg
* @date 2017年6月22日下午2:09:12
*/
public static long zremrangeByRank(String key, int start, int end) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.zremrangeByRank(key, start, end);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 删除给定权重区间的元素
* @param key
* @param min 下限权重(包含)
* @param max 上限权重(包含)
* @return
* @return long 删除的数量
* @author tyg
* @date 2017年6月22日下午2:09:33
*/
public static long zremrangeByScore(String key, double min, double max) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.zremrangeByScore(key, min, max);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 获取给定区间的元素,原始按照权重由高到低排序
* @param key
* @param start
* @param end
* @return
* @return Set<String> 排序后的元素集合
* @author tyg
* @date 2017年6月22日下午2:09:52
*/
public static Set<String> zrevrange(String key, int start, int end) {
Set<String> set = null;
Jedis jedis = getJedis();
try {
set = jedis.zrevrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/**
* 获取给定值在集合中的权重
* @param key
* @param memebr
* @return
* @return double 权重
* @author tyg
* @date 2017年6月22日下午2:10:45
*/
public static double zscore(String key, String memebr) {
Double score = null;
Jedis jedis = getJedis();
try {
score = jedis.zscore(key, memebr);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return score == null ? 0 : score;
}
/* ******************************************Hash*******************************************/
/**
* 从hash中删除指定的存储
* @param key
* @param hashKey 存储的名字
* @return
* @return long 状态码,1成功,0失败
* @author tyg
* @date 2017年6月22日下午2:11:19
*/
public static long hdel(String key, String hashKey) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.hdel(key, hashKey);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 从hash中删除key
* @param key
* @return
* @return long 状态码,1成功,0失败
* @author tyg
* @date 2017年6月22日下午2:11:19
*/
public static long hdel(String key) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 测试hash中指定的存储是否存在
* @param key
* @param hashKey 存储的名字
* @return
* @return boolean 1存在,0不存在
* @author tyg
* @date 2017年6月22日下午2:12:35
*/
public static boolean hexists(String key, String hashKey) {
boolean s = false;
Jedis jedis = getJedis();
try {
s = jedis.hexists(key, hashKey);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 返回hash中指定存储位置的值
* @param key
* @param hashKey 存储的名字
* @return
* @return String 存储对应的值
* @author tyg
* @date 2017年6月22日下午2:14:06
*/
public static String hget(String key, String hashKey) {
String s = null;
Jedis jedis = getJedis();
try {
s = jedis.hget(key, hashKey);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 返回hash中指定存储位置的值
* @param key
* @param hashKey 存储的名字
* @return
* @return String 存储对应的值
* @author tyg
* @date 2017年6月22日下午2:14:06
*/
public static byte[] hget(byte[] key, byte[] hashKey) {
byte[] s = null;
Jedis jedis = getJedis();
try {
s = jedis.hget(key, hashKey);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 以Map的形式返回hash中的存储和值
* @param key
* @return
* @return Map<String,String> 所有的元素
* @author tyg
* @date 2017年6月22日下午2:14:52
*/
public static Map<String, String> hgetAll(String key) {
Map<String, String> map = null;
Jedis jedis = getJedis();
try {
map = jedis.hgetAll(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return map;
}
/**
* 添加元素
* @param key
* @param hashKey
* @param value
* @return
* @return long 状态码 1成功,0失败,hashKey已存在将更新,也返回0
* @author tyg
* @date 2017年6月22日下午2:15:21
*/
public static long hset(String key, String hashKey, String value) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.hset(key, hashKey, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 添加元素
* @param key
* @param hashKey
* @param value
* @return
* @return long 状态码 1成功,0失败,hashKey已存在将更新,也返回0
* @author tyg
* @date 2017年6月22日下午2:15:21
*/
public static long hset(String key, String fieid, byte[] value) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 添加对应关系,只有在fieid不存在时才执行
* @param key
* @param hashKey
* @param value
* @return
* @return long 状态码 1成功,0失败fieid已存
* @author tyg
* @date 2017年6月22日下午2:16:31
*/
public static long hsetnx(String key, String hashKey, String value) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.hsetnx(key, hashKey, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 获取hash中value的集合
* @param key
* @return
* @return List<String> value的集合
* @author tyg
* @date 2017年6月22日下午2:16:55
*/
public static List<String> hvals(String key) {
List<String> list = null;
Jedis jedis = getJedis();
try {
list = jedis.hvals(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return list;
}
/**
* 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型
* @param key
* @param hashKey 存储位置
* @param value 要增加的值,可以是负数
* @return
* @return long 增加指定数字后,存储位置的值
* @author tyg
* @date 2017年6月22日下午2:18:17
*/
public static long hincrby(String key, String hashKey, long value) {
long s = 0;
Jedis jedis = getJedis();
try {
s = jedis.hincrBy(key, hashKey, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 返回指定hash中的所有存储名字,类似Map中的keySet方法
* @param key
* @return
* @return Set<String> 存储名称的集合
* @author tyg
* @date 2017年6月22日下午2:18:47
*/
public static Set<String> hkeys(String key) {
Set<String> set = null;
Jedis jedis = getJedis();
try {
set = jedis.hkeys(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return set;
}
/**
* 获取hash中存储的个数,类似Map中size方法
* @param key
* @return
* @return long 存储的个数
* @author tyg
* @date 2017年6月22日下午2:19:00
*/
public static long hlen(String key) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.hlen(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/**
* 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
* @param key
* @param hashKey 存储位置
* @return
* @return List<String>
* @author tyg
* @date 2017年6月22日下午2:19:11
*/
public static List<String> hmget(String key, String... hashKey) {
List<String> list = null;
Jedis jedis = getJedis();
try {
list = jedis.hmget(key, hashKey);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return list;
}
/**
* 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
* @param key
* @param hashKey 存储位置
* @return
* @return List<String>
* @author tyg
* @date 2017年6月22日下午2:19:11
*/
public static List<byte[]> hmget(byte[] key, byte[]... hashKey) {
List<byte[]> list = null;
Jedis jedis = getJedis();
try {
list = jedis.hmget(key, hashKey);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return list;
}
/**
* 添加对应关系,如果对应关系已存在,则覆盖
* @param key
* @param map 对应关系
* @return
* @return String 状态,成功返回OK
* @author tyg
* @date 2017年6月22日下午2:20:07
*/
public static String hmset(String key, Map<String, String> map) {
String s = null;
Jedis jedis = getJedis();
try {
s = jedis.hmset(key, map);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/**
* 添加对应关系,如果对应关系已存在,则覆盖
* @param key
* @param map 对应关系
* @return
* @return String 状态,成功返回OK
* @author tyg
* @date 2017年6月22日下午2:20:07
*/
public static String hmset(byte[] key, Map<byte[], byte[]> map) {
String s = null;
Jedis jedis = getJedis();
try {
s = jedis.hmset(key, map);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return s;
}
/* *******************************************Strings*******************************************/
/**
* 根据key获取记录
* @param key
* @return
* @return String
* @author tyg
* @date 2017年6月22日下午2:21:15
*/
public static String get(String key) {
String value = null;
Jedis jedis = getJedis();
try {
value = jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return value;
}
/**
* 根据key获取记录
* @param key
* @return
* @return byte[]
* @author tyg
* @date 2017年6月22日下午2:21:22
*/
public static byte[] get(byte[] key) {
byte[] value = null;
Jedis jedis = getJedis();
try {
value = jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return value;
}
/**
* 添加有过期时间的记录
* @param key
* @param seconds 过期时间,以秒为单位
* @param value
* @return
* @return String 操作状态
* @author tyg
* @date 2017年6月22日下午2:21:29
*/
public static String setEx(String key, int seconds, String value) {
String str = null;
Jedis jedis = getJedis();
try {
str = jedis.setex(key, seconds, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return str;
}
/**
* 添加有过期时间的记录
* @param key
* @param seconds 过期时间,以秒为单位
* @param value
* @return
* @return String 操作状态
* @author tyg
* @date 2017年6月22日下午2:21:44
*/
public static String setEx(byte[] key, int seconds, byte[] value) {
String str = null;
Jedis jedis = getJedis();
try {
str = jedis.setex(key, seconds, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return str;
}
/**
* 当且仅当 key 不存在,将 key 的值设为 value ,并返回1;若给定的 key 已经存在,则 SETNX 不做任何动作,并返回0。
* @param key
* @param value
* @return
* @return long 状态码,1插入成功且key不存在,0未插入,key存在
* @author tyg
* @date 2017年6月22日下午2:21:57
*/
public static long setnx(String key, String value) {
long str = 0;
Jedis jedis = getJedis();
try {
str = jedis.setnx(key, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return str;
}
/**
* 添加记录,如果记录已存在将覆盖原有的value
* @param key
* @param value
* @return
* @return String 状态码
* @author tyg
* @date 2017年6月22日下午2:22:10
*/
public static String set(String key, String value) {
String status = null;
Jedis jedis = getJedis();
try {
status = jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
returnResource(jedis);
}
return status;
}
/**
* 添加记录,如果记录已存在将覆盖原有的value
* @param key
* @param value
* @return
* @return String 状态码
* @author tyg
* @date 2017年6月22日下午2:22:20
*/
public static String set(String key, byte[] value) {
return set(SafeEncoder.encode(key), value);
}
/**
* 添加记录,如果记录已存在将覆盖原有的value
* @param key
* @param value
* @return
* @return String 状态码
* @author tyg
* @date 2017年6月22日下午2:23:12
*/
public static String set(byte[] key, byte[] value) {
String status = null;
Jedis jedis = getJedis();
try {
status = jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return status;
}
/**
* 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据
* 例:String str1="123456789"
* 对str1操作后setRange(key,4,0000),str1="123400009";
* @param key
* @param offset
* @param value
* @return
* @return long value的长度
* @author tyg
* @date 2017年6月22日下午2:24:15
*/
public static long setRange(String key, long offset, String value) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.setrange(key, offset, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/**
* 在指定的key中追加value
* @param key
* @param value
* @return
* @return long 追加后value的长度
* @author tyg
* @date 2017年6月22日下午2:24:44
*/
public static long append(String key, String value) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.append(key, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/**
* 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用
* @param key
* @param number 要减去的值
* @return
* @return long 减指定值后的值
* @author tyg
* @date 2017年6月22日下午2:24:59
*/
public static long decrBy(String key, long number) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.decrBy(key, number);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/**
* 可以作为获取唯一id的方法,将key对应的value加上指定的值,只有value可以转为数字时该方法才可用
* @param key
* @param number 要加入的值
* @return
* @return long 相加后的值
* @author tyg
* @date 2017年6月22日下午2:25:22
*/
public static long incrBy(String key, long number) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.incrBy(key, number);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/**
* 对指定key对应的value进行截取
* @param key
* @param startOffset 开始位置(包含)
* @param endOffset 结束位置(包含)
* @return
* @return String 截取的值
* @author tyg
* @date 2017年6月22日下午2:25:59
*/
public static String getrange(String key, long startOffset, long endOffset) {
String value = null;
Jedis jedis = getJedis();
try {
value = jedis.getrange(key, startOffset, endOffset);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return value;
}
/**
* 获取并设置指定key对应的value
* 如果key存在返回之前的value,否则返回null
* @param key
* @param value
* @return
* @return String 原始value或null
* @author tyg
* @date 2017年6月22日下午2:26:18
*/
public static String getSet(String key, String value) {
String str = null;
Jedis jedis = getJedis();
try {
str = jedis.getSet(key, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return str;
}
/**
* 批量获取记录,如果指定的key不存在返回List的对应位置将是null
* @param keys
* @return
* @return List<String> 值的集合
* @author tyg
* @date 2017年6月22日下午2:26:34
*/
public static List<String> mget(String... keys) {
List<String> str = null;
Jedis jedis = getJedis();
try {
str = jedis.mget(keys);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return str;
}
/**
* 批量存储记录
* @param keysvalues 例:keysvalues="key1","value1","key2","value2";
* @return
* @return String 状态码
* @author tyg
* @date 2017年6月22日下午2:26:50
*/
public static String mset(String... keysvalues) {
String str = null;
Jedis jedis = getJedis();
try {
str = jedis.mset(keysvalues);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return str;
}
/**
* 获取key对应的值的长度
* @param key
* @return
* @return long value值得长度
* @author tyg
* @date 2017年6月22日下午2:27:27
*/
public static long strlen(String key) {
long len = 0;
Jedis jedis = getJedis();
try {
len = jedis.strlen(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return len;
}
/* *******************************************Lists*******************************************/
/**
* List长度
* @param key
* @return
* @return long 长度
* @author tyg
* @date 2017年6月22日下午2:27:43
*/
public static long llen(String key) {
return llen(SafeEncoder.encode(key));
}
/**
* List长度
* @param key
* @return
* @return long 长度
* @author tyg
* @date 2017年6月22日下午2:27:43
*/
public static long llen(byte[] key) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.llen(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 覆盖操作,将覆盖List中指定位置的值
* @param key
* @param index 位置
* @param value 值
* @return
* @return String 状态码
* @author tyg
* @date 2017年6月22日下午2:28:05
*/
public static String lset(byte[] key, int index, byte[] value) {
String status = null;
Jedis jedis = getJedis();
try {
status = jedis.lset(key, index, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return status;
}
/**
* 覆盖操作,将覆盖List中指定位置的值
* @param key
* @param index 位置
* @param value 值
* @return
* @return String 状态码
* @author tyg
* @date 2017年6月22日下午2:28:30
*/
public static String lset(String key, int index, String value) {
return lset(SafeEncoder.encode(key), index,
SafeEncoder.encode(value));
}
/**
* 在value的相对位置插入记录
* @param key
* @param where
* @param pivot 前面插入或后面插入
* @param value 插入的内容
* @return
* @return long 记录总数
* @author tyg
* @date 2017年6月22日下午2:28:44
*/
public static long linsert(String key, LIST_POSITION where, String pivot,
String value) {
return linsert(SafeEncoder.encode(key), where,
SafeEncoder.encode(pivot), SafeEncoder.encode(value));
}
/**
* 在指定位置插入记录
* @param key
* @param where
* @param pivot 相对位置的内容
* @param value 插入的内容
* @return
* @return long 记录总数
* @author tyg
* @date 2017年6月22日下午2:29:21
*/
public static long linsert(byte[] key, LIST_POSITION where, byte[] pivot, byte[] value) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.linsert(key, where, pivot, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 获取List中指定位置的值
* @param key
* @param index 位置
* @return
* @return String 值
* @author tyg
* @date 2017年6月22日下午2:29:47
*/
public static String lindex(String key, int index) {
return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
}
/**
* 获取List中指定位置的值
* @param key
* @param index 位置
* @return
* @return byte[] 值
* @author tyg
* @date 2017年6月22日下午2:30:02
*/
public static byte[] lindex(byte[] key, int index) {
byte[] value = null;
Jedis jedis = getJedis();
try {
value = jedis.lindex(key, index);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return value;
}
/**
* 将List中的第一条记录移出List
* @param key
* @return
* @return String 移出的记录
* @author tyg
* @date 2017年6月22日下午2:30:19
*/
public static String lpop(String key) {
return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
}
/**
* 将List中的第一条记录移出List
* @param key
* @return
* @return String 移出的记录
* @author tyg
* @date 2017年6月22日下午2:30:19
*/
public static byte[] lpop(byte[] key) {
byte[] value = null;
Jedis jedis = getJedis();
try {
value = jedis.lpop(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return value;
}
/**
* 将List中的最一条记录移出List
* @param key
* @return
* @return String 移出的记录
* @author tyg
* @date 2017年6月22日下午2:30:19
*/
public static String rpop(String key) {
String value = null;
Jedis jedis = getJedis();
try {
value = jedis.rpop(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return value;
}
/**
* 将List中的最一条记录移出List
* @param key
* @return
* @return String 移出的记录
* @author tyg
* @date 2017年6月22日下午2:30:19
*/
public static byte[] rpop(byte[] key) {
byte[] value = null;
Jedis jedis = getJedis();
try {
value = jedis.rpop(key);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return value;
}
/**
* 向List尾部追加记录
* @param key
* @param value
* @return
* @return long 记录总数
* @author tyg
* @date 2017年6月22日下午2:31:33
*/
public static long lpush(String key, String value) {
return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
}
/**
* 向List头部追加记录
* @param key
* @param value
* @return
* @return long 记录总数
* @author tyg
* @date 2017年6月22日下午2:32:12
*/
public static long rpush(String key, String value) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.rpush(key, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 向List尾部追加记录
* @param key
* @param value
* @return
* @return long 记录总数
* @author tyg
* @date 2017年6月22日下午2:32:30
*/
public static long rpush(byte[] key, byte[] value) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.rpush(key, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 向List头部中追加记录
* @param key
* @param value
* @return
* @return long 记录总数
* @author tyg
* @date 2017年6月22日下午2:33:13
*/
public static long lpush(byte[] key, byte[] value) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.lpush(key, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 获取指定范围的记录,可以做为分页使用
* @param key
* @param start
* @param end
* @return
* @return List<String>
* @author tyg
* @date 2017年6月22日下午2:33:38
*/
public static List<String> lrange(String key, long start, long end) {
List<String> list = null;
Jedis jedis = getJedis();
try {
list = jedis.lrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return list;
}
/**
* 获取指定范围的记录,可以做为分页使用
* @param key
* @param start
* @param end 如果为负数,则尾部开始计算
* @return
* @return List<byte[]>
* @author tyg
* @date 2017年6月22日下午2:33:52
*/
public static List<byte[]> lrange(byte[] key, int start, int end) {
List<byte[]> list = null;
Jedis jedis = getJedis();
try {
list = jedis.lrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return list;
}
/**
* 删除List中c条记录,被删除的记录值为value
* @param key
* @param c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
* @param value 要匹配的值
* @return
* @return long 删除后的List中的记录数
* @author tyg
* @date 2017年6月22日下午2:34:09
*/
public static long lrem(byte[] key, int c, byte[] value) {
long count = 0;
Jedis jedis = getJedis();
try {
count = jedis.lrem(key, c, value);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return count;
}
/**
* 删除List中c条记录,被删除的记录值为value
* @param key
* @param c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
* @param value 要匹配的值
* @return
* @return long 删除后的List中的记录数
* @author tyg
* @date 2017年6月22日下午2:34:28
*/
public static long lrem(String key, int c, String value) {
return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
}
/**
* 算是删除吧,只保留start与end之间的记录
* @param key
* @param start 记录的开始位置(0表示第一条记录)
* @param end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
* @return
* @return String 执行状态码
* @author tyg
* @date 2017年6月22日下午2:34:46
*/
public static String ltrim(byte[] key, int start, int end) {
String str = null;
Jedis jedis = getJedis();
try {
str = jedis.ltrim(key, start, end);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
returnResource(jedis);
}
return str;
}
/**
* 算是删除吧,只保留start与end之间的记录
* @param key
* @param start 记录的开始位置(0表示第一条记录)
* @param end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
* @return
* @return String 执行状态码
* @author tyg
* @date 2017年6月22日下午2:35:01
*/
public static String ltrim(String key, int start, int end) {
return ltrim(SafeEncoder.encode(key), start, end);
}
/**
* 流水式操作
* @return
* @return Pipeline
* @author tyg
* @date 2017年6月16日下午4:27:25
*/
public static Pipeline pipelined() {
Pipeline pi = null;
Jedis jedis = getJedis();
try {
jedis.pipelined();
} catch (Exception e) {
returnBrokenResource(jedis);
e.printStackTrace();
throw e;
} finally {
//返还到连接池
returnResource(jedis);
}
return pi;
}
/**
* 测试main方法
* @param args
* @return void
* @author tyg
* @date 2017年6月22日下午3:57:27
*/
public static void main(String[] args) {
JedisUtils.set("key", "value");
// JedisUtil.del("key");
//清空所有key
// JedisUtils.flushAll();
// JedisUtils.setTest("key2", "value2");
System.out.println(JedisUtils.get("key")+"======="+JedisUtils.get("key2"));
System.out.println("存在活动的数量:"+jedisPool.getNumActive());
System.out.println("存在闲置的数量:"+jedisPool.getNumIdle());
System.out.println("存在等待的数量:"+jedisPool.getNumWaiters());
System.out.println("平均等待时间(毫秒):"+jedisPool.getMeanBorrowWaitTimeMillis());
System.out.println("最大等待时间(毫秒):"+jedisPool.getMaxBorrowWaitTimeMillis());
}
/**
* 测试
* @param key
* @param value
* @return
* @return String
* @author tyg
* @date 2017年6月22日下午4:46:58
*/
public static String setTest(String key, String value) {
String status = null;
Jedis jedis = getJedis();
returnResource(jedis);
status = jedis.set(key, value);
return status;
}
}