{"id":202219,"date":"2025-05-10T14:39:50","date_gmt":"2025-05-10T06:39:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202219\/"},"modified":"2025-05-10T14:39:50","modified_gmt":"2025-05-10T06:39:50","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8redis%e5%92%8ckotlin%e5%bc%80%e5%8f%91%e5%bc%82%e6%ad%a5%e4%bb%bb%e5%8a%a1%e9%98%9f%e5%88%97%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202219\/","title":{"rendered":"\u5982\u4f55\u5229\u7528Redis\u548cKotlin\u5f00\u53d1\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u529f\u80fd"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5229\u7528Redis\u548cKotlin\u5f00\u53d1\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u529f\u80fd<\/h1>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5229\u7528Redis\u548cKotlin\u5f00\u53d1\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u529f\u80fd\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6570\u636e\u5e93<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6570\u636e\u5e93<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p>\u5982\u4f55\u5229\u7528Redis\u548cKotlin\u5f00\u53d1\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u529f\u80fd<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u5f02\u6b65\u4efb\u52a1\u7684\u5904\u7406\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u9700\u8981\u8017\u65f6\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u53d1\u9001\u90ae\u4ef6\u3001\u5904\u7406\u5927\u6570\u636e\u7b49\u7b49\u3002\u4e3a\u4e86\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u53ef\u4f38\u7f29\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u6765\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Redis\u548cKotlin\u6765\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u6b65\u4efb\u52a1\u961f\u5217\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662f\u5f02\u6b65\u4efb\u52a1\u961f\u5217<br \/>\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u662f\u4e00\u79cd\u5c06\u8017\u65f6\u8f83\u957f\u7684\u4efb\u52a1\u653e\u5165\u961f\u5217\u4e2d\u5f02\u6b65\u6267\u884c\u7684\u673a\u5236\u3002\u901a\u8fc7\u5c06\u4efb\u52a1\u653e\u5165\u961f\u5217\u4e2d\uff0c\u7cfb\u7edf\u53ef\u4ee5\u7acb\u5373\u8fd4\u56de\u7ed9\u7528\u6237\uff0c\u4e0d\u9700\u8981\u7b49\u5f85\u4efb\u52a1\u7684\u6267\u884c\u5b8c\u6210\u3002\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u901a\u5e38\u91c7\u7528\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\uff0c\u5373\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u751f\u4ea7\u8005\u5411\u961f\u5217\u4e2d\u6dfb\u52a0\u4efb\u52a1\uff0c\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u6d88\u8d39\u8005\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u5e76\u6267\u884c\u3002<\/p>\n<p>\u4e8c\u3001Redis\u7684\u4f18\u52bf<br \/>Redis\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u952e\u503c\u5b58\u50a8\u7cfb\u7edf\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u96c6\u5408\u3001\u54c8\u5e0c\u8868\u7b49\u3002Redis\u7684\u9ad8\u6027\u80fd\u548c\u7075\u6d3b\u5ea6\u4f7f\u5f97\u5b83\u6210\u4e3a\u5f00\u53d1\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u7684\u7406\u60f3\u9009\u62e9\u3002\u5728Redis\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\uff08List\uff09\u6570\u636e\u7ed3\u6784\u4f5c\u4e3a\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u53d1\u5e03-\u8ba2\u9605\uff08Pub\/Sub\uff09\u6a21\u5f0f\u5b9e\u73b0\u4efb\u52a1\u7684\u5206\u53d1\u3002<\/p>\n<p>\u4e09\u3001\u4f7f\u7528Redis\u548cKotlin\u5b9e\u73b0\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u7684\u6b65\u9aa4<\/p>\n<ol>\n<li>\n<p>\u6dfb\u52a0Redis\u4f9d\u8d56<br \/>\u9996\u5148\uff0c\u5728Kotlin\u9879\u76ee\u7684build.gradle\u6587\u4ef6\u4e2d\u6dfb\u52a0Redis\u5ba2\u6237\u7aef\u7684\u4f9d\u8d56\uff1a<\/p>\n<pre>dependencies {\n implementation 'redis.clients:jedis:3.7.0'\n}<\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efa\u751f\u4ea7\u8005<br \/>\u521b\u5efa\u4e00\u4e2aProducer\u7c7b\uff0c\u8d1f\u8d23\u5c06\u4efb\u52a1\u6dfb\u52a0\u5230Redis\u7684\u4efb\u52a1\u961f\u5217\u4e2d\uff1a<\/p>\n<pre>import redis.clients.jedis.Jedis\nimport redis.clients.jedis.JedisPool\n\nclass Producer {\n private val redisHost = \"localhost\"  \/\/ Redis\u7684\u4e3b\u673a\u5730\u5740\n private val redisPort = 6379  \/\/ Redis\u7684\u7aef\u53e3\u53f7\n private val jedisPool = JedisPool(redisHost, redisPort)\n \n fun addTask(task: String) {\n     val jedis = jedisPool.resource\n     jedis.rpush(\"task_queue\", task)  \/\/ \u5c06\u4efb\u52a1\u6dfb\u52a0\u5230\u4efb\u52a1\u961f\u5217\u4e2d\n     jedis.close()\n }\n}<\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efa\u6d88\u8d39\u8005<br \/>\u521b\u5efa\u4e00\u4e2aConsumer\u7c7b\uff0c\u8d1f\u8d23\u4eceRedis\u7684\u4efb\u52a1\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u5e76\u6267\u884c\uff1a<\/p>\n<pre>import redis.clients.jedis.Jedis\nimport redis.clients.jedis.JedisPool\n\nclass Consumer {\n private val redisHost = \"localhost\"  \/\/ Redis\u7684\u4e3b\u673a\u5730\u5740\n private val redisPort = 6379  \/\/ Redis\u7684\u7aef\u53e3\u53f7\n private val jedisPool = JedisPool(redisHost, redisPort)\n \n fun start() {\n     val jedis = jedisPool.resource\n     while (true) {\n         val task = jedis.blpop(0, \"task_queue\")[1]  \/\/ \u4ece\u4efb\u52a1\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\n         executeTask(task)  \/\/ \u6267\u884c\u4efb\u52a1\n     }\n     jedis.close()\n }\n \n private fun executeTask(task: String) {\n     \/\/ \u6267\u884c\u4efb\u52a1\u7684\u5177\u4f53\u4ee3\u7801\u903b\u8f91\n     println(\"\u6267\u884c\u4efb\u52a1\uff1a$task\")\n }\n}<\/pre>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5<br \/>\u5728\u4e3b\u51fd\u6570\u4e2d\u521b\u5efa\u4e00\u4e2aProducer\u5bf9\u8c61\uff0c\u5c06\u4efb\u52a1\u6dfb\u52a0\u5230\u4efb\u52a1\u961f\u5217\u4e2d\u3002\u7136\u540e\u521b\u5efa\u4e00\u4e2aConsumer\u5bf9\u8c61\uff0c\u542f\u52a8consumer\u7684start\u51fd\u6570\u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre>fun main() {\n val producer = Producer()\n producer.addTask(\"task1\")\n producer.addTask(\"task2\")\n \n val consumer = Consumer()\n consumer.start()\n}<\/pre>\n<\/li>\n<\/ol>\n<p>\u56db\u3001\u603b\u7ed3<br \/>\u901a\u8fc7\u4f7f\u7528Redis\u548cKotlin\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u3002Redis\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7684\u952e\u503c\u5b58\u50a8\u548c\u53d1\u5e03-\u8ba2\u9605\u529f\u80fd\uff0c\u800cKotlin\u5219\u63d0\u4f9b\u4e86\u7b80\u6d01\u4f18\u96c5\u7684\u4ee3\u7801\u7f16\u5199\u65b9\u5f0f\u3002\u901a\u8fc7\u5c06\u8017\u65f6\u7684\u4efb\u52a1\u653e\u5165\u4efb\u52a1\u961f\u5217\u4e2d\u5f02\u6b65\u6267\u884c\uff0c\u6211\u4eec\u80fd\u591f\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u53ef\u4f38\u7f29\u6027\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5229\u7528Redis\u548cKotlin\u5f00\u53d1\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u529f\u80fd\u7684\u5177\u4f53\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\uff0c\u8c22\u8c22\u9605\u8bfb\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Golang\u5f00\u53d1\uff1a\u6784\u5efa\u652f\u6301\u5e76\u53d1\u7684\u7f51\u7edc\u722c\u866b\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5229\u7528MySQL\u548cC++\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u6279\u91cf\u52a0\u5bc6\u529f\u80fd\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528Redis\u548cKotlin&#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-202219","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202219","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=202219"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202219\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}