Java Redis 连接池 Jedis 工具类
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 | import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class MyJedisPool { private final static Logger logger = LoggerFactory.getLogger(MyJedisPool. class ); private static JedisPool readPool = null ; private static JedisPool writePool = null ; //静态代码初始化池配置 static { try { Properties props = new Properties(); InputStream in = MyJedisPool. class .getResourceAsStream( "/redis.properties" ); props.load(in); //创建jedis池配置实例 JedisPoolConfig config = new JedisPoolConfig(); //设置池配置项值 config.setMaxTotal(Integer.valueOf(props.getProperty( "jedis.pool.maxActive" ))); config.setMaxIdle(Integer.valueOf(props.getProperty( "jedis.pool.maxIdle" ))); config.setMaxWaitMillis(Long.valueOf(props.getProperty( "jedis.pool.maxWait" ))); config.setTestOnBorrow(Boolean.valueOf(props.getProperty( "jedis.pool.testOnBorrow" ))); config.setTestOnReturn(Boolean.valueOf(props.getProperty( "jedis.pool.testOnReturn" ))); //根据配置实例化jedis池 readPool = new JedisPool(config, props.getProperty( "redisReadURL" ), Integer.valueOf(props.getProperty( "redisReadPort" ))); writePool = new JedisPool(config, props.getProperty( "redisWriteURL" ), Integer.valueOf(props.getProperty( "redisWritePort" ))); } catch (IOException e) { logger.info( "redis连接池异常" ,e); } } /**获得jedis对象*/ public static Jedis getReadJedisObject(){ return readPool.getResource(); } /**获得jedis对象*/ public static Jedis getWriteJedisObject(){ return writePool.getResource(); } /**归还jedis对象*/ public static void returnJedisOjbect(Jedis jedis){ if (jedis != null ) { jedis.close(); } } } |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | import redis.clients.jedis.Jedis; import java.util.Set; public class RedisUtils { /** * 获取hash表中所有key * @param name * @return */ public static Set<String> getHashAllKey(String name){ Jedis jedis = null ; try { jedis = MyJedisPool.getReadJedisObject(); return jedis.hkeys(name); } catch (Exception e){ e.printStackTrace(); } finally { MyJedisPool.returnJedisOjbect(jedis); } return null ; } /** * 从redis hash表中获取 * @param hashName * @param key * @return */ public static String getHashKV(String hashName,String key){ Jedis jedis = null ; try { jedis = MyJedisPool.getReadJedisObject(); return jedis.hget(hashName, key); } catch (Exception e){ e.printStackTrace(); } finally { MyJedisPool.returnJedisOjbect(jedis); } return null ; } /** * 删除hash表的键值对 * @param hashName * @param key */ public static Long delHashKV(String hashName,String key){ Jedis jedis = null ; try { jedis = MyJedisPool.getWriteJedisObject(); return jedis.hdel(hashName,key); } catch (Exception e){ e.printStackTrace(); } finally { MyJedisPool.returnJedisOjbect(jedis); } return null ; } /** * 存放hash表键值对 * @param hashName * @param key * @param value */ public static Long setHashKV(String hashName,String key,String value){ Jedis jedis = null ; try { jedis = MyJedisPool.getWriteJedisObject(); return jedis.hset(hashName,key,value); } catch (Exception e){ e.printStackTrace(); } finally { MyJedisPool.returnJedisOjbect(jedis); } return null ; } /** * 删除键值对 * @param k * @return */ public static Long delKV(String k){ Jedis jedis = null ; try { jedis = MyJedisPool.getWriteJedisObject(); return jedis.del(k); } catch (Exception e){ e.printStackTrace(); } finally { MyJedisPool.returnJedisOjbect(jedis); } return null ; } /** * 放键值对 * 永久 * @param k * @param v */ public static String setKV(String k, String v) { Jedis jedis = null ; try { jedis = MyJedisPool.getWriteJedisObject(); return jedis.set(k, v); } catch (Exception e){ e.printStackTrace(); } finally { MyJedisPool.returnJedisOjbect(jedis); } return null ; } /** * 放键值对 * * @param k * @param v */ public static String setKV(String k, int second, String v) { Jedis jedis = null ; try { jedis = MyJedisPool.getWriteJedisObject(); return jedis.setex(k,second, v); } catch (Exception e){ e.printStackTrace(); } finally { MyJedisPool.returnJedisOjbect(jedis); } return null ; } /** * 根据key取value * * @param k * @return */ public static String getKV(String k) { Jedis jedis = null ; try { jedis = MyJedisPool.getReadJedisObject(); return jedis.get(k); } catch (Exception e){ e.printStackTrace(); } finally { MyJedisPool.returnJedisOjbect(jedis); } return null ; } } |