{"id":166893,"date":"2024-11-05T20:30:26","date_gmt":"2024-11-05T12:30:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/166893\/"},"modified":"2024-11-05T20:30:26","modified_gmt":"2024-11-05T12:30:26","slug":"%e7%b4%af%e7%a9%8d%e9%87%91%e9%a1%8d%ef%bc%8credis%e5%af%a6%e7%8f%be%e6%9b%b4%e5%bf%ab%e9%80%9f%ef%bc%88redis%e9%87%91%e9%a1%8d%e7%b4%af%e5%8a%a0%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/166893\/","title":{"rendered":"\u7d2f\u7a4d\u91d1\u984d\uff0cRedis\u5be6\u73fe\u66f4\u5feb\u901f\uff08redis\u91d1\u984d\u7d2f\u52a0\uff09"},"content":{"rendered":"<h1 id=\"%e7%b4%af%e7%a9%8d%e9%87%91%e9%a1%8d%ef%bc%8credis%e5%af%a6%e7%8f%be%e6%9b%b4%e5%bf%ab%e9%80%9f%ef%bc%88redis%e9%87%91%e9%a1%8d%e7%b4%af%e5%8a%a0%ef%bc%89-zKVUGVYZeK\">\u7d2f\u7a4d\u91d1\u984d\uff0cRedis\u5be6\u73fe\u66f4\u5feb\u901f\uff08redis\u91d1\u984d\u7d2f\u52a0\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u5feb\u901f\u4e14\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u5c0d\u65bc\u4f01\u696d\u7684\u6210\u529f\u81f3\u95dc\u91cd\u8981\u3002\u5c24\u5176\u662f\u5728\u91d1\u878d\u3001\u96fb\u5b50\u5546\u52d9\u7b49\u884c\u696d\uff0c\u7d2f\u7a4d\u91d1\u984d\u7684\u8a08\u7b97\u548c\u7ba1\u7406\u662f\u65e5\u5e38\u904b\u71df\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5353\u8d8a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff0c\u6210\u70ba\u4e86\u8a31\u591a\u4f01\u696d\u7684\u9996\u9078\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u5be6\u73fe\u91d1\u984d\u7684\u5feb\u901f\u7d2f\u52a0\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e7%89%b9%e6%80%a7-zKVUGVYZeK\">Redis\u7684\u7279\u6027<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\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\u7b49\u3002\u5176\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u80fd\u5920\u4ee5\u6beb\u79d2\u7d1a\u7684\u5ef6\u9072\u8655\u7406\u6578\u767e\u842c\u6b21\u64cd\u4f5c\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u5834\u666f\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u662f\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u6578\u64da\u6301\u4e45\u5316\uff0c\u80fd\u5920\u5c07\u6578\u64da\u4fdd\u5b58\u5230\u78c1\u789f\u4e2d\uff0c\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li><strong>\u539f\u5b50\u64cd\u4f5c\uff1a<\/strong>Redis\u652f\u6301\u539f\u5b50\u64cd\u4f5c\uff0c\u9019\u610f\u5473\u8457\u5728\u9032\u884c\u91d1\u984d\u7d2f\u52a0\u6642\uff0c\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%87%91%e9%a1%8d%e7%b4%af%e5%8a%a0%e7%9a%84%e9%9c%80%e6%b1%82-zKVUGVYZeK\">\u91d1\u984d\u7d2f\u52a0\u7684\u9700\u6c42<\/h2>\n<p>\u5728\u8a31\u591a\u61c9\u7528\u4e2d\uff0c\u91d1\u984d\u7684\u7d2f\u52a0\u662f\u5e38\u898b\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u96fb\u5b50\u5546\u52d9\u5e73\u53f0\u4e2d\uff0c\u4f7f\u7528\u8005\u7684\u9918\u984d\u3001\u8a02\u55ae\u91d1\u984d\u7b49\u90fd\u9700\u8981\u9032\u884c\u7d2f\u52a0\u8a08\u7b97\u3002\u50b3\u7d71\u7684\u6578\u64da\u5eab\u5728\u8655\u7406\u9019\u985e\u64cd\u4f5c\u6642\uff0c\u53ef\u80fd\u6703\u9762\u81e8\u6027\u80fd\u74f6\u9838\uff0c\u5c24\u5176\u662f\u5728\u9ad8\u4f75\u767c\u7684\u60c5\u6cc1\u4e0b\u3002\u9019\u6642\uff0cRedis\u7684\u512a\u52e2\u4fbf\u986f\u800c\u6613\u898b\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8redis%e9%80%b2%e8%a1%8c%e9%87%91%e9%a1%8d%e7%b4%af%e5%8a%a0-zKVUGVYZeK\">\u4f7f\u7528Redis\u9032\u884c\u91d1\u984d\u7d2f\u52a0<\/h2>\n<p>\u5728Redis\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u985e\u578b\u4f86\u5b58\u5132\u91d1\u984d\uff0c\u4e26\u5229\u7528\u5176\u539f\u5b50\u64cd\u4f5c\u4f86\u5be6\u73fe\u91d1\u984d\u7684\u7d2f\u52a0\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Redis\u9032\u884c\u91d1\u984d\u7684\u7d2f\u52a0\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\u670d\u52d9\u5668\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u8a2d\u5b9a\u521d\u59cb\u91d1\u984d\nr.set('user:1000:balance', 100)\n\n# \u57f7\u884c\u91d1\u984d\u7d2f\u52a0\nr.incrby('user:1000:balance', 50)\n\n# \u7372\u53d6\u7576\u524d\u91d1\u984d\ncurrent_balance = r.get('user:1000:balance')\nprint(f'\u7576\u524d\u91d1\u984d: {current_balance.decode(\"utf-8\")} \u5143')\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u9023\u63a5\u5230Redis\u670d\u52d9\u5668\uff0c\u7136\u5f8c\u8a2d\u7f6e\u4e00\u500b\u7528\u6236\u7684\u521d\u59cb\u91d1\u984d\u3002\u63a5\u8457\uff0c\u6211\u5011\u4f7f\u7528 <code>incrby<\/code> \u65b9\u6cd5\u5c07\u91d1\u984d\u589e\u52a050\u5143\uff0c\u6700\u5f8c\u7372\u53d6\u4e26\u6253\u5370\u7576\u524d\u91d1\u984d\u3002\u9019\u7a2e\u65b9\u5f0f\u4e0d\u50c5\u7c21\u55ae\uff0c\u800c\u4e14\u80fd\u5920\u5728\u9ad8\u4f75\u767c\u7684\u60c5\u6cc1\u4e0b\u4fdd\u6301\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<h2 id=\"%e6%80%a7%e8%83%bd%e5%84%aa%e5%8b%a2-zKVUGVYZeK\">\u6027\u80fd\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528Redis\u9032\u884c\u91d1\u984d\u7d2f\u52a0\u7684\u4e3b\u8981\u512a\u52e2\u5728\u65bc\u5176\u9ad8\u6548\u7684\u6027\u80fd\u3002\u7531\u65bcRedis\u662f\u57fa\u65bc\u5167\u5b58\u7684\u6578\u64da\u5eab\uff0c\u6240\u6709\u64cd\u4f5c\u90fd\u5728\u5167\u5b58\u4e2d\u9032\u884c\uff0c\u9019\u4f7f\u5f97\u5176\u8b80\u5beb\u901f\u5ea6\u6975\u5feb\u3002\u6b64\u5916\uff0cRedis\u7684\u539f\u5b50\u64cd\u4f5c\u78ba\u4fdd\u4e86\u5728\u591a\u500b\u5ba2\u6236\u7aef\u540c\u6642\u9032\u884c\u91d1\u984d\u7d2f\u52a0\u6642\uff0c\u4e0d\u6703\u51fa\u73fe\u6578\u64da\u7af6\u722d\u7684\u554f\u984c\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-zKVUGVYZeK\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u9700\u8981\u5feb\u901f\u7d2f\u52a0\u91d1\u984d\u7684\u61c9\u7528\u5834\u666f\u4e2d\uff0cRedis\u63d0\u4f9b\u4e86\u4e00\u500b\u9ad8\u6548\u4e14\u53ef\u9760\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u5176\u9ad8\u6027\u80fd\u3001\u539f\u5b50\u64cd\u4f5c\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5be6\u73fe\u91d1\u984d\u7684\u7d2f\u52a0\u529f\u80fd\u3002\u5c0d\u65bc\u5e0c\u671b\u63d0\u5347\u6578\u64da\u8655\u7406\u80fd\u529b\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u6280\u8853\u81f3\u95dc\u91cd\u8981\u3002\u82e5\u60a8\u5c0d\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u53ca\u5176\u5728Redis\u61c9\u7528\u4e2d\u7684\u6f5b\u529b\u611f\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u5be6\u73fe\u91d1\u984d\u7d2f\u52a0\uff0c\u63d0\u5347\u7d2f\u7a4d\u91d1\u984d\u7684\u901f\u5ea6\u8207\u6548\u7387\uff0c\u8b93\u6578\u64da\u8655\u7406\u66f4\u5feb\u901f\u3001\u6e96\u78ba\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-166893","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166893","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=166893"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166893\/revisions"}],"predecessor-version":[{"id":166894,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166893\/revisions\/166894"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=166893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=166893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=166893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}