{"id":201162,"date":"2025-05-10T08:00:35","date_gmt":"2025-05-10T00:00:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201162\/"},"modified":"2025-05-10T08:00:35","modified_gmt":"2025-05-10T00:00:35","slug":"redislua%e8%84%9a%e6%9c%ac%e5%ae%9e%e7%8e%b0%e8%ae%a1%e6%95%b0%e5%99%a8%e6%8e%a5%e5%8f%a3%e9%98%b2%e5%88%b7%e5%8a%9f%e8%83%bd%e5%8d%87%e7%ba%a7%e7%89%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201162\/","title":{"rendered":"Redis+Lua\u811a\u672c\u5b9e\u73b0\u8ba1\u6570\u5668\u63a5\u53e3\u9632\u5237\u529f\u80fd(\u5347\u7ea7\u7248)"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis+Lua\u811a\u672c\u5b9e\u73b0\u8ba1\u6570\u5668\u63a5\u53e3\u9632\u5237\u529f\u80fd(\u5347\u7ea7\u7248)<\/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+Lua\u811a\u672c\u5b9e\u73b0\u8ba1\u6570\u5668\u63a5\u53e3\u9632\u5237\u529f\u80fd(\u5347\u7ea7\u7248)\u300b\uff0c\u804a\u804aRedis\u8ba1\u6570\u5668\u3001\u63a5\u53e3\u9632\u5237\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<h2>\u3010\u524d\u8a00\u3011<\/h2>\n<p>Cash Loan\uff08\u4e00\uff09:Redis\u5b9e\u73b0\u8ba1\u6570\u5668\u9632\u5237 \u4e2d\u4ecb\u7ecd\u4e86\u9879\u76ee\u4e2d\u5e94\u7528redis\u6765\u505a\u8ba1\u6570\u5668\u7684\u5b9e\u73b0\u8fc7\u7a0b\uff0c\u6700\u8fd1\u81ea\u5df1\u770b\u4e86\u4e9b\u5173\u4e8eRedis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u4ee3\u7801\u540e\uff0c\u53d1\u73b0\u5728Redis\u5206\u5e03\u5f0f\u9501\u4e2d\u51fa\u73b0\u4e00\u4e2a\u95ee\u9898\u5728\u8fd9\u7248\u8ba1\u6570\u5668\u4e2d\u540c\u6837\u4f1a\u51fa\u73b0\uff0c\u4e8e\u662f\u878d\u5165\u4e86Lua\u811a\u672c\u8fdb\u884c\u5347\u7ea7\u6539\u9020\u6709\u4e86Redis+Lua\u7248\u672c\u3002<\/p>\n<h2>\u3010\u5b9e\u73b0\u8fc7\u7a0b\u3011<\/h2>\n<h3>\u4e00\u3001\u95ee\u9898\u5206\u6790<\/h3>\n<p>&nbsp;\u5982\u679cset\u547d\u4ee4\u8bbe\u7f6e\u4e0a\uff0c\u4f46\u662f\u5728\u8bbe\u7f6e\u5931\u6548\u65f6\u95f4\u65f6\u7531\u4e8e\u7f51\u7edc\u6296\u52a8\u7b49\u539f\u56e0\u5bfc\u81f4\u6ca1\u6709\u8bbe\u7f6e\u6210\u529f\uff0c\u8fd9\u65f6\u5c31\u4f1a\u51fa\u73b0\u6b7b\u8ba1\u6570\u5668\uff08\u7c7b\u4f3c\u6b7b\u9501\uff09\uff1b<\/p>\n<h3>\u4e8c\u3001\u89e3\u51b3\u65b9\u6848<\/h3>\n<p>&nbsp;Redis+Lua\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f7f\u7528\u811a\u672c\u4f7f\u5f97set\u547d\u4ee4\u548cexpire\u547d\u4ee4\u4e00\u540c\u8fbe\u5230Redis\u88ab\u6267\u884c\u4e14\u4e0d\u4f1a\u88ab\u5e72\u6270\uff0c\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u4fdd\u8bc1\u4e86\u539f\u5b50\u64cd\u4f5c\uff1b<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8bf4\u662f\u5f88\u5927\u7a0b\u5ea6\u4e0a\u4fdd\u8bc1\u539f\u5b50\u64cd\u4f5c\u800c\u4e0d\u662f\u5b8c\u5168\u4fdd\u8bc1\uff1f\u56e0\u4e3a\u5728Redis\u5185\u90e8\u6267\u884c\u7684\u65f6\u5019\u51fa\u95ee\u9898\u4e5f\u6709\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\u4e0d\u8fc7\u6982\u7387\u975e\u5e38\u5c0f\uff1b\u5373\u4f7f\u9488\u5bf9\u5c0f\u6982\u7387\u4e8b\u4ef6\u4e5f\u6709\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u6bd4\u5982\u89e3\u51b3\u6b7b\u9501\u4e00\u4e2a\u601d\u8def\u503c\u5f97\u53c2\u8003\uff1a\u9632\u6b62\u6b7b\u9501\u4f1a\u5c06\u9501\u7684\u503c\u5b58\u6210\u4e00\u4e2a\u65f6\u95f4\u6233\uff0c\u5373\u4f7f\u53d1\u751f\u6ca1\u6709\u5c06\u5931\u6548\u65f6\u95f4\u8bbe\u7f6e\u4e0a\u5728\u5224\u65ad\u662f\u5426\u4e0a\u9501\u65f6\u53ef\u4ee5\u52a0\u4e0a\u770b\u770b\u5176\u4e2d\u503c\u8ddd\u73b0\u5728\u662f\u5426\u8d85\u8fc7\u4e00\u4e2a\u8bbe\u5b9a\u7684\u65f6\u95f4\uff0c\u5982\u679c\u8d85\u8fc7\u5219\u5c06\u5176\u5220\u9664\u91cd\u65b0\u8bbe\u7f6e\u9501\u3002&nbsp; &nbsp; &nbsp; &nbsp;<\/p>\n<h3>\u4e09\u3001\u4ee3\u7801\u6539\u9020<\/h3>\n<p>1\u3001Redis+Lua\u9501\u7684\u5b9e\u73b0<\/p>\n<pre>package han.zhang.utils;\n \nimport org.springframework.data.redis.core.StringRedisTemplate;\nimport org.springframework.data.redis.core.script.DigestUtils;\nimport org.springframework.data.redis.core.script.RedisScript;\nimport java.util.Collections;\nimport java.util.UUID;\npublic class RedisLock {\n    private static final LogUtils logger = LogUtils.getLogger(RedisLock.class);\n    private final StringRedisTemplate stringRedisTemplate;\n    private final String lockKey;\n    private final String lockValue;\n    private boolean locked = false;\n    \/**\n     * \u4f7f\u7528\u811a\u672c\u5728redis\u670d\u52a1\u5668\u6267\u884c\u8fd9\u4e2a\u903b\u8f91\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u4fdd\u8bc1\u6b64\u64cd\u4f5c\u7684\u539f\u5b50\u6027\n     * \uff08\u5373\u4e0d\u4f1a\u53d1\u751f\u5ba2\u6237\u7aef\u5728\u6267\u884csetNX\u548cexpire\u547d\u4ee4\u4e4b\u95f4\uff0c\u53d1\u751f\u5d29\u6e83\u6216\u5931\u53bb\u4e0e\u670d\u52a1\u5668\u7684\u8fde\u63a5\u5bfc\u81f4expire\u6ca1\u6709\u5f97\u5230\u6267\u884c\uff0c\u53d1\u751f\u6c38\u4e45\u6b7b\u9501\uff09\n     * <p>\n     * \u9664\u975e\u811a\u672c\u5728redis\u670d\u52a1\u5668\u6267\u884c\u65f6redis\u670d\u52a1\u5668\u53d1\u751f\u5d29\u6e83\uff0c\u4e0d\u8fc7\u6b64\u79cd\u60c5\u51b5\u9501\u4e5f\u4f1a\u5931\u6548\n     *\/\n    private static final RedisScript\n    \n      SETNX_AND_EXPIRE_SCRIPT; static { StringBuilder sb = new StringBuilder(); sb.append(\"if (redis.call('setnx', KEYS[1], ARGV[1]) == 1) then\\n\"); sb.append(\"\\tredis.call('expire', KEYS[1], tonumber(ARGV[2]))\\n\"); sb.append(\"\\treturn true\\n\"); sb.append(\"else\\n\"); sb.append(\"\\treturn false\\n\"); sb.append(\"end\"); SETNX_AND_EXPIRE_SCRIPT = new RedisScriptImpl(sb.toString(), Boolean.class); } private static final RedisScript\n     \n       DEL_IF_GET_EQUALS; sb.append(\"if (redis.call('get', KEYS[1]) == ARGV[1]) then\\n\"); sb.append(\"\\tredis.call('del', KEYS[1])\\n\"); DEL_IF_GET_EQUALS = new RedisScriptImpl(sb.toString(), Boolean.class); public RedisLock(StringRedisTemplate stringRedisTemplate, String lockKey) { this.stringRedisTemplate = stringRedisTemplate; this.lockKey = lockKey; this.lockValue = UUID.randomUUID().toString() + \".\" + System.currentTimeMillis(); private boolean doTryLock(int lockSeconds) { if (locked) { throw new IllegalStateException(\"already locked!\"); } locked = stringRedisTemplate.execute(SETNX_AND_EXPIRE_SCRIPT, Collections.singletonList(lockKey), lockValue, String.valueOf(lockSeconds)); return locked; * \u5c1d\u8bd5\u83b7\u5f97\u9501\uff0c\u6210\u529f\u8fd4\u56detrue\uff0c\u5982\u679c\u5931\u8d25\u7acb\u5373\u8fd4\u56defalse * * @param lockSeconds \u52a0\u9501\u7684\u65f6\u95f4(\u79d2)\uff0c\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u540e\u9501\u4f1a\u81ea\u52a8\u91ca\u653e public boolean tryLock(int lockSeconds) { try { return doTryLock(lockSeconds); } catch (Exception e) { logger.error(\"tryLock Error\", e); return false; * \u8f6e\u8be2\u7684\u65b9\u5f0f\u53bb\u83b7\u5f97\u9501\uff0c\u6210\u529f\u8fd4\u56detrue\uff0c\u8d85\u8fc7\u8f6e\u8be2\u6b21\u6570\u6216\u5f02\u5e38\u8fd4\u56defalse * @param lockSeconds \u52a0\u9501\u7684\u65f6\u95f4(\u79d2)\uff0c\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u540e\u9501\u4f1a\u81ea\u52a8\u91ca\u653e * @param tryIntervalMillis \u8f6e\u8be2\u7684\u65f6\u95f4\u95f4\u9694(\u6beb\u79d2) * @param maxTryCount \u6700\u5927\u7684\u8f6e\u8be2\u6b21\u6570 public boolean tryLock(final int lockSeconds, final long tryIntervalMillis, final int maxTryCount) { int tryCount = 0; while (true) { if (++tryCount &gt;= maxTryCount) { \/\/ \u83b7\u53d6\u9501\u8d85\u65f6 return false; } try { if (doTryLock(lockSeconds)) { return true; } } catch (Exception e) { logger.error(\"tryLock Error\", e); Thread.sleep(tryIntervalMillis); } catch (InterruptedException e) { logger.error(\"tryLock interrupted\", e); * \u89e3\u9501\u64cd\u4f5c public void unlock() { if (!locked) { throw new IllegalStateException(\"not locked yet!\"); locked = false; \/\/ \u5ffd\u7565\u7ed3\u679c stringRedisTemplate.execute(DEL_IF_GET_EQUALS, Collections.singletonList(lockKey), lockValue); private static class RedisScriptImpl\n      \n        implements RedisScript\n       \n         { private final String script; private final String sha1; private final Class\n        \n          resultType; public RedisScriptImpl(String script, Class\n         \n           resultType) { this.script = script; this.sha1 = DigestUtils.sha1DigestAsHex(script); this.resultType = resultType; @Override public String getSha1() { return sha1; public Class\n          \n            getResultType() { return resultType; public String getScriptAsString() { return script; }\n          \n         \n        \n       \n      \n     \n    <\/p><\/pre>\n<p>2\u3001\u501f\u9274\u9501\u5b9e\u73b0Redis+Lua\u8ba1\u6570\u5668<\/p>\n<p>\uff081\uff09\u5de5\u5177\u7c7b&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n<pre>package han.zhang.utils;\n \nimport org.springframework.data.redis.core.StringRedisTemplate;\nimport org.springframework.data.redis.core.script.DigestUtils;\nimport org.springframework.data.redis.core.script.RedisScript;\nimport java.util.Collections;\npublic class CountUtil {\n    private static final LogUtils logger = LogUtils.getLogger(CountUtil.class);\n    private final StringRedisTemplate stringRedisTemplate;\n    \/**\n     * \u4f7f\u7528\u811a\u672c\u5728redis\u670d\u52a1\u5668\u6267\u884c\u8fd9\u4e2a\u903b\u8f91\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u4fdd\u8bc1\u6b64\u64cd\u4f5c\u7684\u539f\u5b50\u6027\n     * \uff08\u5373\u4e0d\u4f1a\u53d1\u751f\u5ba2\u6237\u7aef\u5728\u6267\u884csetNX\u548cexpire\u547d\u4ee4\u4e4b\u95f4\uff0c\u53d1\u751f\u5d29\u6e83\u6216\u5931\u53bb\u4e0e\u670d\u52a1\u5668\u7684\u8fde\u63a5\u5bfc\u81f4expire\u6ca1\u6709\u5f97\u5230\u6267\u884c\uff0c\u53d1\u751f\u6c38\u4e45\u6b7b\u8ba1\u6570\u5668\uff09\n     * <p>\n     * \u9664\u975e\u811a\u672c\u5728redis\u670d\u52a1\u5668\u6267\u884c\u65f6redis\u670d\u52a1\u5668\u53d1\u751f\u5d29\u6e83\uff0c\u4e0d\u8fc7\u6b64\u79cd\u60c5\u51b5\u8ba1\u6570\u5668\u4e5f\u4f1a\u5931\u6548\n     *\/\n    private static final RedisScript\n    \n      SET_AND_EXPIRE_SCRIPT; static { StringBuilder sb = new StringBuilder(); sb.append(\"local visitTimes = redis.call('incr', KEYS[1])\\n\"); sb.append(\"if (visitTimes == 1) then\\n\"); sb.append(\"\\tredis.call('expire', KEYS[1], tonumber(ARGV[1]))\\n\"); sb.append(\"\\treturn false\\n\"); sb.append(\"elseif(visitTimes &gt; tonumber(ARGV[2])) then\\n\"); sb.append(\"\\treturn true\\n\"); sb.append(\"else\\n\"); sb.append(\"end\"); SET_AND_EXPIRE_SCRIPT = new RedisScriptImpl(sb.toString(), Boolean.class); } public CountUtil(StringRedisTemplate stringRedisTemplate) { this.stringRedisTemplate = stringRedisTemplate; public boolean isOverMaxVisitTimes(String key, int seconds, int maxTimes) throws Exception { try { return stringRedisTemplate.execute(SET_AND_EXPIRE_SCRIPT, Collections.singletonList(key), String.valueOf(seconds), String.valueOf(maxTimes)); } catch (Exception e) { logger.error(\"RedisBusiness&gt;&gt;&gt;isOverMaxVisitTimes; get visit times Exception; key:\" + key + \"result:\" + e.getMessage()); throw new Exception(\"already Over MaxVisitTimes\"); } private static class RedisScriptImpl\n     \n       implements RedisScript\n      \n        { private final String script; private final String sha1; private final Class\n       \n         resultType; public RedisScriptImpl(String script, Class\n        \n          resultType) { this.script = script; this.sha1 = DigestUtils.sha1DigestAsHex(script); this.resultType = resultType; @Override public String getSha1() { return sha1; public Class\n         \n           getResultType() { return resultType; public String getScriptAsString() { return script; }\n         \n        \n       \n      \n     \n    <\/p><\/pre>\n<p>\uff082\uff09\u8c03\u7528\u6d4b\u8bd5\u4ee3\u7801<\/p>\n<pre> public void run(String... strings) {\n        CountUtil countUtil = new CountUtil(SpringUtils.getStringRedisTemplate());\n        try {\n            for (int i = 0; i \n<p>\uff083\uff09\u6d4b\u8bd5\u7ed3\u679c<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167240342063aed9dc1092f.png\" class=\"aligncenter\"><\/p>\n<h2>\u3010\u603b\u7ed3\u3011<\/h2>\n<p>&nbsp; &nbsp; &nbsp; &nbsp;1\u3001\u7528\u5fc3\u53bb\u4e0d\u65ad\u7684\u6539\u9020\u81ea\u5df1\u7684\u7a0b\u5e8f\uff1b<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp;2\u3001\u7528\u4ee3\u7801\u6539\u53d8\u4e16\u754c\u3002<\/p>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis+Lua\u811a\u672c\u5b9e\u73b0\u8ba1\u6570\u5668&#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-201162","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201162","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=201162"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201162\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}