{"id":201321,"date":"2025-05-10T11:16:18","date_gmt":"2025-05-10T03:16:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201321\/"},"modified":"2025-05-10T11:16:18","modified_gmt":"2025-05-10T03:16:18","slug":"redis%e8%bf%9e%e6%8e%a5%e6%b1%a0%e9%85%8d%e7%bd%ae%e5%8f%8a%e5%88%9d%e5%a7%8b%e5%8c%96%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201321\/","title":{"rendered":"Redis\u8fde\u63a5\u6c60\u914d\u7f6e\u53ca\u521d\u59cb\u5316\u5b9e\u73b0"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u8fde\u63a5\u6c60\u914d\u7f6e\u53ca\u521d\u59cb\u5316\u5b9e\u73b0<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aRedis\u8fde\u63a5\u6c60\u914d\u7f6e\u53ca\u521d\u59cb\u5316\u5b9e\u73b0\u300b\uff0c\u804a\u804aRedis\u8fde\u63a5\u6c60\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>\u52a0\u5165db\u9009\u62e9\u540e\u7684redis\u8fde\u63a5\u6c60\u914d\u7f6e\u4ee3\u7801<\/p>\n<pre>\npublic class RedisPoolConfigure {\n\n \/\/Redis\u670d\u52a1\u5668IP\n private String ADDR ;\n \n \/\/Redis\u7684\u7aef\u53e3\u53f7\n private int PORT ;\n \n \/\/\u53ef\u7528\u8fde\u63a5\u5b9e\u4f8b\u7684\u6700\u5927\u6570\u76ee\n private int MAX_ACTIVE ;\n \n \/\/pool\u4e2d\u7684idle jedis\u5b9e\u4f8b\u6570\n private int MAX_IDLE ;\n \n \/\/\u7b49\u5f85\u53ef\u7528\u8fde\u63a5\u7684\u6700\u5927\u65f6\u95f4\uff0c\u5355\u4f4d\u6beb\u79d2\n private int MAX_WAIT ;\n \/\/\u8d85\u65f6\u65f6\u95f4\uff0c\u5355\u4f4d\u6beb\u79d2\n private int TIME_OUT ;\n \/\/\u8bbe\u7f6e\u7684\u9010\u51fa\u7b56\u7565\u7c7b\u540d, \u9ed8\u8ba4DefaultEvictionPolicy(\u5f53\u8fde\u63a5\u8d85\u8fc7\u6700\u5927\u7a7a\u95f2\u65f6\u95f4,\u6216\u8fde\u63a5\u6570\u8d85\u8fc7\u6700\u5927\u7a7a\u95f2\u8fde\u63a5\u6570)\n private String EVICTION_POLICY_CLASS_NAME ;\n \n \/\/\u8fde\u63a5\u8017\u5c3d\u65f6\u662f\u5426\u963b\u585e, false\u62a5\u5f02\u5e38,ture\u963b\u585e\u76f4\u5230\u8d85\u65f6\n private boolean BLOCK_WHEN_EXHAUSTED;\n \n \/\/\u662f\u5426\u542f\u7528pool\u7684jmx\u7ba1\u7406\u529f\u80fd, \u9ed8\u8ba4true\n private boolean JMX_ENABLED;\n \n \/\/\u5728borrow\u4e00\u4e2ajedis\u5b9e\u4f8b\u65f6\uff0c\u662f\u5426\u63d0\u524d\u8fdb\u884cvalidate\u64cd\u4f5c\uff1b\u5982\u679c\u4e3atrue\uff0c\u5219\u5f97\u5230\u7684jedis\u5b9e\u4f8b\u5747\u662f\u53ef\u7528\u7684\uff1b\n private boolean TEST_ON_BORROW ;\n \n \/\/\u670d\u52a1\u5668\u5bc6\u7801\n private String REDIS_PASS;\n \/\/redis\u9009\u62e9\u6570\u636e\u5e93DB\n private int REDIS_DB;\n \n \n private String LUASHA;\n \n private Map\n   \n     configure = null;\n \n \/**\n  * \u6839\u636e\u914d\u7f6e\u6587\u4ef6,\u5c06RedisPool\u8fde\u63a5\u914d\u7f6e\u521d\u59cb\u5316\n  *\/\n public RedisPoolConfigure(){\n  try {\n  configure = new ConfigureReader().readProperties(\"redis.properties\");\n } catch (IOException e) {\n e.printStackTrace();\n }\n  this.ADDR = configure.get(\"REDIS.ADDR\");\n  this.LUASHA = configure.get(\"REDIS.LUA_HASH\");\n  this.EVICTION_POLICY_CLASS_NAME = configure.get(\"REDIS.EVICTION_POLICY_CLASS_NAME\");\n  this.BLOCK_WHEN_EXHAUSTED = Boolean.parseBoolean(configure.get(\"REDIS.BLOCK_WHEN_EXHAUSTED\"));\n  this.JMX_ENABLED = Boolean.parseBoolean(configure.get(\"REDIS.JMX_ENABLED\"));\n  this.TEST_ON_BORROW = Boolean.parseBoolean(configure.get(\"REDIS.TEST_ON_BORROW\"));\n  this.REDIS_PASS=configure.get(\"REDIS.PASS\");\n  \n  if(typeCheck()){\n  this.PORT = new Integer(configure.get(\"REDIS.PORT\"));\n  this.MAX_ACTIVE = new Integer(configure.get(\"REDIS.MAX_ACTIVE\"));\n  this.MAX_IDLE = new Integer(configure.get(\"REDIS.MAX_IDLE\"));\n  this.MAX_WAIT = new Integer(configure.get(\"REDIS.MAX_WAIT\"));\n  this.REDIS_DB=new Integer(configure.get(\"REDIS.DB\"));\n  }else{\n  System.out.println(\"error\");\n  }\n }\n \n \/**\n  * \u8f85\u52a9\u5de5\u5177,\u68c0\u67e5map\u4e2d\u6570\u636e\u7684\u7c7b\u578b\n  * @return\n  *\/\n private boolean typeCheck() {\n if (isNumeric(configure.get(\"REDIS.PORT\")) \n &amp;&amp; isNumeric(configure.get(\"REDIS.MAX_ACTIVE\"))\n &amp;&amp; isNumeric(configure.get(\"REDIS.MAX_IDLE\")) \n &amp;&amp; isNumeric(configure.get(\"REDIS.MAX_WAIT\"))\n &amp;&amp; isNumeric(configure.get(\"REDIS.DB\"))) {\n return true;\n } \n return false;\n }\n\n public String getADDR() {\n return ADDR;\n }\n\n public int getPORT() {\n return PORT;\n }\n\n\n public int getMAX_ACTIVE() {\n return MAX_ACTIVE;\n }\n\n public int getMAX_IDLE() {\n return MAX_IDLE;\n }\n\n public int getMAX_WAIT() {\n return MAX_WAIT;\n }\n\n public int getTIME_OUT() {\n return TIME_OUT;\n }\n\n public boolean isTEST_ON_BORROW() {\n return TEST_ON_BORROW;\n }\n\n public String getEVICTION_POLICY_CLASS_NAME() {\n return EVICTION_POLICY_CLASS_NAME;\n }\n\n public boolean isBLOCK_WHEN_EXHAUSTED() {\n return BLOCK_WHEN_EXHAUSTED;\n }\n\n public boolean isJMX_ENABLED() {\n return JMX_ENABLED;\n }\n \/**\n * \u5224\u65ad\u4f20\u5165\u7684\u6570\u636e\u662f\u5426\u4e3a\u7eaf\u6570\u5b57\u6784\u6210\n * @param str\n * @return\n *\/\n public boolean isNumeric(String str) {\n if(str==null || \"\".equals(str)){\n return false;\n }\n for (int i = 0; i \n\n\n    <p>redis\u8fde\u63a5\u6c60\u521d\u59cb\u5316\u3001\u83b7\u53d6Jedis\u5b9e\u4f8b\u548c\u91ca\u653eJedis\u5b9e\u4f8b<\/p>\n\n\n    <pre>\n\/**\n * jedis\u7684\u8fde\u63a5\u6c60,\u8fd4\u56de\u672a\u5c01\u88c5\u7684jedis\u5bf9\u8c61\n * \u4e00\u822c\u53ea\u6709\u5728RedisCache\u7c7b\u63d0\u4f9b\u7684\u64cd\u4f5c\u7c92\u5ea6\u4e0d\u8db3\u4f7f\u7528\u65f6\u624d\u4f7f\u7528\u6b64\u7c7b\u63d0\u4f9b\u7684\u539f\u751fjedis\u65b9\u6cd5\n * @author Hector\n *\n *\/\npublic class RedisPool {\n \n private static JedisPool jedisPool = null;\n \n \/**\n  * \u521d\u59cb\u5316Redis\u8fde\u63a5\u6c60\n  *\/\n static {\n  try {\n   RedisPoolConfigure configure = new RedisPoolConfigure();\n   JedisPoolConfig config = new JedisPoolConfig();\n   config.setBlockWhenExhausted(configure.isBLOCK_WHEN_EXHAUSTED());\n   config.setEvictionPolicyClassName(configure.getEVICTION_POLICY_CLASS_NAME());\n   config.setJmxEnabled(configure.isJMX_ENABLED());\n   config.setMaxIdle(configure.getMAX_IDLE());\n   config.setMaxTotal(configure.getMAX_ACTIVE());\n   config.setMaxWaitMillis(configure.getMAX_WAIT());\n   config.setTestOnBorrow(configure.isTEST_ON_BORROW());\n   jedisPool = new JedisPool(config, configure.getADDR(), configure.getPORT(), configure.getTIME_OUT(),configure.getREDIS_PASS(),configure.getREDIS_DB());\n  } catch (Exception e) {\n   e.printStackTrace();\n  }\n }\n \n \/**\n  * \u83b7\u53d6Jedis\u5b9e\u4f8b\n  * @return\n  *\/\n public synchronized static Jedis getJedis() {\n  Jedis resource=null;\n  try {\n   if (jedisPool != null) {\n    resource = jedisPool.getResource();\n    return resource;\n   } else {\n    return null;\n   }\n  } catch (Exception e) {\n   e.printStackTrace();\n   return null;\n  }\n }\n \n \n \/**\n  * \u91ca\u653ejedis\u8d44\u6e90\n  * @param jedis\n  *\/\n public static void close(final Jedis jedis) {\n  if (jedis != null) {\n   jedis.close();\n  }\n }\n \n public static JedisPool getJedisPool() {\n return jedisPool;\n }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u8fde\u63a5\u6c60\u914d\u7f6e\u53ca\u521d\u59cb\u5316\u5b9e\u73b0&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201321","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201321"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201321\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}