{"id":201798,"date":"2025-05-10T11:04:36","date_gmt":"2025-05-10T03:04:36","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201798\/"},"modified":"2025-05-10T11:04:36","modified_gmt":"2025-05-10T03:04:36","slug":"redis%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81%e4%b8%80%e5%ae%9a%e8%a6%81%e9%81%bf%e5%bc%80%e7%9a%84%e4%b8%a4%e4%b8%aa%e5%9d%91","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201798\/","title":{"rendered":"Redis\u5206\u5e03\u5f0f\u9501\u4e00\u5b9a\u8981\u907f\u5f00\u7684\u4e24\u4e2a\u5751"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5206\u5e03\u5f0f\u9501\u4e00\u5b9a\u8981\u907f\u5f00\u7684\u4e24\u4e2a\u5751<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300aRedis\u5206\u5e03\u5f0f\u9501\u4e00\u5b9a\u8981\u907f\u5f00\u7684\u4e24\u4e2a\u5751\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ecRedis\u5206\u5e03\u5f0f\u9501\uff0c\u5982\u679c\u4f60\u5bf9\u6570\u636e\u5e93\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7golang\u5b66\u4e60\u7f51\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<h2>1 \u7b2c\u4e00\u4e2a\u5751\uff1a\u9519\u8bef\u91ca\u653e\u9501\u65f6\u673a<\/h2>\n<h3>1.1. \u53d1\u73b0\u95ee\u9898<\/h3>\n<p>\u5206\u6790\u4ee5\u4e0b\u4ee3\u7801\u5b58\u5728\u4ec0\u4e48\u95ee\u9898\uff1a<\/p>\n<pre>\/\/ \u5206\u5e03\u5f0f\u9501\u670d\u52a1\npublic interface RedisLockService {\n    \/\/ \u83b7\u53d6\u9501\n    public boolean getLock(String key);\n    \/\/ \u91ca\u653e\u9501\n    public boolean releaseLock(String key);\n}\n\n\/\/ \u4e1a\u52a1\u670d\u52a1\npublic class BizService {\n\n    @Resource\n    private RedisLockService redisLockService;\n\n    public void bizMethod(String bizId) {\n        try {\n            \/\/ \u83b7\u53d6\u9501\n            if(redisLockService.getLock(bizId)) {\n                \/\/ \u4e1a\u52a1\u91cd\u590d\u6821\u9a8c\n                if(!bizValidate(bizId)) {\n                    throw new BizException(ErrorBizCode.REPEATED);\n                }\n                \/\/ \u6267\u884c\u4e1a\u52a1\n                return doBusiness();\n            }\n            \/\/ \u83b7\u53d6\u9501\u5931\u8d25\n            throw new BizException(ErrorBizCode.GET_LOCK_ERROR);\n        } finally {\n            \/\/ \u91ca\u653e\u9501\n            redisLockService.releaseLock(bizId);\n        }\n    }\n}<\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u770b\u4f3c\u6ca1\u95ee\u9898\uff0c\u5b9e\u5219\u9690\u85cf\u5927\u95ee\u9898\u3002\u95ee\u9898\u5728\u4e8e\u91ca\u653e\u9501\u65f6\u6ca1\u6709\u6821\u9a8c\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u62ff\u5230\u9501\uff1a<\/p>\n<ul>\n<li>\u7ebf\u7a0b1\u548c\u7ebf\u7a0b2\u540c\u4e00\u65f6\u523b\u8bbf\u95ee\u4e1a\u52a1\u65b9\u6cd5<\/li>\n<li>\u7ebf\u7a0b2\u83b7\u53d6\u9501\u6210\u529f\uff0c\u8fdb\u884c\u4e1a\u52a1\u5904\u7406<\/li>\n<li>\u7ebf\u7a0b1\u6ca1\u6709\u83b7\u53d6\u5230\u9501\uff0c\u4f46\u662f\u91ca\u653e\u9501\u6210\u529f<\/li>\n<li>\u6b64\u65f6\u6709\u7ebf\u7a0b3\u5c1d\u8bd5\u83b7\u53d6\u9501\u6210\u529f\uff0c\u4f46\u662f\u7ebf\u7a0b2\u4e1a\u52a1\u6ca1\u6709\u5904\u7406\u5b8c\uff0c\u6240\u4ee5\u7ebf\u7a0b3\u4e0d\u4f1a\u5bfc\u81f4\u4e1a\u52a1\u91cd\u590d\u5f02\u5e38<\/li>\n<li>\u6700\u7ec8\u5bfc\u81f4\u7ebf\u7a0b2\u548c\u7ebf\u7a0b3\u91cd\u590d\u6267\u884c\u4e1a\u52a1<\/li>\n<\/ul>\n<h3>1.2 \u89e3\u51b3\u95ee\u9898<\/h3>\n<p>\u89e3\u51b3\u65b9\u6848\u662f\u5728\u786e\u8ba4\u83b7\u53d6\u9501\u6210\u529f\u540e\u624d\u5141\u8bb8\u91ca\u653e\u9501\uff1a<\/p>\n<pre>public class BizService {\n\n    @Resource\n    private RedisLockService redisLockService;\n\n    public void bizMethod(String bizId) {\n        boolean getLockSuccess = false;\n        try {\n            \/\/ \u5c1d\u8bd5\u83b7\u53d6\u9501\n            getLockSuccess = redisLockService.getLock(bizId);\n            \/\/ \u83b7\u53d6\u9501\u6210\u529f\n            if(getLockSuccess) {\n                \/\/ \u4e1a\u52a1\u91cd\u590d\u6821\u9a8c\n                if(!bizValidate(bizId)) {\n                    throw new BizException(ErrorBizCode.REPEATED);\n                }\n                \/\/ \u6267\u884c\u4e1a\u52a1\n                return doBusiness();\n            }\n            \/\/ \u83b7\u53d6\u9501\u5931\u8d25\n            throw new BizException(ErrorBizCode.GET_LOCK_ERROR);\n        } finally {\n            \/\/ \u83b7\u53d6\u9501\u6210\u529f\u624d\u5141\u8bb8\u91ca\u653e\u9501\n            if(getLockSuccess) {\n                redisLockService.releaseLock(bizId);\n            }\n        }\n    }\n}<\/pre>\n<h2>2 \u7b2c\u4e8c\u4e2a\u5751\uff1a\u7f13\u5b58\u5931\u6548\u95ee\u9898<\/h2>\n<p>\u7b2c\u4e8c\u4e2a\u95ee\u9898\u662fRedis\u8fd8\u5b58\u5728\u5185\u5b58\u6e05\u7406\u673a\u5236\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5206\u5e03\u5f0f\u9501\u5931\u6548\u3002<\/p>\n<h3>2.1 \u8fc7\u671f\u6e05\u7406\u673a\u5236<\/h3>\n<p><strong>(1) \u5b9a\u671f\u5220\u9664<\/strong><\/p>\n<p>Redis\u5b9a\u65f6\u68c0\u67e5\u54ea\u4e9bkey\u5df2\u7ecf\u8fc7\u671f\uff0c\u53d1\u73b0\u8fc7\u671f\u5219\u5220\u9664<\/p>\n<p><strong>(2) \u60f0\u6027\u5220\u9664<\/strong><\/p>\n<p>\u5982\u679ckey\u975e\u5e38\u591a\uff0c\u5b9a\u671f\u5220\u9664\u4f1a\u975e\u5e38\u6d88\u8017\u8d44\u6e90\uff0c\u6240\u4ee5\u5f15\u5165\u60f0\u6027\u5220\u9664\u7b56\u7565<\/p>\n<p>\u5982\u679cRedis\u8bbf\u95eekey\u65f6\u53d1\u73b0\u5df2\u7ecf\u8fc7\u671f\u5219\u76f4\u63a5\u5220\u9664<\/p>\n<h3>2.2 \u5185\u5b58\u56de\u6536\u673a\u5236<\/h3>\n<p>\u5f53\u5185\u5b58\u4e0d\u8db3\u65f6Redis\u4f1a\u9009\u62e9\u4e00\u4e9b\u5143\u7d20\u8fdb\u884c\u5220\u9664\uff1a<\/p>\n<p>no-enviction<\/p>\n<p>\u7981\u6b62\u9a71\u9010\u6570\u636e\uff0c\u65b0\u5199\u5165\u64cd\u4f5c\u4f1a\u62a5\u9519<\/p>\n<p>volatile-lru<\/p>\n<p>\u4ece\u5df2\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u96c6\u9009\u62e9\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\u6dd8\u6c70<\/p>\n<p>volatile-ttl<\/p>\n<p>\u4ece\u5df2\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u96c6\u9009\u62e9\u5c06\u8981\u8fc7\u671f\u7684\u6570\u636e\u6dd8\u6c70<\/p>\n<p>volatile-random<\/p>\n<p>\u4ece\u5df2\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u96c6\u9009\u62e9\u4efb\u610f\u7684\u6570\u636e\u6dd8\u6c70<\/p>\n<p>allkeys-lru<\/p>\n<p>\u4ece\u6570\u636e\u96c6\u9009\u62e9\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\u6dd8\u6c70<\/p>\n<p>allkeys-random<\/p>\n<p>\u4ece\u6570\u636e\u96c6\u9009\u62e9\u4efb\u610f\u7684\u6570\u636e\u6dd8\u6c70<\/p>\n<p>\u81f3\u5c11\u5b58\u5728\u4e24\u79cd\u573a\u666f\u5bfc\u81f4\u5206\u5e03\u5f0f\u9501\u5931\u6548\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u573a\u666f\u4e00\uff1aRedis\u5185\u5b58\u4e0d\u8db3\u8fdb\u884c\u5185\u5b58\u56de\u6536\uff0c\u4f7f\u7528<code>allkeys-lru<\/code>\u6216\u8005<code>allkeys-random<\/code>\u56de\u6536\u7b56\u7565\u5bfc\u81f4\u9501\u5931\u6548<\/li>\n<li>\u573a\u666f\u4e8c\uff1a\u7ebf\u7a0b\u83b7\u53d6\u5206\u5e03\u5f0f\u9501\u6210\u529f\uff0c\u4f46\u5904\u7406\u4e1a\u52a1\u65f6\u95f4\u8fc7\u957f\uff0c\u6b64\u65f6\u9501\u5230\u671f\u88ab\u5b9a\u65f6\u6e05\u7406\uff0c\u5bfc\u81f4\u5176\u5b83\u7ebf\u7a0b\u83b7\u53d6\u9501\u6210\u529f\u5e76\u91cd\u590d\u6267\u884c\u4e1a\u52a1<\/li>\n<\/ul>\n<h3>2.3 \u4e50\u89c2\u9501<\/h3>\n<p>\u901a\u7528\u65b9\u6848\u662f\u5728\u6570\u636e\u5e93\u5c42\u4fdd\u62a4\uff0c\u4f8b\u5982\u5e93\u5b58\u6263\u51cf\u4e1a\u52a1\u5728\u6570\u636e\u5e93\u5c42\u7528\u4e50\u89c2\u9501\uff0c\u539f\u7406\u53c2\u770b\u300aMySQL\u4e50\u89c2\u9501\u6263\u51cf\u5e93\u5b58\u539f\u7406\u56fe\u89e3\u300b\u8fd9\u7bc7\u6587\u7ae0\u3002<\/p>\n<pre>udpate goods set stock = stock - #{acquire} \nwhere sku_id = #{skuId} and stock - #{acquire} &gt;= 0<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5206\u5e03\u5f0f\u9501\u4e00\u5b9a\u8981\u907f\u5f00\u7684\u4e24&#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-201798","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201798","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=201798"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201798\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}