{"id":125495,"date":"2024-10-25T23:06:22","date_gmt":"2024-10-25T15:06:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/125495\/"},"modified":"2024-10-25T23:06:22","modified_gmt":"2024-10-25T15:06:22","slug":"redis-%e5%bf%ab%e9%80%9f%e6%b8%ac%e8%a9%a6%e7%b0%a1%e5%96%ae%e5%91%bd%e4%bb%a4%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/125495\/","title":{"rendered":"Redis \u5feb\u901f\u6e2c\u8a66\u7c21\u55ae\u547d\u4ee4\u64cd\u4f5c"},"content":{"rendered":"<h1 id=\"redis-%e5%bf%ab%e9%80%9f%e6%b8%ac%e8%a9%a6%e7%b0%a1%e5%96%ae%e5%91%bd%e4%bb%a4%e6%93%8d%e4%bd%9c-HspzOUIxPE\">Redis \u5feb\u901f\u6e2c\u8a66\u7c21\u55ae\u547d\u4ee4\u64cd\u4f5c<\/h1>\n<p>Redis \u662f\u4e00\u500b\u9ad8\u6548\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u5b58\u5132\u7b49\u5834\u666f\u3002\u7531\u65bc\u5176\u5feb\u901f\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u80fd\u529b\uff0cRedis \u6210\u70ba\u8a31\u591a\u958b\u767c\u8005\u548c\u4f01\u696d\u7684\u9996\u9078\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u4e00\u4e9b\u57fa\u672c\u7684 Redis \u547d\u4ee4\uff0c\u5e6b\u52a9\u7528\u6236\u5feb\u901f\u4e0a\u624b\u4e26\u9032\u884c\u7c21\u55ae\u7684\u6e2c\u8a66\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-redis-HspzOUIxPE\">\u5b89\u88dd Redis<\/h2>\n<p>\u5728\u958b\u59cb\u4f7f\u7528 Redis \u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd\u5b83\u3002\u5c0d\u65bc\u5927\u591a\u6578 Linux \u767c\u884c\u7248\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install redis-server\n<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5 Redis \u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl start redis\n<\/code><\/pre>\n<p>\u8981\u6aa2\u67e5 Redis \u662f\u5426\u6b63\u5728\u904b\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>sudo systemctl status redis\n<\/code><\/pre>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e5%91%bd%e4%bb%a4%e6%93%8d%e4%bd%9c-HspzOUIxPE\">\u57fa\u672c\u547d\u4ee4\u64cd\u4f5c<\/h2>\n<p>Redis \u63d0\u4f9b\u4e86\u591a\u7a2e\u547d\u4ee4\u4f86\u64cd\u4f5c\u6578\u64da\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u57fa\u672c\u547d\u4ee4\uff1a<\/p>\n<h3 id=\"1-set-%e5%92%8c-get-HspzOUIxPE\">1. SET \u548c GET<\/h3>\n<p>SET \u547d\u4ee4\u7528\u65bc\u8a2d\u7f6e\u9375\u7684\u503c\uff0c\u800c GET \u547d\u4ee4\u5247\u7528\u65bc\u7372\u53d6\u9375\u7684\u503c\u3002<\/p>\n<pre><code>SET mykey \"Hello, Redis!\"\nGET mykey\n<\/code><\/pre>\n<p>\u57f7\u884c\u4e0a\u8ff0\u547d\u4ee4\u5f8c\uff0cGET \u547d\u4ee4\u5c07\u8fd4\u56de &#8220;Hello, Redis!&#8221;\u3002<\/p>\n<h3 id=\"2-del-HspzOUIxPE\">2. DEL<\/h3>\n<p>DEL \u547d\u4ee4\u7528\u65bc\u522a\u9664\u6307\u5b9a\u7684\u9375\u3002<\/p>\n<pre><code>DEL mykey\nGET mykey\n<\/code><\/pre>\n<p>\u57f7\u884c DEL \u547d\u4ee4\u5f8c\uff0c\u518d\u6b21\u57f7\u884c GET \u547d\u4ee4\u5c07\u8fd4\u56de nil\uff0c\u8868\u793a\u8a72\u9375\u5df2\u88ab\u522a\u9664\u3002<\/p>\n<h3 id=\"3-expire-HspzOUIxPE\">3. EXPIRE<\/h3>\n<p>EXPIRE \u547d\u4ee4\u7528\u65bc\u8a2d\u7f6e\u9375\u7684\u904e\u671f\u6642\u9593\uff08\u4ee5\u79d2\u70ba\u55ae\u4f4d\uff09\u3002<\/p>\n<pre><code>SET tempkey \"This will expire\"\nEXPIRE tempkey 10\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u547d\u4ee4\u5c07\u5728 10 \u79d2\u5f8c\u81ea\u52d5\u522a\u9664 tempkey\u3002<\/p>\n<h3 id=\"4-list-%e6%93%8d%e4%bd%9c-HspzOUIxPE\">4. LIST \u64cd\u4f5c<\/h3>\n<p>Redis \u9084\u652f\u6301\u5217\u8868\u64cd\u4f5c\uff0c\u4f7f\u7528 LPUSH \u548c LRANGE \u547d\u4ee4\u4f86\u6dfb\u52a0\u548c\u7372\u53d6\u5217\u8868\u5143\u7d20\u3002<\/p>\n<pre><code>LPUSH mylist \"first\"\nLPUSH mylist \"second\"\nLRANGE mylist 0 -1\n<\/code><\/pre>\n<p>\u57f7\u884c LRANGE \u547d\u4ee4\u5c07\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<h3 id=\"5-hash-%e6%93%8d%e4%bd%9c-HspzOUIxPE\">5. HASH \u64cd\u4f5c<\/h3>\n<p>HASH \u662f Redis \u4e2d\u7684\u4e00\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u7528\u4f86\u5b58\u5132\u5c0d\u8c61\u7684\u5c6c\u6027\u3002<\/p>\n<pre><code>HSET user:1000 name \"Alice\"\nHSET user:1000 age 30\nHGETALL user:1000\n<\/code><\/pre>\n<p>HGETALL \u547d\u4ee4\u5c07\u8fd4\u56de\u8a72\u7528\u6236\u7684\u6240\u6709\u5c6c\u6027\u3002<\/p>\n<h2 id=\"%e6%b8%ac%e8%a9%a6-redis-%e6%80%a7%e8%83%bd-HspzOUIxPE\">\u6e2c\u8a66 Redis \u6027\u80fd<\/h2>\n<p>\u70ba\u4e86\u6e2c\u8a66 Redis \u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528 Redis \u81ea\u5e36\u7684\u57fa\u6e96\u6e2c\u8a66\u5de5\u5177 redis-benchmark\u3002\u9019\u500b\u5de5\u5177\u53ef\u4ee5\u5e6b\u52a9\u7528\u6236\u6e2c\u8a66 Redis \u5728\u4e0d\u540c\u64cd\u4f5c\u4e0b\u7684\u6027\u80fd\u3002<\/p>\n<pre><code>redis-benchmark -q -n 100000 -c 50 -P 10\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u547d\u4ee4\u5c07\u9032\u884c 100,000 \u6b21\u8acb\u6c42\uff0c\u4e26\u4f7f\u7528 50 \u500b\u4e26\u767c\u9023\u63a5\uff0c10 \u500b\u7ba1\u9053\u9032\u884c\u6e2c\u8a66\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HspzOUIxPE\">\u7e3d\u7d50<\/h2>\n<p>Redis \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u9069\u5408\u7528\u65bc\u591a\u7a2e\u61c9\u7528\u5834\u666f\u3002\u901a\u904e\u672c\u6587\u4ecb\u7d39\u7684\u57fa\u672c\u547d\u4ee4\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b Redis \u4e26\u9032\u884c\u7c21\u55ae\u7684\u6578\u64da\u64cd\u4f5c\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u90e8\u7f72 Redis\uff0c\u4eab\u53d7\u66f4\u5feb\u7684\u6578\u64da\u8655\u7406\u901f\u5ea6\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5feb\u901f\u5b78\u7fd2Redis\u57fa\u672c\u547d\u4ee4\u64cd\u4f5c\uff0c\u8f15\u9b06\u9032\u884c\u6e2c\u8a66\uff0c\u63d0\u5347\u6578\u64da\u5eab\u7ba1\u7406\u6548\u7387\uff0c\u9069\u5408\u521d\u5b78\u8005\u8207\u958b\u767c\u8005\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-125495","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/125495","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=125495"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/125495\/revisions"}],"predecessor-version":[{"id":125496,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/125495\/revisions\/125496"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=125495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=125495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=125495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}