Redis的Util工具类,超好用!

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;

1 JedisPool pool = **null**; 2 Jedis jedis = **null**; 3 **try** { 4 pool = _getPool_(); 5 jedis = pool.getResource(); 6 value = jedis.get(key); 7 } **catch** (Exception e) { 8 _//__释放__redis__对象

_ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); }

1 **return** value; 2} 3 4_/\*\*

_ * _插入数据 _ _* _ * @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); }

1} 2 3_/\*\*

_ * _若__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 _<T> _ * @param _key _ _*/ _ public static <T> Boolean setexList(String key, List<T> list) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); jedis.set(key.getBytes(), ObjectTranscoder.serialize(list)); return true; } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * 设置 _list _ * * @param _<T> _ * @param _key _ _*/ _ public static <T> Boolean setexList(String key, int seconds, List<T> list) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); jedis.setex(key.getBytes(), seconds, ObjectTranscoder.serialize(list)); return true; } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * _获取__list _ * * @param _<T> _ * @param _key _ * @return _list _ */ public static <T> List<T> getList(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()); List<T> list = (List<T>) ObjectTranscoder.deserialize(in); return list; } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * 设置 _map _ * * @param _<T> _ * @param _key _ _*/ _ public static <T> Boolean setMap(String key, Map<String, T> map) {

1 JedisPool pool = **null**; 2 Jedis jedis = **null**; 3 **try** { 4 pool = _getPool_(); 5 jedis = pool.getResource(); 6 jedis.set(key.getBytes(), ObjectTranscoder._serialize_(map)); 7 **return true**; 8 } **catch** (Exception e) { 9 _//__释放__redis__对象

_ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * 设置 _map _ * * @param _<T> _ * @param _key _ _*/ _ public <T> Boolean setesMap(String key, int seconds, Map<String, T> map) {

1 JedisPool pool = **null**; 2 Jedis jedis = **null**; 3 **try** { 4 pool = _getPool_(); 5 jedis = pool.getResource(); 6 jedis.setex(key.getBytes(), seconds, ObjectTranscoder._serialize_(map)); 7 **return true**; 8 } **catch** (Exception e) { 9 _//__释放__redis__对象

_ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * _获取__list _ * * @param _<T> _ * @param _key _ * @return _list _ */ public <T> Map<String, T> getMap(String key) {

1 String value = **null**; 2 JedisPool pool = **null**; 3 Jedis jedis = **null**; 4 **try** { 5 pool = _getPool_(); 6 jedis = pool.getResource(); 7 value = jedis.get(key); 8 **byte**\[\] in = jedis.get(key.getBytes()); 9 Map<String, T\> map = (Map<String, T\>) ObjectTranscoder._deserialize_(in); 10 **return** map; 11 } **catch** (Exception e) { 12 _//__释放__redis__对象

_ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * _设置 _ _* _ * @param _<T> _ * @param _key _ _*/ _ public static <T> Boolean setexObject(String key, int seconds, Object o) {

1 JedisPool pool = **null**; 2 Jedis jedis = **null**; 3 **try** { 4 pool = _getPool_(); 5 jedis = pool.getResource(); 6 jedis.setex(key.getBytes(), seconds, ObjectTranscoder._serialize_(o)); 7 **return true**; 8 } **catch** (Exception e) { 9 _//__释放__redis__对象

_ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * _设置 _ _* _ * @param _<T> _ * @param _key _ _*/ _ public static <T> Boolean setObject(String key, Object o) {

1 JedisPool pool = **null**; 2 Jedis jedis = **null**; 3 **try** { 4 pool = _getPool_(); 5 jedis = pool.getResource(); 6 jedis.set(key.getBytes(), ObjectTranscoder._serialize_(o)); 7 **return true**; 8 } **catch** (Exception e) { 9 _//__释放__redis__对象

_ pool.returnBrokenResource(jedis); e.printStackTrace(); return false; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * _获取__list _ * * @param _key _ * @return _list _ */ public static Object getObject(String key) {

1 String value = **null**; 2 JedisPool pool = **null**; 3 Jedis jedis = **null**; 4 **try** { 5 pool = _getPool_(); 6 jedis = pool.getResource(); 7 value = jedis.get(key); 8 **byte**\[\] in = jedis.get(key.getBytes()); 9 Object o = ObjectTranscoder._deserialize_(in); 10 **return** o; 11 } **catch** (Exception e) { 12 _//__释放__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();

1 **return** jedis.expire(key, milliseconds); 2 } **catch** (Exception e) { 3 _//__释放__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<String> gethKeys(String pattern) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.hkeys(pattern); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * _返回指定__Map__中的__values__集合 _ _* _ * @param _key _ * _@return _ _*/ _ public static List<String> gethVals(String key) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.hvals(key); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * _修改__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<String> gethm(String key, String fields) { JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); return jedis.hmget(key, fields); } catch (Exception e) { _//__释放__redis__对象 _ pool.returnBrokenResource(jedis); e.printStackTrace(); return null; } finally { _//__返还到连接池 _ returnResource(pool, jedis); } }

_/\*\*

_ * _删除__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;

1 **try** { 2 pool = _getPool_(); 3 jedis = pool.getResource(); 4 5 String pre\_str = PrefixStr; 6 7 Set<String> set = jedis.keys(pre\_str + **"\*"**); 8 9 Iterator<String> it = set.iterator(); 10 System._out_.println(**"keys:"**+set); 11 _//__计数删除了多少__key_

while (it.hasNext()) { String keyStr = it.next();

1 System._out_.println(keyStr); 2 jedis.del(keyStr); 3 x++; 4 } 5 6 } **catch** (Exception e) { 7 8 pool.returnBrokenResource(jedis); 9 e.printStackTrace(); 10 **return null**; 11 12 } **finally** { 13 14 _//__返还到连接池

_ returnResource(pool, jedis); }

    **return** x;

}

1**public static void** main(String\[\] args) { 2 _/\* 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); } } }

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang