{"id":201628,"date":"2025-05-10T09:26:16","date_gmt":"2025-05-10T01:26:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201628\/"},"modified":"2025-05-10T09:26:16","modified_gmt":"2025-05-10T01:26:16","slug":"redis%e7%bc%93%e5%ad%98%e5%b7%a5%e5%85%b7%e5%b0%81%e8%a3%85%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201628\/","title":{"rendered":"Redis\u7f13\u5b58\u5de5\u5177\u5c01\u88c5\u5b9e\u73b0"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u7f13\u5b58\u5de5\u5177\u5c01\u88c5\u5b9e\u73b0<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aRedis\u7f13\u5b58\u5de5\u5177\u5c01\u88c5\u5b9e\u73b0\u300b\uff0c\u8986\u76d6\u4e86\u6570\u636e\u5e93\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>\u5c06 StringRedisTemplate \u5c01\u88c5\u6210\u4e00\u4e2a\u7f13\u5b58\u5de5\u5177\u7c7b\uff0c\u65b9\u4fbf\u4ee5\u540e\u91cd\u590d\u4f7f\u7528\u3002<\/p>\n<h2>1. \u65b9\u6cd5\u8981\u6c42<\/h2>\n<p>\u5728\u8fd9\u4e2a\u5de5\u5177\u7c7b\u4e2d\u6211\u4eec\u5b8c\u6210\u56db\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u65b9\u6cd5\u2460\uff1a\u5c06\u4efb\u610fJava\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3ajson\u5e76\u5b58\u50a8\u5728string\u7c7b\u578b\u7684key\u4e2d\uff0c\u5e76\u4e14\u53ef\u4ee5\u8bbe\u7f6eTTL\u8fc7\u671f\u65f6\u95f4<\/li>\n<li>\u65b9\u6cd5\u2461\uff1a\u5c06\u4efb\u610fJava\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3ajson\u5e76\u5b58\u50a8\u5728string\u7c7b\u578b\u7684key\u4e2d\uff0c\u5e76\u4e14\u53ef\u4ee5\u8bbe\u7f6e\u903b\u8f91\u8fc7\u671f\u65f6\u95f4\uff0c\u7528\u4e8e\u5904\u7406\u7f13\u5b58\u51fb\u7a7f\u95ee\u9898<\/li>\n<li>\u65b9\u6cd5\u2462\uff1a\u6839\u636e\u6307\u5b9a\u7684key\u67e5\u8be2\u7f13\u5b58\uff0c\u5e76\u53cd\u5e8f\u5217\u5316\u4e3a\u6307\u5b9a\u7c7b\u578b\uff0c\u5229\u7528\u7f13\u5b58\u7a7a\u503c\u7684\u65b9\u5f0f\u89e3\u51b3\u7f13\u5b58\u7a7f\u900f\u95ee\u9898<\/li>\n<li>\u65b9\u6cd5\u2463\uff1a\u6839\u636e\u6307\u5b9a\u7684key\u67e5\u8be2\u7f13\u5b58\uff0c\u5e76\u53cd\u5e8f\u5217\u5316\u4e3a\u6307\u5b9a\u7c7b\u578b\uff0c\u9700\u8981\u5229\u7528\u903b\u8f91\u8fc7\u671f\u89e3\u51b3\u7f13\u5b58\u51fb\u7a7f\u95ee\u9898<\/li>\n<\/ul>\n<p>\u6211\u4eec\u65b0\u5efa\u4e00\u4e2a\u7c7b\uff0c\u5148\u628a\u5927\u81f4\u6846\u67b6\u5199\u51fa\u6765\uff0c\u65b9\u6cd5\u7684\u53c2\u6570\u53ef\u4ee5\u8fb9\u5199\u8fb9\u5b8c\u5584\uff0c\u4f46\u662f\u6211\u7684\u65b9\u6cd5\u53c2\u6570\u5df2\u7ecf\u5b8c\u5584\u597d\u4e86\uff1a<\/p>\n<pre>@Component\npublic class CacheClient {\n\n&nbsp; &nbsp; private final StringRedisTemplate stringRedisTemplate;\n\n&nbsp; &nbsp; public CacheClient(StringRedisTemplate stringRedisTemplate) {\n&nbsp; &nbsp; &nbsp; &nbsp; this.stringRedisTemplate = stringRedisTemplate;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; \/\/\u65b9\u6cd5\u4e00\n&nbsp; &nbsp; public void set(String key, Object value, Long time, TimeUnit unit) {\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; \/\/\u65b9\u6cd5\u4e8c\n&nbsp; &nbsp; public void setWithLogicExpire(String key, Object value, Long time, TimeUnit unit) {\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; \/\/\u65b9\u6cd5\u4e09\n&nbsp; &nbsp; public \n   \n     R queryWithPassThrough(String keyPrefix, ID id, Class\n    \n      type, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Long time, TimeUnit unit, Function\n     \n       dbFallback) { &nbsp; &nbsp; } &nbsp; &nbsp; \/\/\u65b9\u6cd5\u56db &nbsp; &nbsp; public \n      \n        R queryWithLogicalExpire(String prefix, ID id, String lockPre, Class\n       \n         type, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Long time, TimeUnit unit, Function\n        \n          dbFallback) { &nbsp; &nbsp; } &nbsp; &nbsp; \/\/\u7ebf\u7a0b\u6c60 &nbsp; &nbsp; private static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10); &nbsp; &nbsp; \/\/\u83b7\u53d6\u9501 &nbsp; &nbsp; private boolean tryLock(String key) { &nbsp; &nbsp; &nbsp; &nbsp; Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, \"1\", LOCK_SHOP_TTL, TimeUnit.SECONDS); &nbsp; &nbsp; &nbsp; &nbsp; return BooleanUtil.isTrue(flag); &nbsp; &nbsp; } &nbsp; &nbsp; \/\/\u91ca\u653e\u9501 &nbsp; &nbsp; private void unLock(String key) { &nbsp; &nbsp; &nbsp; &nbsp; stringRedisTemplate.delete(key); &nbsp; &nbsp; } }\n        \n       \n      \n     \n    \n   <\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u53ef\u4ee5\u4e0d\u65ad\u5b8c\u5584\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<h3>1.1 \u65b9\u6cd5\u4e00<\/h3>\n<pre>public void set(String key, Object value, Long time, TimeUnit unit) {\n&nbsp; &nbsp; stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value), time, unit);\n}<\/pre>\n<h3>1.2 \u65b9\u6cd5\u4e8c<\/h3>\n<pre>public void setWithLogicExpire(String key, Object value, Long time, TimeUnit unit) {\n&nbsp; &nbsp; RedisData redisData = new RedisData();\n&nbsp; &nbsp; redisData.setData(value);\n&nbsp; &nbsp; redisData.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));\n&nbsp; &nbsp; stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData));\n}<\/pre>\n<h3>1.3 \u65b9\u6cd5\u4e09<\/h3>\n<pre>public \n   \n     R queryWithPassThrough(String keyPrefix, ID id, Class\n    \n      type, Long time, TimeUnit unit, Function\n     \n       dbFallback) { String key = keyPrefix + id; \/\/1.\u4eceredis\u4e2d\u67e5\u8be2\u5546\u94fa\u7f13\u5b58 String json = stringRedisTemplate.opsForValue().get(key); \/\/2.\u5224\u65ad\u662f\u5426\u5b58\u5728 if (StrUtil.isNotBlank(json)) { \/\/2.1.\u5b58\u5728 return JSONUtil.toBean(json, type); } \/\/2.2.\u4e0d\u5b58\u5728 \/\/\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\u503c if (json != null) { \/\/\u4e0d\u4e3anull\uff0c\u5219\u5fc5\u4e3a\u7a7a return null; } \/\/3.\u67e5\u8be2\u6570\u636e\u5e93 R r = dbFallback.apply(id); if (r == null) { \/\/3.1.\u4e0d\u5b58\u5728\uff0c\u7f13\u5b58\u7a7a\u503c stringRedisTemplate.opsForValue().set(key, \"\", CACHE_NULL_TTL, TimeUnit.MINUTES); } else { \/\/3.2.\u5b58\u5728\uff0c\u7f13\u5b58\u6570\u636e this.set(key, r, time, unit); } return r; } \n     \n    \n   <\/pre>\n<p>\u65b9\u6cd5\u4e09\u7528\u5230\u4e86\u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u8fd9\u91cc\u975e\u5e38\u5de7\u5999\uff0c\u987a\u4fbf\u518d\u8d34\u4e00\u4e0b\u8c03\u7528\u65b9\u6cd5\u662f\u600e\u6837\u8c03\u7528\u7684\uff1a<\/p>\n<pre>Shop shop = cacheClient.queryWithPassThrough(CACHE_SHOP_KEY,id,Shop.class,CACHE_SHOP_TTL,TimeUnit.MINUTES,this::getById);\n<\/pre>\n<h3>1.4 \u65b9\u6cd5\u56db<\/h3>\n<pre>public \n   \n     R queryWithLogicalExpire(String prefix, ID id, String lockPre, Class\n    \n      type, Long time, TimeUnit unit, Function\n     \n       dbFallback) { \/\/1.\u4eceredis\u67e5\u8be2\u5546\u94fa\u7f13\u5b58 String key = prefix + id; String json = stringRedisTemplate.opsForValue().get(key); \/\/2.\u5224\u65ad\u662f\u5426\u5b58\u5728 if (StrUtil.isBlank(json)) { \/\/\u672a\u547d\u4e2d\uff0c\u76f4\u63a5\u8fd4\u56de\u7a7a return null; } \/\/3.\u547d\u4e2d\uff0c\u5224\u65ad\u662f\u5426\u8fc7\u671f RedisData redisData = JSONUtil.toBean(json, RedisData.class); R r = JSONUtil.toBean((JSONObject) redisData.getData(), type); if (redisData.getExpireTime().isAfter(LocalDateTime.now())) { \/\/3.1\u672a\u8fc7\u671f,\u76f4\u63a5\u8fd4\u56de\u5e97\u94fa\u4fe1\u606f return r; } \/\/3.2.\u5df2\u8fc7\u671f\uff0c\u7f13\u5b58\u91cd\u5efa \/\/3.3.\u83b7\u53d6\u9501 String lockKey = lockPre + id; boolean flag = tryLock(lockKey); if (flag) { \/\/3.4.\u83b7\u53d6\u6210\u529f \/\/4\u518d\u6b21\u68c0\u67e5redis\u7f13\u5b58\u662f\u5426\u8fc7\u671f\uff0c\u505adouble check json = stringRedisTemplate.opsForValue().get(key); \/\/4.1.\u5224\u65ad\u662f\u5426\u5b58\u5728 if (StrUtil.isBlank(json)) { \/\/\u672a\u547d\u4e2d\uff0c\u76f4\u63a5\u8fd4\u56de\u7a7a return null; } \/\/4.2.\u547d\u4e2d\uff0c\u5224\u65ad\u662f\u5426\u8fc7\u671f redisData = JSONUtil.toBean(json, RedisData.class); r = JSONUtil.toBean((JSONObject) redisData.getData(), type); if (redisData.getExpireTime().isAfter(LocalDateTime.now())) { \/\/4.3.\u672a\u8fc7\u671f,\u76f4\u63a5\u8fd4\u56de\u5e97\u94fa\u4fe1\u606f return r; } \/\/4.4\u8fc7\u671f\uff0c\u8fd4\u56de\u65e7\u6570\u636e CACHE_REBUILD_EXECUTOR.submit(() -&gt; { \/\/5.\u91cd\u5efa\u7f13\u5b58 try { R r1 = dbFallback.apply(id); this.setWithLogicExpire(key, r1, time, unit); } catch (Exception e) { throw new RuntimeException(e); } finally { \/\/\u91ca\u653e\u9501 unLock(lockKey); } }); } \/\/7.\u83b7\u53d6\u5931\u8d25,\u8fd4\u56de\u65e7\u6570\u636e return r; } \n     \n    \n   <\/pre>\n<h2>2. \u5b8c\u6574\u5de5\u5177\u7c7b\u4ee3\u7801<\/h2>\n<pre>@Component\n@Slf4j\npublic class CacheClient {\n\n&nbsp; &nbsp; private final StringRedisTemplate stringRedisTemplate;\n\n&nbsp; &nbsp; public CacheClient(StringRedisTemplate stringRedisTemplate) {\n&nbsp; &nbsp; &nbsp; &nbsp; this.stringRedisTemplate = stringRedisTemplate;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public void set(String key, Object value, Long time, TimeUnit unit) {\n&nbsp; &nbsp; &nbsp; &nbsp; stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value), time, unit);\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public void setWithLogicExpire(String key, Object value, Long time, TimeUnit unit) {\n&nbsp; &nbsp; &nbsp; &nbsp; RedisData redisData = new RedisData();\n&nbsp; &nbsp; &nbsp; &nbsp; redisData.setData(value);\n&nbsp; &nbsp; &nbsp; &nbsp; redisData.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));\n&nbsp; &nbsp; &nbsp; &nbsp; stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData));\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; public \n   \n     R queryWithPassThrough(String keyPrefix, ID id, Class\n    \n      type, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Long time, TimeUnit unit, Function\n     \n       dbFallback) { &nbsp; &nbsp; &nbsp; &nbsp; String key = keyPrefix + id; &nbsp; &nbsp; &nbsp; &nbsp; \/\/1.\u4eceredis\u4e2d\u67e5\u8be2\u5546\u94fa\u7f13\u5b58 &nbsp; &nbsp; &nbsp; &nbsp; String json = stringRedisTemplate.opsForValue().get(key); &nbsp; &nbsp; &nbsp; &nbsp; \/\/2.\u5224\u65ad\u662f\u5426\u5b58\u5728 &nbsp; &nbsp; &nbsp; &nbsp; if (StrUtil.isNotBlank(json)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/2.1.\u5b58\u5728 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return JSONUtil.toBean(json, type); &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; \/\/2.2.\u4e0d\u5b58\u5728 &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\u503c &nbsp; &nbsp; &nbsp; &nbsp; if (json != null) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u4e0d\u4e3anull\uff0c\u5219\u5fc5\u4e3a\u7a7a &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; \/\/3.\u67e5\u8be2\u6570\u636e\u5e93 &nbsp; &nbsp; &nbsp; &nbsp; R r = dbFallback.apply(id); &nbsp; &nbsp; &nbsp; &nbsp; if (r == null) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/3.1.\u4e0d\u5b58\u5728\uff0c\u7f13\u5b58\u7a7a\u503c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringRedisTemplate.opsForValue().set(key, \"\", CACHE_NULL_TTL, TimeUnit.MINUTES); &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/3.2.\u5b58\u5728\uff0c\u7f13\u5b58\u6570\u636e &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.set(key, r, time, unit); &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; return r; &nbsp; &nbsp; } &nbsp; &nbsp; public \n      \n        R queryWithLogicalExpire(String prefix, ID id, String lockPre, Class\n       \n         type, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Long time, TimeUnit unit, Function\n        \n          dbFallback) { &nbsp; &nbsp; &nbsp; &nbsp; \/\/1.\u4eceredis\u67e5\u8be2\u5546\u94fa\u7f13\u5b58 &nbsp; &nbsp; &nbsp; &nbsp; String key = prefix + id; &nbsp; &nbsp; &nbsp; &nbsp; String json = stringRedisTemplate.opsForValue().get(key); &nbsp; &nbsp; &nbsp; &nbsp; \/\/2.\u5224\u65ad\u662f\u5426\u5b58\u5728 &nbsp; &nbsp; &nbsp; &nbsp; if (StrUtil.isBlank(json)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u672a\u547d\u4e2d\uff0c\u76f4\u63a5\u8fd4\u56de\u7a7a &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; \/\/3.\u547d\u4e2d\uff0c\u5224\u65ad\u662f\u5426\u8fc7\u671f &nbsp; &nbsp; &nbsp; &nbsp; RedisData redisData = JSONUtil.toBean(json, RedisData.class); &nbsp; &nbsp; &nbsp; &nbsp; R r = JSONUtil.toBean((JSONObject) redisData.getData(), type); &nbsp; &nbsp; &nbsp; &nbsp; if (redisData.getExpireTime().isAfter(LocalDateTime.now())) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/3.1\u672a\u8fc7\u671f,\u76f4\u63a5\u8fd4\u56de\u5e97\u94fa\u4fe1\u606f &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return r; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; \/\/3.2.\u5df2\u8fc7\u671f\uff0c\u7f13\u5b58\u91cd\u5efa &nbsp; &nbsp; &nbsp; &nbsp; \/\/3.3.\u83b7\u53d6\u9501 &nbsp; &nbsp; &nbsp; &nbsp; String lockKey = lockPre + id; &nbsp; &nbsp; &nbsp; &nbsp; boolean flag = tryLock(lockKey); &nbsp; &nbsp; &nbsp; &nbsp; if (flag) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/3.4.\u83b7\u53d6\u6210\u529f &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/4\u518d\u6b21\u68c0\u67e5redis\u7f13\u5b58\u662f\u5426\u8fc7\u671f\uff0c\u505adouble check &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json = stringRedisTemplate.opsForValue().get(key); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/4.1.\u5224\u65ad\u662f\u5426\u5b58\u5728 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (StrUtil.isBlank(json)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u672a\u547d\u4e2d\uff0c\u76f4\u63a5\u8fd4\u56de\u7a7a &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/4.2.\u547d\u4e2d\uff0c\u5224\u65ad\u662f\u5426\u8fc7\u671f &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redisData = JSONUtil.toBean(json, RedisData.class); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r = JSONUtil.toBean((JSONObject) redisData.getData(), type); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (redisData.getExpireTime().isAfter(LocalDateTime.now())) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/4.3.\u672a\u8fc7\u671f,\u76f4\u63a5\u8fd4\u56de\u5e97\u94fa\u4fe1\u606f &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return r; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/4.4\u8fc7\u671f\uff0c\u8fd4\u56de\u65e7\u6570\u636e &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CACHE_REBUILD_EXECUTOR.submit(() -&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/5.\u91cd\u5efa\u7f13\u5b58 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R r1 = dbFallback.apply(id); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.setWithLogicExpire(key, r1, time, unit); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } finally { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u91ca\u653e\u9501 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unLock(lockKey); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; \/\/7.\u83b7\u53d6\u5931\u8d25,\u8fd4\u56de\u65e7\u6570\u636e &nbsp; &nbsp; &nbsp; &nbsp; return r; &nbsp; &nbsp; } &nbsp; &nbsp; private static final ExecutorService CACHE_REBUILD_EXECUTOR = Executors.newFixedThreadPool(10); &nbsp; &nbsp; \/\/\u83b7\u53d6\u9501 &nbsp; &nbsp; private boolean tryLock(String key) { &nbsp; &nbsp; &nbsp; &nbsp; Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, \"1\", LOCK_SHOP_TTL, TimeUnit.SECONDS); &nbsp; &nbsp; &nbsp; &nbsp; return BooleanUtil.isTrue(flag); &nbsp; &nbsp; } &nbsp; &nbsp; \/\/\u91ca\u653e\u9501 &nbsp; &nbsp; private void unLock(String key) { &nbsp; &nbsp; &nbsp; &nbsp; stringRedisTemplate.delete(key); &nbsp; &nbsp; } }\n        \n       \n      \n     \n    \n   <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u7f13\u5b58\u5de5\u5177\u5c01\u88c5\u5b9e\u73b0 \u6536\u85cf&#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-201628","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201628","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=201628"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201628\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}