日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

redis pool

發(fā)布時間:2024/4/17 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 redis pool 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Redis Pool--Java?

配置文件

#redis confADDR=127.0.0.1 PORT=6379 AUTH=#session timeout TIMEOUT=1000 MAX_ACTIVE=500 MAX_IDLE=200 MIN_IDLE=100 MAX_WAIT=1000

?

RedisPool.java

1 public final class RedisPool { 2 3 private static JedisPool jedisPool = null; 4 5 // 最大連接數(shù):可同時建立的最大鏈接數(shù) 6 private static int max_acti; 7 8 // 最大空閑數(shù):空閑鏈接數(shù)大于maxIdle時,將進(jìn)行回收 9 private static int max_idle; 10 11 // 最小空閑數(shù):低于minIdle時,將創(chuàng)建新的鏈接 12 private static int min_idle; 13 14 // 最大等待時間:單位ms 15 private static int max_wait; 16 17 private static String addr; 18 19 private static int port; 20 21 private static String auth; 22 23 // session timeout by seconds 24 private static int session_timeout; 25 26 private RedisPool() { 27 } 28 29 /** 30 * get properties and init RedisPool 31 */ 32 static { 33 Properties pps = new Properties(); 34 InputStream in; 35 try { 36 in = new BufferedInputStream(new FileInputStream("src"+File.separator+"conf"+File.separator+"redispool.properties")); 37 pps.load(in); 38 39 addr = pps.getProperty("ADDR"); 40 auth = pps.getProperty("AUTH"); 41 port = Integer.parseInt(pps.getProperty("PORT")); 42 session_timeout = Integer.parseInt(pps.getProperty("TIMEOUT")); 43 max_acti = Integer.parseInt(pps.getProperty("MAX_ACTIVE")); 44 max_idle = Integer.parseInt(pps.getProperty("MAX_IDLE")); 45 min_idle = Integer.parseInt(pps.getProperty("MIN_IDLE")); 46 max_wait = Integer.parseInt(pps.getProperty("MAX_WAIT")); 47 48 JedisPoolConfig config = new JedisPoolConfig(); 49 config.setMaxActive(max_acti); 50 config.setMaxIdle(max_idle); 51 config.setMaxWait(max_wait); 52 config.setMinIdle(min_idle); 53 jedisPool = new JedisPool(config, addr, port, 1000, auth); 54 55 } catch (Exception e) { 56 throw new RuntimeException("jedisPool init error" + e.getMessage()); 57 } 58 59 } 60 61 /** 62 * get jedis resource 63 */ 64 public synchronized static Jedis getJedis() { 65 if (jedisPool != null) { 66 return jedisPool.getResource(); 67 } else { 68 throw new RuntimeException("jedisPool is null"); 69 } 70 } 71 72 /** 73 * get value map by key 74 * 75 * @param key 76 * @return map 77 */ 78 public static Map<String, String> getHashValue(String key) { 79 Jedis jedis = getJedis(); 80 Map<String, String> map = new HashMap<String, String>(); 81 Iterator<String> iter = jedis.hkeys(key).iterator(); 82 while (iter.hasNext()) { 83 String mkey = iter.next(); 84 map.put(mkey, jedis.hmget(key, mkey).get(0)); 85 } 86 jedisPool.returnResource(jedis); 87 return map; 88 } 89 90 /** 91 * set value by key and map value 92 * 93 * @param key 94 * @param hash 95 */ 96 public static void setHashValue(String key, Map<String, String> hash) { 97 Jedis jedis = getJedis(); 98 jedis.hmset(key, hash); 99 jedis.expire(key, session_timeout); 100 jedisPool.returnResource(jedis); 101 } 102 103 /** 104 * remove value by key 105 * 106 * @param key 107 */ 108 public static void remove(String key) { 109 Jedis jedis = getJedis(); 110 jedis.del(key); 111 jedisPool.returnResource(jedis); 112 } 113 114 /** 115 * expire session time to session_timeout 116 * 117 * @param key 118 */ 119 public static void expire(String key) { 120 Jedis jedis = getJedis(); 121 jedis.expire(key, session_timeout); 122 jedisPool.returnResource(jedis); 123 } 124 125 /** 126 * return jedis resource 127 */ 128 public static void returnResource(final Jedis jedis) { 129 if (jedis != null) { 130 jedisPool.returnResource(jedis); 131 } 132 } 133 134 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/luangeng/p/5767531.html

總結(jié)

以上是生活随笔為你收集整理的redis pool的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。