package com.lgdz.zhenshiUtil;
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig;
import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set;
_/** _ * Redis _公共类 _ _* _ * @author xizhuangchui _ * @createDate 2015-6-27 16:39:40 _ */ public class RedisUtil { private static JedisPool pool = null; public static String ip = "127.0.0.1"; public static int port = 6379; /** _ * 十分钟(600__秒) _ */ public static int SMS_TIME = 10 * 60;
_/\*\*
_ * _构建__redis__连接池 _ _* _ * @return _JedisPool _ */ public static JedisPool getPool() { if (pool == null) { JedisPoolConfig config = new JedisPoolConfig(); _//__控制一个__pool__最多有多少个状态为__idle(空闲的)__的__jedis__实例。 _ config.setMaxIdle(500); _//__在__borrow__一个__jedis__实例时,是否提前进行__validate__操作;如果为__true__,则得到的__jedis__实例均是可用的; _ config.setTestOnBorrow(true); _/* Properties properties = BaseConfig.getSysProperties();*/ _ _/* InputStream in = RedisUtil.class.getResourceAsStream("../../../redis.properties"); //---------------------_这个是文件的路径(注意区别很简单,就是加上包的路径) try { properties.load(in); } catch (IOException e) { e.printStackTrace(); } _*/ _ RedisUtil.ip = "127.0.0.1"; RedisUtil.port = 6379; pool = new JedisPool(config, ip, port); } return pool; }
_/\*\*
_ * _返还到连接池 _ _* _ * @param pool _redis__连接池 _ * @param redis _redis__对象 _ _*/ _ public static void returnResource(JedisPool pool, Jedis redis) { if (redis != null) { pool.returnResource(redis); } }
_/\*\*
_ * _获取数据 _ _* _ * @param key _key__的值 _ * @return _String value__的值 _ _*/ _ public static String get(String key) { String value = null;
JedisPool pool = **null**;
Jedis jedis = **null**;
**try** {
pool = _getPool_();
jedis = pool.getResource();
value = jedis.get(key);
} **catch** (Exception e) {
_//__释放__redis__对象
_ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); }
**return** value;
}
_/\*\*
_ * _插入数据 _ _* _ * @param _key _ * @param _value _ * @return Boolean _返回插入状态 _ _*/ _ public static Boolean set(String key, String value) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); jedis.set(key, value); return true; } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); }
}
_/\*\*
_ * _若__key__不存在则储存 _ _* _ * @param _key _ * @param _value _ * @return Long _成功操作的个数 _ _*/ _ public static Long setnx(String key, String value) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.setnx(key, value); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _设置__key__的过期时间,单位:秒,如果__key__在超时之前被修改,则该键关联的超时将被移除。 _ _* _ * @param _key _ * @param seconds _超时时间(秒) _ * @param _value _ * @return 正常返回 _OK _ */ public static String setex(String key, int seconds, String value) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.setex(key, seconds, value); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * 设置 _list
_ * * @param _
_/\*\*
_ * 设置 _list
_ * * @param _
_/\*\*
_ * _获取__list
_ * * @param _
_/\*\*
_ * 设置 _map
_ * * @param _
JedisPool pool = **null**;
Jedis jedis = **null**;
**try** {
pool = _getPool_();
jedis = pool.getResource();
jedis.set(key.getBytes(), ObjectTranscoder._serialize_(map));
**return true**;
} **catch** (Exception e) {
_//__释放__redis__对象
_ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * 设置 _map
_ * * @param _
JedisPool pool = **null**;
Jedis jedis = **null**;
**try** {
pool = _getPool_();
jedis = pool.getResource();
jedis.setex(key.getBytes(), seconds, ObjectTranscoder._serialize_(map));
**return true**;
} **catch** (Exception e) {
_//__释放__redis__对象
_ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _获取__list
_ * * @param _
String value = **null**;
JedisPool pool = **null**;
Jedis jedis = **null**;
**try** {
pool = _getPool_();
jedis = pool.getResource();
value = jedis.get(key);
**byte**\[\] in = jedis.get(key.getBytes());
Map<String, T\> map = (Map<String, T\>) ObjectTranscoder._deserialize_(in);
**return** map;
} **catch** (Exception e) {
_//__释放__redis__对象
_ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _设置
_ _*
_ * @param _
JedisPool pool = **null**;
Jedis jedis = **null**;
**try** {
pool = _getPool_();
jedis = pool.getResource();
jedis.setex(key.getBytes(), seconds, ObjectTranscoder._serialize_(o));
**return true**;
} **catch** (Exception e) {
_//__释放__redis__对象
_ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _设置
_ _*
_ * @param _
JedisPool pool = **null**;
Jedis jedis = **null**;
**try** {
pool = _getPool_();
jedis = pool.getResource();
jedis.set(key.getBytes(), ObjectTranscoder._serialize_(o));
**return true**;
} **catch** (Exception e) {
_//__释放__redis__对象
_ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _获取__list _ * * @param _key _ * @return _list _ */ public static Object getObject(String key) {
String value = **null**;
JedisPool pool = **null**;
Jedis jedis = **null**;
**try** {
pool = _getPool_();
jedis = pool.getResource();
value = jedis.get(key);
**byte**\[\] in = jedis.get(key.getBytes());
Object o = ObjectTranscoder._deserialize_(in);
**return** o;
} **catch** (Exception e) {
_//__释放__redis__对象
_ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _设置__key__对应的一条数据超时时间 _ _* _ * @param _key _ * @param milliseconds _超时的时间(毫秒) _ * @return _成功操作的个数 _ _*/ _ public static Long expire(String key, int milliseconds) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource();
**return** jedis.expire(key, milliseconds);
} **catch** (Exception e) {
_//__释放__redis__对象
_ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _指定超时时间 _ _* _ * @param _key _ * @param millisecondsTimestamp _超时时间的时间戳 _ * @return _成功返回__1__失败返回__0 _ */ public static Long expireAt(String key, long millisecondsTimestamp) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.expireAt(key, millisecondsTimestamp); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _查询剩余时间 _ _* _ * @param _key _ * @return _剩余时间 _ _*/ _ public static Long ttl(String key) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.ttl(key); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _判断__key__是否存在 _ _* _ * @param _key _ * @return _Boolean _ */ public static Boolean exists(String key) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); jedis.exists(key); return true; } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _删除多个 _ _* _ * @param keys _key__数组 _ * @return _成功操作的个数 _ _*/ _ public static Long del(String[] keys) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.del(keys); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _删除一个 _ _* _ * @param _key _ * @return _成功操作的个数 _ _*/ _ public static Long del(String key) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.del(key); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _返回指定__Map__中的__key__集合
_ _*
_ * @param _pattern
_ * _@return
_ _*/
_ public static Set
_/\*\*
_ * _返回指定__Map__中的__values__集合
_ _*
_ * @param _key
_ * _@return
_ _*/
_ public static List
_/\*\*
_ * _修改__key _ * * @param _oldkey _ * @param _newkey _ * _@return _ _*/ _ public static String rename(String oldkey, String newkey) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.rename(oldkey, newkey); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _如果不存在则修改__key _ * * @param _oldkey _ * @param _newkey _ * _@return _ _*/ _ public static Long renamenx(String oldkey, String newkey) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.renamenx(oldkey, newkey); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _获取__value__,并重新赋值 _ _* _ * @param _key _ * @param _value _ * _@return _ _*/ _ public static String getSet(String key, String value) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.getSet(key, value); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _给__value__追加值 _ _* _ * @param _key _ * @param _value _ * _@return _ _*/ _ public static Long append(String key, String value) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.append(key, value); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _获取指定位置的__value__值 _ _* _ * @param _key _ * @param _start _ * @param _end _ * _@return _ _*/ _ public static String substr(String key, int start, int end) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.substr(key, start, end); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _存入__map__数据 _ _* _ * @param _key _ * @param _hash _ * _@return _ _*/ _ public static String sethm(String key, Map<String, String> hash) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.hmset(key, hash); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _存入__MAP__并设置超时 _ _* _ * @param _key _ * @param seconds 超时时间(秒) _ * @param hash Map<String__, String> _ * @return _Boolean _ */ public static Boolean sethmExpire(String key, int seconds, Map<String, String> hash) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); String state = jedis.hmset(key, hash); if (state.equals("OK")) { jedis.expire(key, seconds); } else { return false; } return true; } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _插入__map__指定的__key__对应的__value _ * * @param _key _ * @param _field _ * @param _value _ * _@return _ _*/ _ public static Long seth(String key, String field, String value) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.hset(key, field, value); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _若不存在则插入__map__指定的__key__对应的__value _ * * @param _key _ * @param _field _ * @param _value _ * _@return _ _*/ _ public static Long sethnx(String key, String field, String value) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.hsetnx(key, field, value); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _获取__map__指定的__key__对应的__value
_ * * @param _key
_ * @param _fields
_ * _@return
_ _*/
_ public static List
_/\*\*
_ * _删除__map__指定的__key _ * * @param _key _ * @param _fields _ * _@return _ _*/ _ public static Long delh(String key, String fields) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.hdel(key, fields); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _获取指定__key__的__MAP _ * * @param _key _ * @return Map<_String_, _String_> _*/ _ public static Map<String, String> gethAll(String key) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.hgetAll(key); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }
_/\*\*
_ * _批量删除以指定前缀开头的__KEYS _ * @param PrefixStr _前缀 _ _*/ _ public static Long delkeysforPrefixStr(String PrefixStr){ JedisPool pool = null; Jedis jedis = null; Long x= (long)0;
**try** {
pool = _getPool_();
jedis = pool.getResource();
String pre\_str = PrefixStr;
Set<String> set = jedis.keys(pre\_str + **"\*"**);
Iterator<String> it = set.iterator();
System._out_.println(**"keys:"**+set);
_//__计数删除了多少__key_
while (it.hasNext()) { String keyStr = it.next();
System._out_.println(keyStr);
jedis.del(keyStr);
x++;
}
} **catch** (Exception e) {
pool.returnBrokenResource(jedis);
e.printStackTrace();
**return null**;
} **finally** {
_//__返还到连接池
_ returnResource(pool, jedis); }
**return** x;
}
**public static void** main(String\[\] args) {
_/\* String message = "C0700000000000046A100G10000001";_
set("test", message); System.out.println(get("test"));*/ _//_批量删除以某个字符串为前缀的__keys _ delkeysforPrefixStr("qq"); //存__key__:"offLine192.168.10.25" 时间:__90__秒 数据:"1" _ setex("offLine192.168.10.25",90,"1"); }
_/\*\*
_ * _生成序列号 _ _* _ * _@return _ _*/ _ public static Long incr(String key) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.incr(key); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } } }