package terminal.util.redisUtil;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.util.SafeEncoder;
/**
* @author Mr.Ma
* @version crateTime:2015-08-11
* Class Explain:JedisUtil
*/
public class JedisUtil {
private static final JedisUtil redis = new JedisUtil();
/**
* 缓存生存时间
*/
private final int expire = 60000;
/** 操作Key的方法 */
public static JedisUtil.Keys KEYS = new JedisUtil().new Keys();
/** 对存储结构为String类型的操作 */
public static JedisUtil.Strings STRINGS = new JedisUtil().new Strings();
/** 对存储结构为List类型的操作 */
public static JedisUtil.Lists LISTS = new JedisUtil().new Lists();
/** 对存储结构为Set类型的操作 */
public static JedisUtil.Sets SETS = new JedisUtil().new Sets();
/** 对存储结构为HashMap类型的操作 */
public static JedisUtil.Hash HASH = new JedisUtil().new Hash();
/** 对存储结构为Set(排序的)类型的操作 */
public static JedisUtil.SortSet SORTSET = new JedisUtil().new SortSet();
private static JedisPool jedisPool = null;
private ShardedJedisPool shardedJedisPool = null;
private JedisUtil() {
init();
}
/**
* 构建redis连接池
* @param ip
* @param port
* @return JedisPool
*/
public static void init() {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException(
"[redis.properties] is not found!");
}
if (jedisPool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(bundle.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.parseInt(bundle.getString("redis.pool.maxIdle")));
config.setMaxWaitMillis(Long.parseLong(bundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.parseBoolean(bundle.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.parseBoolean(bundle.getString("redis.pool.testOnReturn")));
jedisPool = new JedisPool(config, bundle.getString("redis.ip"), Integer.parseInt(bundle.getString("redis.port")));
}
}
public JedisPool getPool() {
return jedisPool;
}
public ShardedJedisPool getShardedJedisPool() {
return shardedJedisPool;
}
/**
* 从jedis连接池中获取获取jedis对象
* @return
*/
public Jedis getJedis() {
return jedisPool.getResource();
}
/**
* @param 选择指定DB
* @return
*/
public Jedis getJedis(int DBindex) {
Jedis jedis = jedisPool.getResource();
jedis.select(DBindex);
return jedis;
}
/**
* 获取JedisUtil实例
* @return
*/
public static JedisUtil getInstance() {
return redis;
}
/**
* 回收jedis
* @param jedis
*/
public void returnJedis(Jedis jedis) {
// jedisPool.returnResource(jedis);
jedis.close();
}
/**
* 设置过期时间
*
* @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);
}
public void expire(String key, int seconds, int DBindex) {
if (seconds <= 0) {
return;
}
Jedis jedis = getJedis(DBindex);
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;
}
public long expired(String key, int seconds, int DBindex) {
Jedis jedis = getJedis(DBindex);
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) {
- 1
- 2
前往页