{"id":97666,"date":"2024-10-20T20:00:20","date_gmt":"2024-10-20T12:00:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97666\/"},"modified":"2024-10-20T20:00:21","modified_gmt":"2024-10-20T12:00:21","slug":"redisson-%e9%ab%98%e6%80%a7%e8%83%bd-redis-%e5%88%86%e4%bd%88%e5%bc%8f%e9%8e%96%e6%ba%90%e7%a2%bc%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97666\/","title":{"rendered":"Redisson \u9ad8\u6027\u80fd Redis \u5206\u4f48\u5f0f\u9396\u6e90\u78bc\u5206\u6790"},"content":{"rendered":"<h1 id=\"redisson-%e9%ab%98%e6%80%a7%e8%83%bd-redis-%e5%88%86%e4%bd%88%e5%bc%8f%e9%8e%96%e6%ba%90%e7%a2%bc%e5%88%86%e6%9e%90-kipyXyDGmH\">Redisson \u9ad8\u6027\u80fd Redis \u5206\u4f48\u5f0f\u9396\u6e90\u78bc\u5206\u6790<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u5206\u4f48\u5f0f\u7cfb\u7d71\u4e2d\uff0c\u9396\u7684\u7ba1\u7406\u662f\u78ba\u4fdd\u6578\u64da\u4e00\u81f4\u6027\u548c\u9632\u6b62\u7af6\u722d\u689d\u4ef6\u7684\u91cd\u8981\u624b\u6bb5\u3002Redis \u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u7d50\u69cb\u548c\u64cd\u4f5c\uff0c\u4e26\u4e14\u5728\u5206\u4f48\u5f0f\u9396\u7684\u5be6\u73fe\u4e0a\u4e5f\u6709\u8457\u5ee3\u6cdb\u7684\u61c9\u7528\u3002Redisson \u662f\u4e00\u500b\u57fa\u65bc Redis \u7684 Java \u5ba2\u6236\u7aef\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7684\u5206\u4f48\u5f0f\u9396\u529f\u80fd\u3002\u672c\u6587\u5c07\u5c0d Redisson \u7684\u5206\u4f48\u5f0f\u9396\u6e90\u78bc\u9032\u884c\u5206\u6790\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u5176\u5be6\u73fe\u539f\u7406\u3002<\/p>\n<h2 id=\"redisson-%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%9e%b6%e6%a7%8b-kipyXyDGmH\">Redisson \u7684\u57fa\u672c\u67b6\u69cb<\/h2>\n<p>Redisson \u7684\u67b6\u69cb\u8a2d\u8a08\u65e8\u5728\u7c21\u5316 Redis \u7684\u4f7f\u7528\uff0c\u4e26\u63d0\u4f9b\u9ad8\u7d1a\u529f\u80fd\u3002\u5176\u6838\u5fc3\u7d44\u4ef6\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>RedissonClient\uff1a<\/strong>\u4e3b\u8981\u7684\u5ba2\u6236\u7aef\u63a5\u53e3\uff0c\u7528\u65bc\u8207 Redis \u9032\u884c\u4ea4\u4e92\u3002<\/li>\n<li><strong>RLock\uff1a<\/strong>\u5206\u4f48\u5f0f\u9396\u7684\u63a5\u53e3\uff0c\u63d0\u4f9b\u9396\u7684\u7372\u53d6\u3001\u91cb\u653e\u7b49\u64cd\u4f5c\u3002<\/li>\n<li><strong>Codec\uff1a<\/strong>\u7de8\u78bc\u89e3\u78bc\u5668\uff0c\u7528\u65bc\u5c07 Java \u5c0d\u8c61\u8f49\u63db\u70ba Redis \u53ef\u5b58\u5132\u7684\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%88%86%e4%bd%88%e5%bc%8f%e9%8e%96%e7%9a%84%e5%af%a6%e7%8f%be-kipyXyDGmH\">\u5206\u4f48\u5f0f\u9396\u7684\u5be6\u73fe<\/h2>\n<p>Redisson \u7684\u5206\u4f48\u5f0f\u9396\u4e3b\u8981\u4f9d\u8cf4\u65bc Redis \u7684\u539f\u5b50\u64cd\u4f5c\u4f86\u5be6\u73fe\u3002\u4ee5\u4e0b\u662f\u5176\u57fa\u672c\u7684\u9396\u7372\u53d6\u6d41\u7a0b\uff1a<\/p>\n<pre><code>\npublic RLock getLock(String name) {\n    return new RedissonLock(name);\n}\n<\/code><\/pre>\n<p>\u7576\u7528\u6236\u8acb\u6c42\u7372\u53d6\u9396\u6642\uff0cRedisson \u6703\u4f7f\u7528 SETNX \u547d\u4ee4\u4f86\u5617\u8a66\u8a2d\u7f6e\u4e00\u500b\u9375\u3002\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u5247\u8868\u793a\u9396\u53ef\u7528\uff0c\u4e26\u4e14\u6703\u8a2d\u7f6e\u4e00\u500b\u904e\u671f\u6642\u9593\u4ee5\u9632\u6b62\u6b7b\u9396\u3002<\/p>\n<pre><code>\npublic boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) {\n    long currentTime = System.currentTimeMillis();\n    long expireTime = currentTime + unit.toMillis(leaseTime);\n    if (redisTemplate.opsForValue().setIfAbsent(lockName, expireTime)) {\n        return true;\n    }\n    return false;\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c`tryLock` \u65b9\u6cd5\u5617\u8a66\u7372\u53d6\u9396\uff0c\u4e26\u8a2d\u7f6e\u904e\u671f\u6642\u9593\u3002\u9019\u6a23\uff0c\u5373\u4f7f\u7372\u53d6\u9396\u7684\u904e\u7a0b\u4e2d\u51fa\u73fe\u7570\u5e38\uff0c\u9396\u4e5f\u4e0d\u6703\u6c38\u4e45\u4f54\u7528\u3002<\/p>\n<h2 id=\"%e9%8e%96%e7%9a%84%e9%87%8b%e6%94%be-kipyXyDGmH\">\u9396\u7684\u91cb\u653e<\/h2>\n<p>\u91cb\u653e\u9396\u7684\u904e\u7a0b\u540c\u6a23\u91cd\u8981\u3002Redisson \u78ba\u4fdd\u53ea\u6709\u6301\u6709\u9396\u7684\u7dda\u7a0b\u624d\u80fd\u91cb\u653e\u9396\uff0c\u9019\u662f\u901a\u904e\u6bd4\u8f03\u9396\u7684\u6301\u6709\u8005\u4f86\u5be6\u73fe\u7684\uff1a<\/p>\n<pre><code>\npublic void unlock() {\n    if (isHeldByCurrentThread()) {\n        redisTemplate.delete(lockName);\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u6aa2\u67e5\u7576\u524d\u7dda\u7a0b\u662f\u5426\u6301\u6709\u9396\uff0c\u5982\u679c\u662f\uff0c\u5247\u522a\u9664\u9396\u7684\u9375\uff0c\u5f9e\u800c\u91cb\u653e\u9396\u3002<\/p>\n<h2 id=\"redisson-%e7%9a%84%e5%84%aa%e5%8b%a2-kipyXyDGmH\">Redisson \u7684\u512a\u52e2<\/h2>\n<p>Redisson \u63d0\u4f9b\u7684\u5206\u4f48\u5f0f\u9396\u5177\u6709\u4ee5\u4e0b\u5e7e\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>\u57fa\u65bc Redis \u7684\u9ad8\u6548\u6027\uff0c\u80fd\u5920\u5feb\u901f\u7372\u53d6\u548c\u91cb\u653e\u9396\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684 API\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u96c6\u6210\u5230\u61c9\u7528\u4e2d\u3002<\/li>\n<li><strong>\u81ea\u52d5\u904e\u671f\uff1a<\/strong>\u9632\u6b62\u6b7b\u9396\u7684\u767c\u751f\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-kipyXyDGmH\">\u7e3d\u7d50<\/h2>\n<p>Redisson \u7684\u9ad8\u6027\u80fd Redis \u5206\u4f48\u5f0f\u9396\u5be6\u73fe\u4e86\u7c21\u55ae\u800c\u6709\u6548\u7684\u9396\u7ba1\u7406\uff0c\u9069\u5408\u7528\u65bc\u5404\u7a2e\u5206\u4f48\u5f0f\u7cfb\u7d71\u4e2d\u3002\u901a\u904e\u5c0d\u5176\u6e90\u78bc\u7684\u5206\u6790\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230\u5176\u80cc\u5f8c\u7684\u8a2d\u8a08\u7406\u5ff5\u548c\u5be6\u73fe\u7d30\u7bc0\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u6578\u64da\u4e00\u81f4\u6027\u7684\u61c9\u7528\u4f86\u8aaa\uff0cRedisson \u63d0\u4f9b\u4e86\u4e00\u500b\u53ef\u9760\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u5206\u6790 Redisson \u9ad8\u6027\u80fd Redis \u5206\u4f48\u5f0f\u9396\u7684\u6e90\u78bc\uff0c\u63a2\u7d22\u5176\u8a2d\u8a08\u539f\u7406\u8207\u5be6\u73fe\u7d30\u7bc0\uff0c\u63d0\u5347\u5206\u4f48\u5f0f\u7cfb\u7d71\u7684\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-97666","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97666","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=97666"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97666\/revisions"}],"predecessor-version":[{"id":97667,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97666\/revisions\/97667"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}