{"id":201477,"date":"2025-05-10T11:46:33","date_gmt":"2025-05-10T03:46:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201477\/"},"modified":"2025-05-10T11:46:33","modified_gmt":"2025-05-10T03:46:33","slug":"%e4%bd%bf%e7%94%a8luaredis%e8%a7%a3%e5%86%b3%e5%8f%91%e5%a4%9a%e5%bc%a0%e5%88%b8%e7%9a%84%e5%b9%b6%e5%8f%91%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201477\/","title":{"rendered":"\u4f7f\u7528lua+redis\u89e3\u51b3\u53d1\u591a\u5f20\u5238\u7684\u5e76\u53d1\u95ee\u9898"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f7f\u7528lua+redis\u89e3\u51b3\u53d1\u591a\u5f20\u5238\u7684\u5e76\u53d1\u95ee\u9898<\/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\u300a\u4f7f\u7528lua+redis\u89e3\u51b3\u53d1\u591a\u5f20\u5238\u7684\u5e76\u53d1\u95ee\u9898\u300b\uff0c\u804a\u804a\u5e76\u53d1\u3001luaredis\u3001\u591a\u5f20\u5238\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<h2>\u4e1a\u52a1\u63cf\u8ff0<\/h2>\n<p>\u8fd9\u4e2a\u63a5\u53e3\u7684\u4f5c\u7528\u662f\u7ed9\u4f1a\u5458\u53d1\u591a\u5f20\u5238\u7801\u3002\u6d89\u53ca\u52304\u5f20\u4e3b\u4f53\uff0c\u5206\u522b\u662f\uff1a\u7528\u6237\uff0c\u5238\uff0c\u5238\u7801\uff0c\u7528\u6237\u9886\u53d6\u8bb0\u5f55\u3002<br \/> \u4e0b\u9762\u662f\u6539\u9020\u524d\u7684\u4f2a\u4ee3\u7801\u3002<br \/> \u4e3b\u8981\u662f\u56e0\u4e3a\u67e5\u51fa\u5238\u7801\u90a3\u884c\u5b58\u5728\u5e76\u53d1\u5b89\u5168\u95ee\u9898\uff0c\u591a\u4e2a\u7ebf\u7a0b\u62ff\u5230\u540c\u51e0\u4e2a\u5238\u7801\u3002\u4ee5\u4e0b\u90fd\u662f\u57fa\u4e8e\u5982\u4f55\u8ba9\u53d6\u5238\u7801\u53d8\u6210\u539f\u5b50\u7684\u53bb\u5c55\u5f00\u3002<\/p>\n<pre>\npublic boolean sendCoupons(Long userId, Long couponId) {\n \/\/ \u4e00\u5806\u6821\u9a8c\n \/\/ ...\n \/\/ \u67e5\u51fa\u5238\u7801\n List\n   \n     couponCodes = couponCodeService.findByCouponId(couponId, num);\n \/\/ batchUpdateStatus\u662f\u4e00\u4e2a\u88ab@Transactional(propagation = Propagation.REQUIRES_NEW)\u4fee\u9970\u7684\u65b9\u6cd5\n \/\/ \u6279\u91cf\u66f4\u65b0\u4e3a\u5df2\u88ab\u9886\u53d6\u72b6\u6001\n couponCodeService.batchUpdateStatus(couponCods);\n \/\/ \u53d1\u5238\n \/\/ \u53d1\u6743\u76ca\n \/\/ \u65b0\u589e\u7528\u6237\u5238\u7801\u9886\u53d6\u8bb0\u5f55\n}\n   <\/pre>\n<h2>\u6539\u9020\u8fc7\u7a0b<\/h2>\n<p>\u56e0\u4e3a\u5238\u7801\u662f\u591a\u5f20\uff0c\u60f3\u7528lua+redis\u7684list\u7ed3\u6784\u53bb\u505a\u5f39\u51fa\u3002\u4e3a\u4ec0\u4e48\u7528\u8fd9\u79cd\u65b9\u6848\u662f\u56e0\u4e3afor update\u76f4\u63a5\u88ab\u5426\u4e86\u3002<\/p>\n<p>\u8fd9\u662f\u5199\u7684lua\u811a\u672c\u3002\u3002<\/p>\n<pre>\nlocal result = {}\nfor i=1,ARGV[1],1 do\n result[i] = redis.call(\"lpop\", KEYS[1])\nend\nreturn table.contact(result , \"|\")<\/pre>\n<p>\u8fd9\u662f\u5199\u7684\u6267\u884clua\u811a\u672c\u7684client\u3002\u3002\u5176\u5b9e\u4e3b\u8981\u7684\u89e3\u51b3\u65b9\u6cd5\u5c31\u662f\u5728redis\u7684list\u91ccrpush\uff08\u5b58\uff09,lpop\uff08\u53d6\uff09\u53d6\u6570\u636e<\/p>\n<pre>\n@Slf4j\n@Component\npublic class CouponCodeRedisQueueClient implements InitializingBean {\n\n \/**\n  * redis lua\u811a\u672c\u6587\u4ef6\u8def\u5f84\n  *\/\n public static final String POP_COUPON_CODE_LUA_PATH = \"lua\/pop-coupon-code.lua\";\n public static final String SEPARATOR = \"|\";\n\n private static final String COUPON_CODE_KEY_PATTERN = \"PROMOTION:COUPON_CODE_{0}\";\n private String LUA_COUPON_CODE_SCRIPT;\n\n private String LUA_COUPON_CODE_SCRIPT_SHA;\n\n @Autowired\n private JedisTemplate jedisTemplate;\n\n @Override\n public void afterPropertiesSet() throws Exception {\n\n  LUA_COUPON_CODE_SCRIPT = Resources.toString(Resources.getResource(POP_COUPON_CODE_LUA_PATH), Charsets.UTF_8);\n  if (StringUtils.isNotBlank(LUA_COUPON_CODE_SCRIPT)) {\n\n   LUA_COUPON_CODE_SCRIPT_SHA = jedisTemplate.execute(jedis -&gt; {\n    return jedis.scriptLoad(LUA_COUPON_CODE_SCRIPT);\n   });\n   log.info(\"redis lock script sha:{}\", LUA_COUPON_CODE_SCRIPT_SHA);\n  }\n\n }\n\n \/**\n  * \u83b7\u53d6Code\n  *\n  * @param activityId\n  * @param num\n  * @return\n  *\/\n public List\n   \n     popCouponCode(Long activityId, String num , int retryNum) {\n  if(retryNum == 0){\n   log.error(\"reload lua script error , try limit times ,activityId:{}\", activityId);\n   return Collections.emptyList();\n  }\n  List\n    \n      keys = Lists.newArrayList(); String key = buildKey(String.valueOf(activityId)); keys.add(key); List\n     \n       args = Lists.newArrayList(); args.add(num); try { Object result = jedisTemplate.execute(jedis -&gt; { if (StringUtils.isNotBlank(LUA_COUPON_CODE_SCRIPT_SHA)) { return jedis.evalsha(LUA_COUPON_CODE_SCRIPT_SHA, keys, args); } else { return jedis.eval(LUA_COUPON_CODE_SCRIPT, keys, args); } }); log.info(\"pop coupon code by lua script.result:{}\", result); if (Objects.isNull(result)) { return Collections.emptyList(); } return Splitter.on(SEPARATOR).splitToList(result.toString()); } catch (JedisNoScriptException jnse) { log.error(\"no lua lock script found.try to reload it\", jnse); reloadLuaScript(); \/\/\u52a0\u8f7d\u540e\u91cd\u65b0\u6267\u884c popCouponCode(activityId, num, --retryNum); } catch (Exception e) { log.error(\"failed to get a redis lock.key:{}\", key, e); } return Collections.emptyList(); } \/** * \u91cd\u65b0\u52a0\u8f7dLUA\u811a\u672c * * @throws Exception *\/ public void reloadLuaScript() { synchronized (CouponCodeRedisQueueClient.class) { try { afterPropertiesSet(); } catch (Exception e) { log.error(\"failed to reload redis lock lua script.retry load it.\"); reloadLuaScript(); } } } \/** * \u6784\u5efaKey * * @param activityId * @return *\/ public String buildKey(String activityId) { return MessageFormat.format(COUPON_CODE_KEY_PATTERN, activityId); } }\n     \n    \n   <\/pre>\n<p>\u5f53\u7136\u8fd9\u79cd\u64cd\u4f5c\u9700\u8981\u53bb\u63d0\u524d\u628a\u6240\u6709\u5238\u7684\u5238\u7801\u4e22\u5230redis\u91cc\u53bb\uff0c\u8fd9\u91cc\u6211\u4eec\u4e5f\u78b0\u5230\u4e86\u4e00\u4e9b\u95ee\u9898\uff08\u5238\u7801\u91cf\u6bd4\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\uff09\u3002\u6bd4\u5982\u5f00\u59cb\u76f4\u63a5\u7c97\u66b4\u7684\u7528@PostConstruct\u53bb\u653e\u5165redis\uff0c\u5bfc\u81f4\u9879\u76ee\u542f\u52a8\u9700\u8981\u5f88\u4e45\u5f88\u4e45\u3002\u3002\u8fd9\u91cc\u5c31\u4e0d\u5c55\u5f00\u4e86\uff0c\u8bf4\u4e00\u4e0b\u6211\u4eec\u5c1d\u8bd5\u7684\u51e0\u79cd\u65b9\u6cd5<\/p>\n<ul>\n<li>@PostConstruct\u6ce8\u89e3<\/li>\n<li>CommandLineRunner\u63a5\u53e3<\/li>\n<li>redis\u7684pipeline\u6280\u672f<\/li>\n<li>\u5148\u4fdd\u8bc1\u6bcf\u4e2a\u5361\u5238\u6709\u4e00\u5b9a\u91cf\u7684\u5238\u7801\u5728redis\uff0c\u518d\u7528\u5b9a\u65f6\u4efb\u52a1\u5b9a\u65f6\uff08\u6839\u636e\u4e1a\u52a1\u91cf\uff09\u53bb\u8865<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528lua+redis\u89e3\u51b3\u53d1\u591a\u5f20&#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-201477","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201477","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=201477"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201477\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}