Redis 连接池
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| 1public class RedisPool {
2 private static JedisPool pool;//jedis连接池
3 private static Integer maxTotal = ; //最大连接数
4 private static Integer maxIdle = ;//在jedispool中最大的idle状态(空闲的)的jedis实例的个数
5 private static Integer minIdle = ;//在jedispool中最小的idle状态(空闲的)的jedis实例的个数
6
7 private static Boolean testOnBorrow = ;//在borrow一个jedis实例的时候,是否要进行验证操作,如果赋值true。则得到的jedis实例肯定是可以用的。
8 private static Boolean testOnReturn = ;//在return一个jedis实例的时候,是否要进行验证操作,如果赋值true。则放回jedispool的jedis实例肯定是可以用的。
9
10 private static String redisIp = ;
11 private static Integer redisPort = ;
12
13
14 private static void initPool(){
15 JedisPoolConfig config = new JedisPoolConfig();
16
17 config.setMaxTotal(maxTotal);
18 config.setMaxIdle(maxIdle);
19 config.setMinIdle(minIdle);
20
21 config.setTestOnBorrow(testOnBorrow);
22 config.setTestOnReturn(testOnReturn);
23
24 config.setBlockWhenExhausted(true);//连接耗尽的时候,是否阻塞,false会抛出异常,true阻塞直到超时。默认为true。
25
26 pool = new JedisPool(config,redisIp,redisPort,1000*2);
27 }
28
29 static{
30 initPool();
31 }
32
33 public static Jedis getJedis(){
34 return pool.getResource();
35 }
36
37
38 public static void returnBrokenResource(Jedis jedis){
39 pool.returnBrokenResource(jedis);
40 }
41
42
43
44 public static void returnResource(Jedis jedis){
45 pool.returnResource(jedis);
46 }
47
48
49 |
Redis 工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| 1public class RedisPoolUtil {
2
3
4 /**
5 * 设置key的有效期,单位是秒
6 * @param key
7 * @param exTime
8 * @return
9 */
10 public static Long expire(String key,int exTime){
11 Jedis jedis = null;
12 Long result = null;
13 try {
14 jedis = RedisPool.getJedis();
15 result = jedis.expire(key,exTime);
16 } catch (Exception e) {
17 log.error("expire key:{} error",key,e);
18 RedisPool.returnBrokenResource(jedis);
19 return result;
20 }
21 RedisPool.returnResource(jedis);
22 return result;
23 }
24
25 //exTime的单位是秒
26 public static String setEx(String key,String value,int exTime){
27 Jedis jedis = null;
28 String result = null;
29 try {
30 jedis = RedisPool.getJedis();
31 result = jedis.setex(key,exTime,value);
32 } catch (Exception e) {
33 log.error("setex key:{} value:{} error",key,value,e);
34 RedisPool.returnBrokenResource(jedis);
35 return result;
36 }
37 RedisPool.returnResource(jedis);
38 return result;
39 }
40
41 public static String set(String key,String value){
42 Jedis jedis = null;
43 String result = null;
44
45 try {
46 jedis = RedisPool.getJedis();
47 result = jedis.set(key,value);
48 } catch (Exception e) {
49 log.error("set key:{} value:{} error",key,value,e);
50 RedisPool.returnBrokenResource(jedis);
51 return result;
52 }
53 RedisPool.returnResource(jedis);
54 return result;
55 }
56
57 public static String get(String key){
58 Jedis jedis = null;
59 String result = null;
60 try {
61 jedis = RedisPool.getJedis();
62 result = jedis.get(key);
63 } catch (Exception e) {
64 log.error("get key:{} error",key,e);
65 RedisPool.returnBrokenResource(jedis);
66 return result;
67 }
68 RedisPool.returnResource(jedis);
69 return result;
70 }
71
72 public static Long del(String key){
73 Jedis jedis = null;
74 Long result = null;
75 try {
76 jedis = RedisPool.getJedis();
77 result = jedis.del(key);
78 } catch (Exception e) {
79 log.error("del key:{} error",key,e);
80 RedisPool.returnBrokenResource(jedis);
81 return result;
82 }
83 RedisPool.returnResource(jedis);
84 return result;
85 }
86}
87 |