|
| 1 | +package com.sohu.cache.util; |
| 2 | + |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | + |
| 5 | +import redis.clients.jedis.Jedis; |
| 6 | +import redis.clients.jedis.Protocol; |
| 7 | + |
| 8 | +/** |
| 9 | + * 获取jedis连接 |
| 10 | + * |
| 11 | + * @author leifu |
| 12 | + * @Date 2016年6月14日 |
| 13 | + * @Time 上午10:38:36 |
| 14 | + */ |
| 15 | +public class JedisUtil { |
| 16 | + |
| 17 | + public static Jedis getJedis(String host, int port) throws Exception { |
| 18 | + return getJedis(host, port, Protocol.DEFAULT_TIMEOUT, null); |
| 19 | + } |
| 20 | + |
| 21 | + public static Jedis getJedis(String host, int port, int timeout) throws Exception { |
| 22 | + return getJedis(host, port, timeout, null); |
| 23 | + } |
| 24 | + |
| 25 | + public static Jedis getJedis(String host, int port, int connectionTimeout, int soTimeout) throws Exception { |
| 26 | + return getJedis(host, port, connectionTimeout, soTimeout, null); |
| 27 | + } |
| 28 | + |
| 29 | + public static Jedis getJedis(String host, int port, String password) throws Exception { |
| 30 | + return getJedis(host, port, Protocol.DEFAULT_TIMEOUT, password); |
| 31 | + } |
| 32 | + |
| 33 | + public static Jedis getJedis(String host, int port, int timeout, String password) throws Exception { |
| 34 | + return getJedis(host, port, timeout, timeout, password); |
| 35 | + } |
| 36 | + |
| 37 | + public static Jedis getJedis(String host, int port, int connectionTimeout, int soTimeout, String password) throws Exception { |
| 38 | + try { |
| 39 | + Jedis jedis = new Jedis(host, port, connectionTimeout, soTimeout); |
| 40 | + if (StringUtils.isNotBlank(password)) { |
| 41 | + jedis.auth(password); |
| 42 | + } |
| 43 | + return jedis; |
| 44 | + } catch (Exception e) { |
| 45 | + throw e; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments