package cloud.app.prod.home.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; public class RedisUtil { // Redis服务器IP
private static String ADDR = "127.0.0.1"; // Redis的端口号
private static int PORT = 6379; // 访问密码
private static String AUTH = "scrm__12345"; // 可用连接实例的最大数目,默认值为8;
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 private static int MAX_ACTIVE = 1024; // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200; // 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000; // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true; private static JedisPool jedisPool = null; /**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWait(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 获取Jedis实例
*
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 释放jedis资源
*
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
} /**
* Jedis对象出异常的时候,回收Jedis对象资源
*
* @param jedis
*/
public static void returnBrokenResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnBrokenResource(jedis);
} } /**
* 通过Redis的key获取值,并释放连接资源
* @param key
* @return 成功返回value,失败返回null
*/
public static String get(String key) {
Jedis jedis = null;
String value = null; try {
jedis = getJedis();
if (null != jedis) {
value = jedis.get(key);
}
} catch (Exception e) {
returnBrokenResource(jedis);
e.printStackTrace();
} finally {
returnResource(jedis);
}
return value;
} /**
* 向redis存入key和value(如果key已经存在 则覆盖),并释放连接资源
* @param key
* @param value
*/
public static void set(String key, String value) {
Jedis jedis = null;
try {
jedis = getJedis();
if (null != jedis) {
jedis.set(key, value);
}
} catch (Exception e) {
returnBrokenResource(jedis);
e.printStackTrace();
} finally {
returnResource(jedis);
}
} /**
* 向redis存入key和value(如果key已经存在 则覆盖),并且设置存活时间,及释放连接资源
* @param key
* @param value
* @param seconds
*/
public static void set(String key, String value, int seconds) {
Jedis jedis = null;
try {
jedis = getJedis();
if (null != jedis) {
jedis.set(key, value);
jedis.expire(key, seconds);
}
} catch (Exception e) {
returnBrokenResource(jedis);
e.printStackTrace();
} finally {
returnResource(jedis);
}
} public static String byte2hex(byte[] buffer) {
String h = "0x";
for (byte aBuffer : buffer) {
String temp = Integer.toHexString(aBuffer & 0xFF);
if (temp.length() == 1) {
temp = "0" + temp;
}
h = h + " " + temp;
}
return h;
} }

最新文章

  1. 集合工具类:collections
  2. [转]FINDSTR正则表达式小结
  3. javascript入门:prototype和面向对象的实现
  4. json数组的序列化和反序列化json数组的序列化和反序列化
  5. STL Map的使用
  6. Uva12504 Updating a Dictonary
  7. oracle启动,提示“LRM-00109: could not open parameter file”
  8. C# 相对路径转绝对路径
  9. 【翻译】Ext JS最新技巧——2016-3-4
  10. SpringMVC 全局异常处理
  11. linux内存源码分析 - 内存池
  12. python编程 之 json包
  13. Stanford CoreNLP使用需要注意的一点
  14. 开源API测试工具 Hitchhiker v0.10 - 中文版
  15. Linux下DHCP服务安装配置
  16. 一篇采访窥C#的未来
  17. Ubuntu 录制视频并制作成gif图
  18. BackgroundWorker简单实用(简便的异步操作)
  19. HDU 2895 贪心 还是 大水题
  20. Nginx 分析access日志文件

热门文章

  1. 实现div毛玻璃背景
  2. JS——选择水果
  3. python web 开发学习路线
  4. shell使用eval进行赋值bc计算,bad substitution
  5. Bullet:Python的函数中参数是引用吗?
  6. G. I love Codeforces
  7. P2884 [USACO07MAR]每月的费用Monthly Expense
  8. opcache的配置
  9. odoo widget 标签介绍
  10. 渗透实战(周二):FLUXION暴力破解WIFI登录口令