{"id":167483,"date":"2024-11-06T01:02:19","date_gmt":"2024-11-05T17:02:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167483\/"},"modified":"2024-11-06T01:02:20","modified_gmt":"2024-11-05T17:02:20","slug":"%e5%88%a9%e7%94%a8redis%e6%9e%b6%e6%a7%8b%e6%94%b9%e5%96%84%e6%9c%8d%e5%8b%99%e5%99%a8%e6%80%a7%e8%83%bd%e7%9a%84%e6%9c%89%e6%95%88%e6%8e%aa%e6%96%bd%ef%bc%88redis-%e6%9e%b6%e6%a7%8bcr%e3%80%8a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167483\/","title":{"rendered":"\u5229\u7528Redis\u67b6\u69cb\u6539\u5584\u670d\u52d9\u5668\u6027\u80fd\u7684\u6709\u6548\u63aa\u65bd\uff08redis \u67b6\u69cbcr\u300a\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e6%9e%b6%e6%a7%8b%e6%94%b9%e5%96%84%e6%9c%8d%e5%8b%99%e5%99%a8%e6%80%a7%e8%83%bd%e7%9a%84%e6%9c%89%e6%95%88%e6%8e%aa%e6%96%bd-QqyorvXWpK\">\u5229\u7528Redis\u67b6\u69cb\u6539\u5584\u670d\u52d9\u5668\u6027\u80fd\u7684\u6709\u6548\u63aa\u65bd<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u670d\u52d9\u5668\u6027\u80fd\u7684\u512a\u5316\u5c0d\u65bc\u63d0\u4f9b\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5e8f\u548c\u670d\u52d9\u81f3\u95dc\u91cd\u8981\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5353\u8d8a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u67b6\u69cb\u4f86\u6539\u5584\u670d\u52d9\u5668\u6027\u80fd\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u6709\u6548\u7684\u63aa\u65bd\u548c\u5be6\u8e10\u5efa\u8b70\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-QqyorvXWpK\">Redis\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u5408\u7b49\u3002\u7531\u65bc\u5176\u6578\u64da\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0cRedis\u80fd\u5920\u63d0\u4f9b\u6975\u5feb\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u8a31\u591a\u9ad8\u6027\u80fd\u61c9\u7528\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"redis%e5%9c%a8%e6%9c%8d%e5%8b%99%e5%99%a8%e6%80%a7%e8%83%bd%e5%84%aa%e5%8c%96%e4%b8%ad%e7%9a%84%e6%87%89%e7%94%a8-QqyorvXWpK\">Redis\u5728\u670d\u52d9\u5668\u6027\u80fd\u512a\u5316\u4e2d\u7684\u61c9\u7528<\/h2>\n<h3 id=\"1-%e7%b7%a9%e5%ad%98%e6%a9%9f%e5%88%b6-QqyorvXWpK\">1. \u7de9\u5b58\u6a5f\u5236<\/h3>\n<p>\u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\u5c64\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u6578\u64da\u5eab\u7684\u8ca0\u64d4\u3002\u7576\u7528\u6236\u8acb\u6c42\u6578\u64da\u6642\uff0c\u9996\u5148\u67e5\u8a62Redis\u7de9\u5b58\uff0c\u5982\u679c\u6578\u64da\u5b58\u5728\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5247\u518d\u67e5\u8a62\u5f8c\u7aef\u6578\u64da\u5eab\u3002\u9019\u7a2e\u65b9\u5f0f\u4e0d\u50c5\u63d0\u9ad8\u4e86\u97ff\u61c9\u901f\u5ea6\uff0c\u9084\u6e1b\u5c11\u4e86\u6578\u64da\u5eab\u7684\u67e5\u8a62\u6b21\u6578\u3002<\/p>\n<pre><code>if (redis.exists(key)) {\n    return redis.get(key);\n} else {\n    data = database.query(key);\n    redis.set(key, data);\n    return data;\n}<\/code><\/pre>\n<h3 id=\"2-%e5%af%a6%e6%99%82%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86-QqyorvXWpK\">2. \u5be6\u6642\u6578\u64da\u8655\u7406<\/h3>\n<p>Redis\u652f\u6301\u767c\u5e03\/\u8a02\u95b1\u6a21\u5f0f\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u7528\u65bc\u5be6\u6642\u6578\u64da\u8655\u7406\u3002\u4f8b\u5982\uff0c\u5728\u5373\u6642\u804a\u5929\u61c9\u7528\u4e2d\uff0c\u7576\u4e00\u500b\u7528\u6236\u767c\u9001\u6d88\u606f\u6642\uff0c\u8a72\u6d88\u606f\u53ef\u4ee5\u901a\u904eRedis\u7684\u767c\u5e03\/\u8a02\u95b1\u6a5f\u5236\u8fc5\u901f\u63a8\u9001\u7d66\u6240\u6709\u5728\u7dda\u7528\u6236\uff0c\u5f9e\u800c\u5be6\u73fe\u4f4e\u5ef6\u9072\u7684\u6d88\u606f\u50b3\u905e\u3002<\/p>\n<pre><code>redis.publish('chat_channel', message);<\/code><\/pre>\n<h3 id=\"3-%e6%95%b8%e6%93%9a%e6%8c%81%e4%b9%85%e5%8c%96-QqyorvXWpK\">3. \u6578\u64da\u6301\u4e45\u5316<\/h3>\n<p>\u96d6\u7136Redis\u4e3b\u8981\u7528\u4f5c\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u6578\u64da\u6301\u4e45\u5316\u7684\u529f\u80fd\u3002\u901a\u904eRDB\u5feb\u7167\u6216AOF\uff08Append Only File\uff09\u6a21\u5f0f\uff0cRedis\u53ef\u4ee5\u5c07\u5167\u5b58\u4e2d\u7684\u6578\u64da\u5b9a\u671f\u4fdd\u5b58\u5230\u78c1\u789f\u4e2d\uff0c\u9019\u6a23\u5373\u4f7f\u670d\u52d9\u5668\u91cd\u555f\uff0c\u6578\u64da\u4e5f\u4e0d\u6703\u4e1f\u5931\u3002<\/p>\n<pre><code>redis.config('SET', 'save', '900 1'); \/\/ \u6bcf900\u79d2\u4fdd\u5b58\u4e00\u6b21<\/code><\/pre>\n<h3 id=\"4-%e5%88%86%e4%bd%88%e5%bc%8f%e9%8e%96-QqyorvXWpK\">4. \u5206\u4f48\u5f0f\u9396<\/h3>\n<p>\u5728\u591a\u500b\u670d\u52d9\u5668\u4e4b\u9593\u5354\u8abf\u8cc7\u6e90\u6642\uff0cRedis\u53ef\u4ee5\u7528\u4f5c\u5206\u4f48\u5f0f\u9396\u7684\u5be6\u73fe\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u591a\u500b\u5be6\u4f8b\u540c\u6642\u8a2a\u554f\u5171\u4eab\u8cc7\u6e90\uff0c\u5f9e\u800c\u63d0\u9ad8\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/p>\n<pre><code>if (redis.setnx('lock_key', 'locked')) {\n    \/\/ \u7372\u53d6\u9396\uff0c\u57f7\u884c\u64cd\u4f5c\n    redis.del('lock_key'); \/\/ \u91cb\u653e\u9396\n}<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-QqyorvXWpK\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li>\u5b9a\u671f\u6e05\u7406\u904e\u671f\u7684\u7de9\u5b58\u6578\u64da\uff0c\u4ee5\u91cb\u653e\u5167\u5b58\u8cc7\u6e90\u3002<\/li>\n<li>\u6839\u64da\u5be6\u969b\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u6301\u4e45\u5316\u7b56\u7565\u3002<\/li>\n<li>\u4f7f\u7528Redis\u96c6\u7fa4\u4f86\u64f4\u5c55\u6027\u80fd\uff0c\u5206\u6563\u8ca0\u8f09\u3002<\/li>\n<li>\u76e3\u63a7Redis\u7684\u6027\u80fd\u6307\u6a19\uff0c\u53ca\u6642\u8abf\u6574\u914d\u7f6e\u4ee5\u9069\u61c9\u8b8a\u5316\u7684\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-QqyorvXWpK\">\u7e3d\u7d50<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u80fd\u5920\u901a\u904e\u7de9\u5b58\u3001\u5be6\u6642\u6578\u64da\u8655\u7406\u3001\u6578\u64da\u6301\u4e45\u5316\u548c\u5206\u4f48\u5f0f\u9396\u7b49\u529f\u80fd\u986f\u8457\u6539\u5584\u670d\u52d9\u5668\u6027\u80fd\u3002\u5c0d\u65bc\u5e0c\u671b\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u97ff\u61c9\u901f\u5ea6\u548c\u7cfb\u7d71\u7a69\u5b9a\u6027\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u5408\u7406\u5229\u7528Redis\u67b6\u69cb\u5c07\u662f\u975e\u5e38\u6709\u6548\u7684\u63aa\u65bd\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u90e8\u7f72\u60a8\u7684Redis\u670d\u52d9\uff0c\u5c07\u6709\u52a9\u65bc\u9032\u4e00\u6b65\u63d0\u5347\u60a8\u7684\u670d\u52d9\u5668\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u6709\u6548\u5229\u7528Redis\u67b6\u69cb\u63d0\u5347\u670d\u52d9\u5668\u6027\u80fd\u7684\u7b56\u7565\uff0c\u512a\u5316\u6578\u64da\u5b58\u53d6\u8207\u8655\u7406\u6548\u7387\uff0c\u5be6\u73fe\u66f4\u5feb\u7684\u97ff\u61c9\u6642\u9593\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-167483","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167483","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=167483"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167483\/revisions"}],"predecessor-version":[{"id":167484,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167483\/revisions\/167484"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}