{"id":201313,"date":"2025-05-10T08:37:37","date_gmt":"2025-05-10T00:37:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201313\/"},"modified":"2025-05-10T08:37:37","modified_gmt":"2025-05-10T00:37:37","slug":"redis-%e9%9b%86%e7%be%a4%e6%89%b9%e9%87%8f%e6%93%8d%e4%bd%9c%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201313\/","title":{"rendered":"redis \u96c6\u7fa4\u6279\u91cf\u64cd\u4f5c\u5b9e\u73b0"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>redis \u96c6\u7fa4\u6279\u91cf\u64cd\u4f5c\u5b9e\u73b0<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aredis \u96c6\u7fa4\u6279\u91cf\u64cd\u4f5c\u5b9e\u73b0\u300b\uff0c\u6d89\u53ca\u5230\u64cd\u4f5c\u3001\u6279\u91cf\u3001redis\u96c6\u7fa4\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p>&nbsp;Redis\u96c6\u7fa4\u662f\u6ca1\u6cd5\u6267\u884c\u6279\u91cf\u64cd\u4f5c\u547d\u4ee4\u7684\uff0c\u5982mget\uff0cpipeline\u7b49\u3002\u8fd9\u662f\u56e0\u4e3aredis\u5c06\u96c6\u7fa4\u5212\u5206\u4e3a16383\u4e2a\u54c8\u5e0c\u69fd\uff0c\u4e0d\u540c\u7684key\u4f1a\u5212\u5206\u5230\u4e0d\u540c\u7684\u69fd\u4e2d\u3002\u4f46\u662f\uff0cJedis\u5ba2\u6237\u7aef\u63d0\u4f9b\u4e86\u8ba1\u7b97key\u7684slot\u65b9\u6cd5\uff0c\u5df2\u7ecfslot\u548c\u8282\u70b9\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u901a\u8fc7\u8fd9\u4e24\u4e2a\u6570\u636e\uff0c\u5c31\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6bcf\u4e2akey\u6240\u5728\u7684\u8282\u70b9\uff0c\u7136\u540e\u4f7f\u7528pipeline\u83b7\u53d6\u6570\u636e\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>\u521d\u59cb\u5316&nbsp;&nbsp;&nbsp; JedisCluster\u7c7b<\/p>\n<pre>\n@Configuration\npublic class JedisClusterConfig {\n\n    @Value(\"${spring.redis.cluster.nodes}\")\n    private String clusterNodes;\n\n    @Value(\"${spring.redis.cache.commandTimeout}\")\n    private Integer commandTimeout;\n\n    @Bean\n    public JedisCluster getJedisCluster() {\n\n        String[] serverArray = clusterNodes.split(\",\");\n        Set\n   \n     nodes = new HashSet();\n        for (String ipPort : serverArray) {\n            String[] ipPortPair = ipPort.split(\":\");\n            nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));\n        }\n        return new JedisCluster(nodes, commandTimeout);\n    }\n}\n\n\n   <\/pre>\n<p>\u5de5\u5177\u7c7b JedisClusterUtil<\/p>\n<pre>\n@Component\npublic class JedisClusterUtil {\n\n    @Autowired\n    private JedisCluster jedisCluster;\n\n    @Resource(name = \"redisTemplate4Json\")\n    protected RedisTemplate\n   \n     redisTemplate;\n\n    \/**\n     * ZSet\u6279\u91cf\u67e5\u8be2\n     * @param keys\n     * @return\n     *\/\n    public List\n     batchZRange(List\n     \n       keys) { List\n       resList = new ArrayList(); if (keys == null || keys.size() == 0) { return resList; } if (keys.size() == 1) { BoundZSetOperations\n       \n         operations = redisTemplate.boundZSetOps(keys.get(0)); Set\n         set = operations.reverseRange(0, 0); resList.add(set.iterator().next()); return resList; } Map\n         \n          &gt; jedisPoolMap = getJedisPool(keys); List\n          \n            keyList; JedisPool currentJedisPool = null; Pipeline currentPipeline; List\n            res = new ArrayList(); Map\n            \n              resultMap = new HashMap(); \/\/\u6267\u884c for (Map.Entry\n             \n              &gt; entry : jedisPoolMap.entrySet()) { Jedis jedis = null; try { currentJedisPool = entry.getKey(); keyList = entry.getValue(); \/\/\u83b7\u53d6pipeline jedis = currentJedisPool.getResource(); currentPipeline = jedis.pipelined(); for (String key : keyList) { currentPipeline.zrevrange(key, 0, 0); } \/\/\u4ecepipeline\u4e2d\u83b7\u53d6\u7ed3\u679c res = currentPipeline.syncAndReturnAll(); currentPipeline.close(); for (int i = 0; i set = (Set\n              ) res.get(i); if (null == set || set.isEmpty()) { resultMap.put(keyList.get(i), null); } else { byte[] byteStr = set.iterator().next().toString().getBytes(); Object obj = redisTemplate.getDefaultSerializer().deserialize(byteStr); resultMap.put(keyList.get(i), obj); } } } } catch (Exception e) { e.printStackTrace(); } finally { returnResource(jedis, currentJedisPool); } } resList = sortList(keys, resultMap); return resList; } \/** * Value\u6279\u91cf\u67e5\u8be2 * @param keys * @return *\/ public List batchGet(List\n                \n                  keys){ List\n                  resList = new ArrayList(); if (keys == null || keys.size() == 0) { return resList; } if (keys.size() == 1) { BoundValueOperations\n                  \n                    operations = redisTemplate.boundValueOps(keys.get(0)); resList.add(operations.get()); return resList; } Map\n                   \n                    &gt; jedisPoolMap = getJedisPool(keys); List\n                    \n                      keyList; JedisPool currentJedisPool = null; Pipeline currentPipeline; List\n                      res = new ArrayList(); Map\n                      \n                        resultMap = new HashMap(); for (Map.Entry\n                       \n                        &gt; entry : jedisPoolMap.entrySet()) { Jedis jedis = null; try { currentJedisPool = entry.getKey(); keyList = entry.getValue(); \/\/\u83b7\u53d6pipeline jedis = currentJedisPool.getResource(); currentPipeline = jedis.pipelined(); for (String key : keyList) { currentPipeline.get(key); } \/\/\u4ecepipeline\u4e2d\u83b7\u53d6\u7ed3\u679c res = currentPipeline.syncAndReturnAll(); currentPipeline.close(); for (int i = 0; i &gt; getJedisPool(List\n                        \n                          keys){ \/\/JedisCluster\u7ee7\u627f\u4e86BinaryJedisCluster \/\/BinaryJedisCluster\u7684JedisClusterConnectionHandler\u5c5e\u6027 \/\/\u91cc\u9762\u6709JedisClusterInfoCache\uff0c\u6839\u636e\u8fd9\u4e00\u6761\u7ee7\u627f\u94fe\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230JedisClusterInfoCache \/\/\u4ece\u800c\u83b7\u53d6slot\u548cJedisPool\u76f4\u63a5\u7684\u6620\u5c04 MetaObject metaObject = SystemMetaObject.forObject(jedisCluster); JedisClusterInfoCache cache = (JedisClusterInfoCache) metaObject.getValue(\"connectionHandler.cache\"); \/\/\u4fdd\u5b58\u5730\u5740+\u7aef\u53e3\u548c\u547d\u4ee4\u7684\u6620\u5c04 Map\n                         \n                          &gt; jedisPoolMap = new HashMap(); JedisPool currentJedisPool = null; List\n                          \n                            keyList; for (String key : keys) { \/\/\u8ba1\u7b97\u54c8\u5e0c\u69fd int crc = JedisClusterCRC16.getSlot(key); \/\/\u901a\u8fc7\u54c8\u5e0c\u69fd\u83b7\u53d6\u8282\u70b9\u7684\u8fde\u63a5 currentJedisPool = cache.getSlotPool(crc); \/\/\u7531\u4e8eJedisPool\u4f5c\u4e3avalue\u4fdd\u5b58\u5728JedisClusterInfoCache\u4e2d\u7684\u4e00\u4e2amap\u5bf9\u8c61\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684 \/\/JedisPool\u5728map\u7684\u521d\u59cb\u5316\u9636\u6bb5\u5c31\u662f\u786e\u5b9a\u7684\u548c\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u83b7\u53d6\u5230\u7684\u6bcf\u4e2a\u8282\u70b9\u7684JedisPool\u90fd\u662f\u4e00\u6837 \/\/\u7684\uff0c\u53ef\u4ee5\u4f5c\u4e3amap\u7684key if (jedisPoolMap.containsKey(currentJedisPool)) { jedisPoolMap.get(currentJedisPool).add(key); } else { keyList = new ArrayList(); keyList.add(key); jedisPoolMap.put(currentJedisPool, keyList); } } return jedisPoolMap; } private List\n                            sortList(List\n                            \n                              keys, Map\n                             \n                               params) { List\n                               resultList = new ArrayList(); Iterator\n                               \n                                 it = keys.iterator(); while (it.hasNext()) { String key = it.next(); resultList.add(params.get(key)); } return resultList; } \/** * \u91ca\u653ejedis\u8d44\u6e90 * * @param jedis *\/ public void returnResource(Jedis jedis, JedisPool jedisPool) { if (jedis != null &amp;&amp; jedisPool != null) { jedisPool.returnResource(jedis); } }\n                               \n                             \n                            \n                          \n                         \n                        \n                       \n                      \n                    \n                   \n                  \n                \n             \n            \n          \n         \n       \n     \n   <\/pre>\n<p>&nbsp;\u6ce8\u610f\uff1a\u4e00\u5b9a\u8981\u5b8c\u6210\u540e\u91ca\u653e jedis \u8d44\u6e90&nbsp; \u4e0d\u7136\u4f1a\u9020\u6210\u5361\u6b7b\u73b0\u8c61<\/p>\n","protected":false},"excerpt":{"rendered":"<p>redis \u96c6\u7fa4\u6279\u91cf\u64cd\u4f5c\u5b9e\u73b0 \u6536&#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-201313","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201313","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=201313"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201313\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}