{"id":44927,"date":"2024-12-01T12:07:24","date_gmt":"2024-12-01T04:07:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/44927\/"},"modified":"2024-12-01T12:07:24","modified_gmt":"2024-12-01T04:07:24","slug":"redis%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/44927\/","title":{"rendered":"Redis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f<\/h1>\n<p><span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<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\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\u300b\uff0c\u804a\u804a\u5206\u5e03\u5f0f\u3001\u9501\u3001JavaRedis\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<h2>\u4e00\u3001\u5206\u5e03\u5f0f\u9501\u662f\u4ec0\u4e48<\/h2>\n<p>\u5206\u5e03\u5f0f\u9501\u662f \u6ee1\u8db3\u5206\u5e03\u5f0f\u7cfb\u7edf\u6216\u96c6\u7fa4\u6a21\u5f0f\u4e0b\u591a\u8fdb\u7a0b\u53ef\u89c1\u5e76\u4e14\u4e92\u65a5\u7684\u9501\u3002<\/p>\n<p>\u57fa\u4e8eRedis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\uff1a<\/p>\n<h3>1\u3001\u83b7\u53d6\u9501<\/h3>\n<ul>\n<li>\u4e92\u65a5\uff1a\u786e\u4fdd\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u9501;<\/li>\n<li>\u975e\u963b\u585e\uff1a\u5c1d\u8bd5\u83b7\u53d6\u9501\uff0c\u6210\u529f\u8fd4\u56detrue\uff0c\u5931\u8d25\u8fd4\u56defalse\uff1b<\/li>\n<\/ul>\n<p>\u6dfb\u52a0\u9501\u8fc7\u671f\u65f6\u95f4\uff0c\u907f\u514d\u670d\u52a1\u5b95\u673a\u5f15\u8d77\u6b7b\u9501\u3002<\/p>\n<p><code>SET lock thread1 NX EX 10<\/code><\/p>\n<h3>2\u3001\u91ca\u653e\u9501<\/h3>\n<ul>\n<li>\u624b\u52a8\u91ca\u653e\uff1b<code>DEL key1<\/code><\/li>\n<li>\u8d85\u65f6\u91ca\u653e\uff0c\u83b7\u53d6\u9501\u65f6\u6dfb\u52a0\u4e00\u4e2a\u8d85\u65f6\u9501\uff1b<\/li>\n<\/ul>\n<h2>\u4e8c\u3001\u4ee3\u7801\u5b9e\u4f8b<\/h2>\n<pre>package com.guor.utils;\n\nimport org.springframework.data.redis.core.StringRedisTemplate;\n\nimport java.util.concurrent.TimeUnit;\n\npublic class RedisLock implements ILock{\n\n    private String name;\n    private StringRedisTemplate stringRedisTemplate;\n\n    public RedisLock(String name, StringRedisTemplate stringRedisTemplate) {\n        this.name = name;\n        this.stringRedisTemplate = stringRedisTemplate;\n    }\n\n    private static final String KEY_PREFIX = \"lock:\";\n\n    @Override\n    public boolean tryLock(long timeout) {\n        \/\/ \u83b7\u53d6\u7ebf\u7a0b\u552f\u4e00\u6807\u8bc6\n        long threadId = Thread.currentThread().getId();\n        \/\/ \u83b7\u53d6\u9501\n        Boolean success = stringRedisTemplate.opsForValue()\n                .setIfAbsent(KEY_PREFIX + name, threadId+\"\", timeout, TimeUnit.SECONDS);\n        \/\/ \u9632\u6b62\u62c6\u7bb1\u7684\u7a7a\u6307\u9488\u5f02\u5e38\n        return Boolean.TRUE.equals(success);\n    }\n\n    @Override\n    public void unlock() {\n        stringRedisTemplate.delete(KEY_PREFIX + name);\n    }\n}\n<\/pre>\n<h3>\u4e0a\u9762\u4ee3\u7801\u5b58\u5728\u9501\u8bef\u5220\u95ee\u9898\uff1a<\/h3>\n<ol>\n<li>\u5982\u679c\u7ebf\u7a0b1\u83b7\u53d6\u9501\uff0c\u4f46\u7ebf\u7a0b1\u53d1\u751f\u4e86\u963b\u585e\uff0c\u5bfc\u81f4Redis\u8d85\u65f6\u91ca\u653e\u9501\uff1b<\/li>\n<li>\u6b64\u65f6\uff0c\u7ebf\u7a0b2\u5c1d\u8bd5\u83b7\u53d6\u9501\uff0c\u6210\u529f\uff0c\u5e76\u6267\u884c\u4e1a\u52a1\uff1b<\/li>\n<li>\u6b64\u65f6\uff0c\u7ebf\u7a0b1\u91cd\u65b0\u5f00\u59cb\u6267\u884c\u4efb\u52a1\uff0c\u5e76\u6267\u884c\u5b8c\u6bd5\uff0c\u6267\u884c\u91ca\u653e\u9501\uff08\u5373\u5220\u9664\u9501\uff09\uff1b<\/li>\n<li>\u4f46\u662f\uff0c\u7ebf\u7a0b1\u5220\u9664\u7684\u9501\uff0c\u548c\u7ebf\u7a0b2\u7684\u9501\u662f\u540c\u4e00\u628a\u9501\uff0c\u8fd9\u5c31\u662f<code>\u5206\u5e03\u5f0f\u9501\u8bef\u5220\u95ee\u9898<\/code>\uff1b<\/li>\n<\/ol>\n<p>\u5728\u91ca\u653e\u9501\u65f6\uff0c\u91ca\u653e\u7ebf\u7a0b\u81ea\u5df1\u7684\u5206\u5e03\u5f0f\u9501\uff0c\u5c31\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<pre>package com.guor.utils;\n\nimport cn.hutool.core.lang.UUID;\nimport org.springframework.data.redis.core.StringRedisTemplate;\n\nimport java.util.concurrent.TimeUnit;\n\npublic class RedisLock implements ILock{\n\n    private String name;\n    private StringRedisTemplate stringRedisTemplate;\n\n    public RedisLock(String name, StringRedisTemplate stringRedisTemplate) {\n        this.name = name;\n        this.stringRedisTemplate = stringRedisTemplate;\n    }\n\n    private static final String KEY_PREFIX = \"lock:\";\n    private static final String UUID_PREFIX = UUID.randomUUID().toString(true) + \"-\";\n\n    @Override\n    public boolean tryLock(long timeout) {\n        \/\/ \u83b7\u53d6\u7ebf\u7a0b\u552f\u4e00\u6807\u8bc6\n        String threadId = UUID_PREFIX + Thread.currentThread().getId();\n        \/\/ \u83b7\u53d6\u9501\n        Boolean success = stringRedisTemplate.opsForValue()\n                .setIfAbsent(KEY_PREFIX + name, threadId, timeout, TimeUnit.SECONDS);\n        \/\/ \u9632\u6b62\u62c6\u7bb1\u7684\u7a7a\u6307\u9488\u5f02\u5e38\n        return Boolean.TRUE.equals(success);\n    }\n\n    @Override\n    public void unlock() {\n        \/\/ \u83b7\u53d6\u7ebf\u7a0b\u552f\u4e00\u6807\u8bc6\n        String threadId = UUID_PREFIX + Thread.currentThread().getId();\n        \/\/ \u83b7\u53d6\u9501\u4e2d\u7684\u6807\u8bc6\n        String id = stringRedisTemplate.opsForValue().get(KEY_PREFIX + name);\n        \/\/ \u5224\u65ad\u6807\u793a\u662f\u5426\u4e00\u81f4\n        if(threadId.equals(id)) {\n            \/\/ \u91ca\u653e\u9501\n            stringRedisTemplate.delete(KEY_PREFIX + name);\n        }\n    }\n}\n<\/pre>\n<h2>\u4e09\u3001\u57fa\u4e8e<code>SETNX<\/code>\u5b9e\u73b0\u7684\u5206\u5e03\u5f0f\u9501\u5b58\u5728\u4e0b\u9762\u51e0\u4e2a\u95ee\u9898<\/h2>\n<h3>1\u3001\u4e0d\u53ef\u91cd\u5165<\/h3>\n<p>\u540c\u4e00\u4e2a\u7ebf\u7a0b\u65e0\u6cd5\u591a\u6b21\u83b7\u53d6\u540c\u4e00\u628a\u9501\u3002<\/p>\n<h3>2\u3001\u4e0d\u53ef\u91cd\u8bd5<\/h3>\n<p>\u83b7\u53d6\u9501\u53ea\u5c1d\u8bd5\u4e00\u6b21\u5c31\u8fd4\u56defalse\uff0c\u6ca1\u6709\u91cd\u8bd5\u673a\u5236\u3002<\/p>\n<h3>3\u3001\u8d85\u65f6\u91ca\u653e<\/h3>\n<p>\u9501\u7684\u8d85\u65f6\u91ca\u653e\u867d\u7136\u53ef\u4ee5\u907f\u514d\u6b7b\u9501\uff0c\u4f46\u5982\u679c\u4e1a\u52a1\u6267\u884c\u8017\u65f6\u8f83\u957f\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u9501\u91ca\u653e\uff0c\u5b58\u5728\u5b89\u5168\u9690\u60a3\u3002<\/p>\n<h3>4\u3001\u4e3b\u4ece\u4e00\u81f4\u6027<\/h3>\n<p>\u5982\u679cRedis\u662f\u96c6\u7fa4\u90e8\u7f72\u7684\uff0c\u4e3b\u4ece\u540c\u6b65\u5b58\u5728\u5ef6\u8fdf\uff0c\u5f53\u4e3b\u673a\u5b95\u673a\u65f6\uff0c\u6b64\u65f6\u4f1a\u9009\u4e00\u4e2a\u4ece\u4f5c\u4e3a\u4e3b\u673a\uff0c\u4f46\u662f\u6b64\u65f6\u7684\u4ece\u6ca1\u6709\u9501\u6807\u8bc6\uff0c\u6b64\u65f6\uff0c\u5176\u5b83\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u83b7\u53d6\u5230\u9501\uff0c\u5bfc\u81f4\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<h2>\u56db\u3001Redisson\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501<\/h2>\n<p>Redisson\u662f\u4e00\u4e2a\u5728Redis\u7684\u57fa\u7840\u4e0a\u5b9e\u73b0\u7684Java\u9a7b\u5185\u5b58\u6570\u636e\u7f51\u683c\u3002\u5b83\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u5206\u5e03\u5f0f\u7684Java\u5e38\u7528\u5bf9\u8c61\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5206\u5e03\u5f0f\u670d\u52a1\uff0c\u5176\u4e2d\u5305\u542b\u5404\u79cd\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u3002<\/p>\n<h3>1\u3001pom<\/h3>\n<pre>\n   \n\n   \n    \n     org.redisson\n    \n    \n     redisson\n    \n    \n     3.13.6\n    \n   <\/pre>\n<h3>2\u3001\u914d\u7f6e\u7c7b<\/h3>\n<pre>package com.guor.config;\n\nimport org.redisson.Redisson;\nimport org.redisson.api.RedissonClient;\nimport org.redisson.config.Config;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration\npublic class RedissonConfig {\n\n    @Bean\n    public RedissonClient redissonClient(){\n        \/\/ \u914d\u7f6e\n        Config config = new Config();\n\n        \/**\n         * \u5355\u70b9\u5730\u5740useSingleServer\uff0c\u96c6\u7fa4\u5730\u5740useClusterServers\n         *\/\n        config.useSingleServer().setAddress(\"redis:\/\/127.0.0.1:6379\").setPassword(\"123456\");\n        \/\/ \u521b\u5efaRedissonClient\u5bf9\u8c61\n        return Redisson.create(config);\n    }\n}\n<\/pre>\n<h3>3\u3001\u6d4b\u8bd5\u7c7b<\/h3>\n<pre>package com.guor;\n\nimport lombok.extern.slf4j.Slf4j;\nimport org.junit.jupiter.api.BeforeEach;\nimport org.junit.jupiter.api.Test;\nimport org.redisson.api.RLock;\nimport org.redisson.api.RedissonClient;\nimport org.springframework.boot.test.context.SpringBootTest;\n\nimport javax.annotation.Resource;\nimport java.util.concurrent.TimeUnit;\n\n@Slf4j\n@SpringBootTest\nclass RedissonTest {\n\n    @Resource\n    private RedissonClient redissonClient;\n\n    private RLock lock;\n\n    @BeforeEach\n    void setUp() {\n    \t\/\/ \u83b7\u53d6\u6307\u5b9a\u540d\u79f0\u7684\u9501\n        lock = redissonClient.getLock(\"nezha\");\n    }\n\n    @Test\n    void test() throws InterruptedException {\n        \/\/ \u5c1d\u8bd5\u83b7\u53d6\u9501\n        boolean isLock = lock.tryLock(1L, TimeUnit.SECONDS);\n        if (!isLock) {\n            log.error(\"\u83b7\u53d6\u9501\u5931\u8d25\");\n            return;\n        }\n        try {\n            log.info(\"\u54ea\u5412\u6700\u5e05\uff0c\u54c8\u54c8\u54c8\");\n        } finally {\n            \/\/ \u91ca\u653e\u9501\n            lock.unlock();\n        }\n    }\n}\n<\/pre>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230512\/1683894430645e309ee4254.jpg1683894430645e309ee4268.jpg\" class=\"aligncenter\" title=\"Redis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\u63d2\u56fe\" alt=\"Redis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\u63d2\u56fe\" \/><\/p>\n<h2>\u4e94\u3001\u63a2\u7d22tryLock\u6e90\u7801<\/h2>\n<h3>1\u3001tryLock\u6e90\u7801<\/h3>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230512\/1683894431645e309f2661f.jpg1683894431645e309f26635.png\" class=\"aligncenter\" title=\"Redis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\u63d2\u56fe1\" alt=\"Redis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\u63d2\u56fe1\" \/><\/p>\n<h4>\u5c1d\u8bd5\u83b7\u53d6\u9501<\/h4>\n<pre>public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {\n\t\/\/ \u6700\u5927\u7b49\u5f85\u65f6\u95f4\n\tlong time = unit.toMillis(waitTime);\n\tlong current = System.currentTimeMillis();\n\tlong threadId = Thread.currentThread().getId();\n\tLong ttl = this.tryAcquire(waitTime, leaseTime, unit, threadId);\n\tif (ttl == null) {\n\t\treturn true;\n\t} else {\n\t\t\/\/ \u5269\u4f59\u7b49\u5f85\u65f6\u95f4 = \u6700\u5927\u7b49\u5f85\u65f6\u95f4 - \u83b7\u53d6\u9501\u5931\u8d25\u6d88\u8017\u7684\u65f6\u95f4\n\t\ttime -= System.currentTimeMillis() - current;\n\t\tif (time  subscribeFuture = this.subscribe(threadId);\n\t\t\t\/\/ \u5f53Future\u5728\u7b49\u5f85\u6307\u5b9a\u65f6\u95f4time\u5185\u5b8c\u6210\u65f6\uff0c\u8fd4\u56detrue\n\t\t\tif (!subscribeFuture.await(time, TimeUnit.MILLISECONDS)) {\n\t\t\t\tif (!subscribeFuture.cancel(false)) {\n\t\t\t\t\tsubscribeFuture.onComplete((res, e) -&gt; {\n\t\t\t\t\t\tif (e == null) {\n\t\t\t\t\t\t\t\/\/ \u53d6\u6d88\u8ba2\u9605\n\t\t\t\t\t\t\tthis.unsubscribe(subscribeFuture, threadId);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tthis.acquireFailed(waitTime, unit, threadId);\n\t\t\t\treturn false;\/\/ \u83b7\u53d6\u9501\u5931\u8d25\n\t\t\t} else {\n\t\t\t\ttry {\n\t\t\t\t\t\/\/ \u5269\u4f59\u7b49\u5f85\u65f6\u95f4 = \u5269\u4f59\u7b49\u5f85\u65f6\u95f4 - \u83b7\u53d6\u9501\u5931\u8d25\u6d88\u8017\u7684\u65f6\u95f4\n\t\t\t\t\ttime -= System.currentTimeMillis() - current;\n\t\t\t\t\tif (time = 0L &amp;&amp; ttl  0L);\n\n\t\t\t\t\t\tthis.acquireFailed(waitTime, unit, threadId);\n\t\t\t\t\t\tvar16 = false;\n\t\t\t\t\t\treturn var16;\n\t\t\t\t\t}\n\t\t\t\t} finally {\n\t\t\t\t\tthis.unsubscribe(subscribeFuture, threadId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n<\/pre>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230512\/1683894431645e309f5ea14.jpg1683894431645e309f5ea27.png\" class=\"aligncenter\" title=\"Redis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\u63d2\u56fe2\" alt=\"Redis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\u63d2\u56fe2\" \/><\/p>\n<h3>2\u3001\u91cd\u7f6e\u9501\u7684\u6709\u6548\u671f<\/h3>\n<pre>private void scheduleExpirationRenewal(long threadId) {\n\tRedissonLock.ExpirationEntry entry = new RedissonLock.ExpirationEntry();\n\t\/\/ this.getEntryName()\uff1a\u9501\u7684\u540d\u5b57\uff0c\u4e00\u4e2a\u9501\u5bf9\u5e94\u4e00\u4e2aentry\n\t\/\/ putIfAbsent\uff1a\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5c06\u9501\u548centry\u653e\u5230map\u91cc\n\tRedissonLock.ExpirationEntry oldEntry = (RedissonLock.ExpirationEntry)EXPIRATION_RENEWAL_MAP.putIfAbsent(this.getEntryName(), entry);\n\tif (oldEntry != null) {\n\t\t\/\/ \u540c\u4e00\u4e2a\u7ebf\u7a0b\u591a\u6b21\u83b7\u53d6\u9501\uff0c\u76f8\u5f53\u4e8e\u91cd\u5165\n\t\toldEntry.addThreadId(threadId);\n\t} else {\n\t\t\/\/ \u5982\u679c\u662f\u7b2c\u4e00\u6b21\n\t\tentry.addThreadId(threadId);\n\t\t\/\/ \u66f4\u65b0\u6709\u6548\u671f\n\t\tthis.renewExpiration();\n\t}\n}\n<\/pre>\n<p>\u66f4\u65b0\u6709\u6548\u671f\uff0c\u9012\u5f52\u8c03\u7528\u66f4\u65b0\u6709\u6548\u671f\uff0c\u6c38\u4e0d\u8fc7\u671f<\/p>\n<pre>private void renewExpiration() {\n\t\/\/ \u4ecemap\u4e2d\u5f97\u5230\u5f53\u524d\u9501\u7684entry\n\tRedissonLock.ExpirationEntry ee = (RedissonLock.ExpirationEntry)EXPIRATION_RENEWAL_MAP.get(this.getEntryName());\n\tif (ee != null) {\n\t\t\/\/ \u5f00\u542f\u5ef6\u65f6\u4efb\u52a1\n\t\tTimeout task = this.commandExecutor.getConnectionManager().newTimeout(new TimerTask() {\n\t\t\tpublic void run(Timeout timeout) throws Exception {\n\t\t\t\tRedissonLock.ExpirationEntry ent = (RedissonLock.ExpirationEntry)RedissonLock.EXPIRATION_RENEWAL_MAP.get(RedissonLock.this.getEntryName());\n\t\t\t\tif (ent != null) {\n\t\t\t\t\t\/\/ \u53d6\u51fa\u7ebf\u7a0bid\n\t\t\t\t\tLong threadId = ent.getFirstThreadId();\n\t\t\t\t\tif (threadId != null) {\n\t\t\t\t\t\t\/\/ \u5237\u65b0\u6709\u6548\u671f\n\t\t\t\t\t\tRFuture\n   \n     future = RedissonLock.this.renewExpirationAsync(threadId);\n\t\t\t\t\t\tfuture.onComplete((res, e) -&gt; {\n\t\t\t\t\t\t\tif (e != null) {\n\t\t\t\t\t\t\t\tRedissonLock.log.error(\"Can't update lock \" + RedissonLock.this.getName() + \" expiration\", e);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tif (res) {\n\t\t\t\t\t\t\t\t\t\/\/ \u9012\u5f52\u8c03\u7528\u66f4\u65b0\u6709\u6548\u671f\uff0c\u6c38\u4e0d\u8fc7\u671f\n\t\t\t\t\t\t\t\t\tRedissonLock.this.renewExpiration();\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}, this.internalLockLeaseTime \/ 3L, TimeUnit.MILLISECONDS);\/\/ 10S\n\t\tee.setTimeout(task);\n\t}\n}\n\n   <\/pre>\n<h4>\u66f4\u65b0\u6709\u6548\u671f<\/h4>\n<pre>protected RFuture\n   \n     renewExpirationAsync(long threadId) {\n\treturn this.evalWriteAsync(this.getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, \n\t\/\/ \u5224\u65ad\u5f53\u524d\u7ebf\u7a0b\u7684\u9501\u662f\u5426\u662f\u5f53\u524d\u7ebf\u7a0b\n\t\"if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then \n\t\t\/\/ \u66f4\u65b0\u6709\u6548\u671f\n\t\tredis.call('pexpire', KEYS[1], ARGV[1]); \n\t\treturn 1; \n\t\tend; \n\t\treturn 0;\", \n\t\tCollections.singletonList(this.getName()), this.internalLockLeaseTime, this.getLockName(threadId));\n}\n\n   <\/pre>\n<h3>3\u3001\u8c03\u7528lua\u811a\u672c<\/h3>\n<pre>\n   \n     RFuture\n    \n      tryLockInnerAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand\n     \n       command) { \/\/ \u9501\u91ca\u653e\u65f6\u95f4 this.internalLockLeaseTime = unit.toMillis(leaseTime); return this.evalWriteAsync(this.getName(), LongCodec.INSTANCE, command, \/\/ \u5224\u65ad\u9501\u6210\u529f \"if (redis.call('exists', KEYS[1]) == 0) then redis.call('hincrby', KEYS[1], ARGV[2], 1); \/\/ \u5982\u679c\u4e0d\u5b58\u5728\uff0c\u8bb0\u5f55\u9501\u6807\u8bc6\uff0c\u6b21\u6570+1 redis.call('pexpire', KEYS[1], ARGV[1]); \/\/ \u8bbe\u7f6e\u9501\u6709\u6548\u671f return nil; \/\/ \u76f8\u5f53\u4e8eJava\u7684null end; if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then redis.call('hincrby', KEYS[1], ARGV[2], 1); \/\/ \u5982\u679c\u5b58\u5728\uff0c\u5224\u65ad\u9501\u6807\u8bc6\u662f\u5426\u662f\u81ea\u5df1\u7684\uff0c\u6b21\u6570+1 redis.call('pexpire', KEYS[1], ARGV[1]); \/\/ \u8bbe\u7f6e\u9501\u6709\u6548\u671f return nil; end; \/\/ \u5224\u65ad\u9501\u5931\u8d25\uff0cpttl\uff1a\u6307\u5b9a\u9501\u5269\u4f59\u6709\u6548\u671f\uff0c\u5355\u4f4d\u6beb\u79d2\uff0cKEYS[1]\uff1a\u9501\u7684\u540d\u79f0 return redis.call('pttl', KEYS[1]);\", Collections.singletonList(this.getName()), this.internalLockLeaseTime, this.getLockName(threadId)); } \n     \n    \n   <\/pre>\n<h2>\u516d\u3001\u91ca\u653e\u9501unlock\u6e90\u7801<\/h2>\n<h3>1\u3001\u53d6\u6d88\u66f4\u65b0\u4efb\u52a1<\/h3>\n<pre>public RFuture\n   \n     unlockAsync(long threadId) {\n\tRPromise\n    \n      result = new RedissonPromise(); RFuture\n     \n       future = this.unlockInnerAsync(threadId); future.onComplete((opStatus, e) -&gt; { \/\/ \u53d6\u6d88\u66f4\u65b0\u4efb\u52a1 this.cancelExpirationRenewal(threadId); if (e != null) { result.tryFailure(e); } else if (opStatus == null) { IllegalMonitorStateException cause = new IllegalMonitorStateException(\"attempt to unlock lock, not locked by current thread by node id: \" + this.id + \" thread-id: \" + threadId); result.tryFailure(cause); } else { result.trySuccess((Object)null); } }); return result; } \n     \n    \n   <\/pre>\n<h3>2\u3001\u5220\u9664\u5b9a\u65f6\u4efb\u52a1<\/h3>\n<pre>void cancelExpirationRenewal(Long threadId) {\n\t\/\/ \u4ecemap\u4e2d\u53d6\u51fa\u5f53\u524d\u9501\u7684\u5b9a\u65f6\u4efb\u52a1entry\n\tRedissonLock.ExpirationEntry task = (RedissonLock.ExpirationEntry)EXPIRATION_RENEWAL_MAP.get(this.getEntryName());\n\tif (task != null) {\n\t\tif (threadId != null) {\n\t\t\ttask.removeThreadId(threadId);\n\t\t}\n\t\t\/\/ \u5220\u9664\u5b9a\u65f6\u4efb\u52a1\n\t\tif (threadId == null || task.hasNoThreads()) {\n\t\t\tTimeout timeout = task.getTimeout();\n\t\t\tif (timeout != null) {\n\t\t\t\ttimeout.cancel();\n\t\t\t}\n\n\t\t\tEXPIRATION_RENEWAL_MAP.remove(this.getEntryName());\n\t\t}\n\t}\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aRedis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8eredis\u7684\u8d44\u6599\u8bf7\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff01<\/p>\n<p>     \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1a\u811a\u672c\u4e4b\u5bb6 \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   golang\u4e2d&#8221;var&#8221;\u4e0e&#8221;:=&#8221;\u7684\u533a\u522b\u89e3\u6790\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Goland\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6837\u5f0f\u4e2d\u201c\\r\u201c\u7684\u4f5c\u7528\u8be6\u89e3\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f 0\u6d4f\u89c8 \u6536\u85cf 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\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\u300b\uff0c\u804a\u804a\u5206\u5e03\u5f0f\u3001\u9501\u3001JavaRedis\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 \u4e00\u3001\u5206\u5e03\u5f0f\u9501\u662f\u4ec0\u4e48 \u5206\u5e03\u5f0f\u9501\u662f \u6ee1\u8db3\u5206\u5e03\u5f0f\u7cfb\u7edf\u6216\u96c6\u7fa4\u6a21\u5f0f\u4e0b\u591a\u8fdb\u7a0b\u53ef\u89c1\u5e76\u4e14\u4e92\u65a5\u7684\u9501\u3002 \u57fa\u4e8eRedis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\uff1a 1\u3001\u83b7\u53d6\u9501 \u4e92\u65a5\uff1a\u786e\u4fdd\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u9501; \u975e\u963b\u585e\uff1a\u5c1d\u8bd5\u83b7\u53d6\u9501\uff0c\u6210\u529f\u8fd4\u56detrue\uff0c\u5931\u8d25\u8fd4\u56defalse\uff1b \u6dfb\u52a0\u9501\u8fc7\u671f\u65f6\u95f4\uff0c\u907f\u514d\u670d\u52a1\u5b95\u673a\u5f15\u8d77\u6b7b\u9501\u3002 SET lock thread1 NX EX 10 2\u3001\u91ca\u653e\u9501 \u624b\u52a8\u91ca\u653e\uff1bDEL key1 \u8d85\u65f6\u91ca\u653e\uff0c\u83b7\u53d6\u9501\u65f6\u6dfb\u52a0\u4e00\u4e2a\u8d85\u65f6\u9501\uff1b \u4e8c\u3001\u4ee3\u7801\u5b9e\u4f8b package com.guor.utils; import org.springframework.data.redis.core.StringRedisTemplate; import java.util.concurrent.TimeUnit; public class RedisLock implements ILock{ private String name; private StringRedisTemplate stringRedisTemplate; public RedisLock(String name, StringRedisTemplate stringRedisTemplate) { this.name = name; this.stringRedisTemplate = stringRedisTemplate; } private static [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-44927","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=44927"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44927\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=44927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=44927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=44927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}