{"id":184696,"date":"2024-11-10T15:22:18","date_gmt":"2024-11-10T07:22:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184696\/"},"modified":"2024-11-10T15:22:19","modified_gmt":"2024-11-10T07:22:19","slug":"%e4%bd%bf%e7%94%a8redis%e9%9d%88%e6%b4%bb%e5%ad%98%e5%84%b2json%e6%95%b8%e6%93%9a%ef%bc%88redis%e7%94%a8json%e5%84%b2%e5%ad%98%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184696\/","title":{"rendered":"\u4f7f\u7528Redis\u9748\u6d3b\u5b58\u5132JSON\u6578\u64da\uff08redis\u7528json\u5132\u5b58\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e9%9d%88%e6%b4%bb%e5%ad%98%e5%84%b2json%e6%95%b8%e6%93%9a-KTmxMfIZpe\">\u4f7f\u7528Redis\u9748\u6d3b\u5b58\u5132JSON\u6578\u64da<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0cJSON\uff08JavaScript Object Notation\uff09\u5df2\u6210\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u6578\u64da\u4ea4\u63db\u683c\u5f0f\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\u4e0d\u65b7\u589e\u9577\uff0c\u958b\u767c\u8005\u9700\u8981\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u65b9\u5f0f\u4f86\u5b58\u5132\u548c\u6aa2\u7d22\u9019\u4e9b\u6578\u64da\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u7d50\u69cb\u4f86\u652f\u6301\u4e0d\u540c\u7684\u61c9\u7528\u5834\u666f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Redis\u9748\u6d3b\u5730\u5b58\u5132JSON\u6578\u64da\u3002<\/p>\n<h2 id=\"redis%e7%b0%a1%e4%bb%8b-KTmxMfIZpe\">Redis\u7c21\u4ecb<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u5408\u7b49\u6578\u64da\u985e\u578b\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff0cRedis\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u5206\u6790\u7b49\u5834\u666f\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%e5%ad%98%e5%84%b2json%e6%95%b8%e6%93%9a-KTmxMfIZpe\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u5b58\u5132JSON\u6578\u64da<\/h2>\n<p>\u4f7f\u7528Redis\u5b58\u5132JSON\u6578\u64da\u6709\u5e7e\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9069\u5408\u9700\u8981\u9ad8\u983b\u8a2a\u554f\u7684\u61c9\u7528\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u5b58\u5132\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis\u7684\u547d\u4ee4\u7c21\u55ae\u660e\u4e86\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u4e0a\u624b\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8redis%e4%b8%ad%e5%ad%98%e5%84%b2json%e6%95%b8%e6%93%9a-KTmxMfIZpe\">\u5982\u4f55\u5728Redis\u4e2d\u5b58\u5132JSON\u6578\u64da<\/h2>\n<p>\u5728Redis\u4e2d\u5b58\u5132JSON\u6578\u64da\u7684\u5e38\u898b\u65b9\u6cd5\u662f\u5c07JSON\u5b57\u7b26\u4e32\u76f4\u63a5\u5b58\u5132\u70baRedis\u7684\u5b57\u7b26\u4e32\u985e\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dredis-KTmxMfIZpe\">\u5b89\u88ddRedis<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86Redis\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5728Linux\u7cfb\u7d71\u4e0a\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt-get update\nsudo apt-get install redis-server<\/code><\/pre>\n<h3 id=\"%e5%ad%98%e5%84%b2json%e6%95%b8%e6%93%9a-KTmxMfIZpe\">\u5b58\u5132JSON\u6578\u64da<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500bJSON\u5c0d\u8c61\uff0c\u8868\u793a\u4e00\u500b\u7528\u6236\u7684\u8cc7\u6599\uff1a<\/p>\n<pre><code>{\n    \"id\": 1,\n    \"name\": \"John Doe\",\n    \"email\": \"john@example.com\"\n}<\/code><\/pre>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528Redis\u7684SET\u547d\u4ee4\u5c07\u5176\u5b58\u5132\u70ba\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>SET user:1 '{\"id\": 1, \"name\": \"John Doe\", \"email\": \"john@example.com\"}'<\/code><\/pre>\n<h3 id=\"%e6%aa%a2%e7%b4%a2json%e6%95%b8%e6%93%9a-KTmxMfIZpe\">\u6aa2\u7d22JSON\u6578\u64da<\/h3>\n<p>\u8981\u6aa2\u7d22\u5b58\u5132\u7684JSON\u6578\u64da\uff0c\u53ef\u4ee5\u4f7f\u7528GET\u547d\u4ee4\uff1a<\/p>\n<pre><code>GET user:1<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u5b58\u5132\u7684JSON\u5b57\u7b26\u4e32\u3002\u63a5\u4e0b\u4f86\uff0c\u4f60\u53ef\u4ee5\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u5c07\u5176\u89e3\u6790\u70ba\u5c0d\u8c61\u9032\u884c\u9032\u4e00\u6b65\u8655\u7406\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8redisjson%e6%a8%a1%e5%a1%8a-KTmxMfIZpe\">\u4f7f\u7528RedisJSON\u6a21\u584a<\/h3>\n<p>\u9664\u4e86\u76f4\u63a5\u5b58\u5132JSON\u5b57\u7b26\u4e32\u5916\uff0cRedis\u9084\u63d0\u4f9b\u4e86\u4e00\u500b\u540d\u70baRedisJSON\u7684\u6a21\u584a\uff0c\u5c08\u9580\u7528\u65bc\u8655\u7406JSON\u6578\u64da\u3002\u9019\u500b\u6a21\u584a\u5141\u8a31\u4f60\u4ee5\u66f4\u7d50\u69cb\u5316\u7684\u65b9\u5f0f\u5b58\u5132\u548c\u67e5\u8a62JSON\u6578\u64da\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88ddRedisJSON\u6a21\u584a\u3002\u53ef\u4ee5\u901a\u904eDocker\u4f86\u5b89\u88dd\uff1a<\/p>\n<pre><code>docker run -p 6379:6379 redislabs\/rejson:latest<\/code><\/pre>\n<p>\u4f7f\u7528RedisJSON\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528JSON.SET\u547d\u4ee4\u4f86\u5b58\u5132JSON\u6578\u64da\uff1a<\/p>\n<pre><code>JSON.SET user:1 . '{\"id\": 1, \"name\": \"John Doe\", \"email\": \"john@example.com\"}'<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528JSON.GET\u547d\u4ee4\u4f86\u6aa2\u7d22\u6578\u64da\uff1a<\/p>\n<pre><code>JSON.GET user:1<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-KTmxMfIZpe\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528Redis\u5b58\u5132JSON\u6578\u64da\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u7121\u8ad6\u662f\u76f4\u63a5\u5b58\u5132\u5b57\u7b26\u4e32\u9084\u662f\u4f7f\u7528RedisJSON\u6a21\u584a\uff0c\u90fd\u80fd\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5e8f\u7684\u767c\u5c55\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5b58\u5132\u65b9\u5f0f\u5c07\u5c0d\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u7522\u751f\u91cd\u8981\u5f71\u97ff\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u904b\u884c\u4f60\u7684Redis\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u4f60\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Redis\u9748\u6d3b\u5b58\u5132JSON\u6578\u64da\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u7c21\u5316\u61c9\u7528\u958b\u767c\u904e\u7a0b\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-184696","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184696","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=184696"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184696\/revisions"}],"predecessor-version":[{"id":184697,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184696\/revisions\/184697"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}