{"id":10006025,"date":"2022-07-26T15:16:09","date_gmt":"2022-07-26T07:16:09","guid":{"rendered":"https:\/\/ityet.com\/?p=10006025"},"modified":"2022-07-26T15:16:09","modified_gmt":"2022-07-26T07:16:09","slug":"redis-5-%e7%a7%8d%e5%9f%ba%e6%9c%ac%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%ef%bc%88string%e3%80%81list%e3%80%81hash%e3%80%81set%e3%80%81sorted-set%ef%bc%89%e8%af%a6%e8%a7%a3-javaguide","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006025","title":{"rendered":"Redis 5 \u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\uff08String\u3001List\u3001Hash\u3001Set\u3001Sorted Set\uff09\u8be6\u89e3 | JavaGuide"},"content":{"rendered":"<blockquote>\n<p>\u9996\u53d1\u4e8e\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=iDNbI1tlFqnOnNxfVDHvLQ%3D%3D.Cid1lWX%2FHC7gQF8mmdOvp7CVGD%2BfZ0MqqypsVLNy3ZBBcgCSJ1wXgxKVYQKQX5GqjUOIuLxHEKX7GoeLkllJHeOy%2BSVwu%2Byz8c8gD%2Fm3yr4%3D\" rel=\"nofollow\">Redis 5 \u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\u8be6\u89e3 &#8211; JavaGuide<\/a><\/p>\n<p>\u76f8\u5173\u6587\u7ae0\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=J2HgQZmMMzYRgnbd%2B%2BR7Qg%3D%3D.qszRdFpfTJE52%2B0EXu2%2Fooamq0VXEcr6yJPipm87QKtKctRqE%2BCibahdNqscIFXld4eUGMUbama94E2xuY7faw%3D%3D\" rel=\"nofollow\">Redis\u5e38\u89c1\u9762\u8bd5\u9898\u603b\u7ed3(\u4e0a)<\/a> \u3002<\/p>\n<\/blockquote>\n<p>Redis 5 \u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\uff08String\u3001List\u3001Hash\u3001Set\u3001Sorted Set\uff09\u5728\u9762\u8bd5\u4e2d\u7ecf\u5e38\u4f1a\u88ab\u95ee\u5230\uff0c\u8fd9\u7bc7\u6587\u7ae0\u6211\u4eec\u4e00\u8d77\u6765\u56de\u987e\u6e29\u4e60\u4e00\u4e0b\u3002<\/p>\n<p>\u8fd8\u6709\u51e0\u79cd\u6bd4\u8f83\u7279\u6b8a\u7684\u6570\u636e\u7ed3\u6784\uff08HyperLogLogs\u3001Bitmap \u3001Geospatial\u3001Stream\uff09\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u6211\u4eec\u540e\u9762\u4e0b\u6b21\u518d\u804a\uff01<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224184\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0b\u9762\u662f\u6b63\u6587\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u5728 Redis \u5b98\u7f51\u4e0a\u627e\u5230 Redis \u6570\u636e\u7ed3\u6784\u975e\u5e38\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=DwFu57g%2FChStxeYATVBP%2Bg%3D%3D.YPmLMMXzMsZ4UFaGSjy4n9liURfViREai6Eqioe7m7yUrgsJNiAPpR%2FqDyQhDpr3QGZZZ4TRcMOrnCgDwfb0lQ%3D%3D\" rel=\"nofollow\" title=\"Redis Data Structures\">Redis Data Structures<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=jKuPbtXZIfX%2BaSCoJL%2FxSw%3D%3D.cKSPVjwQ9Q51DjZrkHwH9Q3sp5d104Nj3dhdstmUx6yta307NOkLhN9us2IrlXElgLse2x3k9icDjr4SFNLGtw%3D%3D\" rel=\"nofollow\" title=\"Redis Data types tutorial\">Redis Data types tutorial<\/a><\/li>\n<\/ul>\n<p>\u672a\u6765\u968f\u7740 Redis \u65b0\u7248\u672c\u7684\u53d1\u5e03\uff0c\u53ef\u80fd\u4f1a\u6709\u65b0\u7684\u6570\u636e\u7ed3\u6784\u51fa\u73b0\uff0c\u901a\u8fc7\u67e5\u9605 Redis \u5b98\u7f51\u5bf9\u5e94\u7684\u4ecb\u7ecd\uff0c\u4f60\u603b\u80fd\u83b7\u53d6\u5230\u6700\u9760\u8c31\u7684\u4fe1\u606f\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224185\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>String\uff08\u5b57\u7b26\u4e32\uff09<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>String \u662f Redis \u4e2d\u6700\u7b80\u5355\u540c\u65f6\u4e5f\u662f\u6700\u5e38\u7528\u7684\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>String \u662f\u4e00\u79cd\u4e8c\u8fdb\u5236\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u6bd4\u5982\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u56fe\u7247\uff08\u56fe\u7247\u7684 base64 \u7f16\u7801\u6216\u8005\u89e3\u7801\u6216\u8005\u56fe\u7247\u7684\u8def\u5f84\uff09\u3001\u5e8f\u5217\u5316\u540e\u7684\u5bf9\u8c61\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224186\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u867d\u7136 Redis \u662f\u7528 C \u8bed\u8a00\u5199\u7684\uff0c\u4f46\u662f Redis \u5e76\u6ca1\u6709\u4f7f\u7528 C \u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u800c\u662f\u81ea\u5df1\u6784\u5efa\u4e86\u4e00\u79cd <strong>\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32<\/strong>\uff08Simple Dynamic String\uff0c<strong>SDS<\/strong>\uff09\u3002\u76f8\u6bd4\u4e8e C \u7684\u539f\u751f\u5b57\u7b26\u4e32\uff0cRedis \u7684 SDS \u4e0d\u5149\u53ef\u4ee5\u4fdd\u5b58\u6587\u672c\u6570\u636e\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5e76\u4e14\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\u590d\u6742\u5ea6\u4e3a O(1)\uff08C \u5b57\u7b26\u4e32\u4e3a O(N)\uff09,\u9664\u6b64\u4e4b\u5916\uff0cRedis \u7684 SDS API \u662f\u5b89\u5168\u7684\uff0c\u4e0d\u4f1a\u9020\u6210\u7f13\u51b2\u533a\u6ea2\u51fa\u3002<\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u547d\u4ee4<\/th>\n<th>\u4ecb\u7ecd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>SET key value<\/td>\n<td>\u8bbe\u7f6e\u6307\u5b9a key \u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>SETNX key value<\/td>\n<td>\u53ea\u6709\u5728 key \u4e0d\u5b58\u5728\u65f6\u8bbe\u7f6e key \u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>GET key<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a key \u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>MSET key1 value1 key2 value2 \u2026<\/td>\n<td>\u8bbe\u7f6e\u4e00\u4e2a\u6216\u591a\u4e2a\u6307\u5b9a key \u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>MGET key1 key2 &#8230;<\/td>\n<td>\u83b7\u53d6\u4e00\u4e2a\u6216\u591a\u4e2a\u6307\u5b9a key \u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>STRLEN key<\/td>\n<td>\u8fd4\u56de key \u6240\u50a8\u5b58\u7684\u5b57\u7b26\u4e32\u503c\u7684\u957f\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>INCR key<\/td>\n<td>\u5c06 key \u4e2d\u50a8\u5b58\u7684\u6570\u5b57\u503c\u589e\u4e00<\/td>\n<\/tr>\n<tr>\n<td>DECR key<\/td>\n<td>\u5c06 key \u4e2d\u50a8\u5b58\u7684\u6570\u5b57\u503c\u51cf\u4e00<\/td>\n<\/tr>\n<tr>\n<td>EXISTS key<\/td>\n<td>\u5224\u65ad\u6307\u5b9a key \u662f\u5426\u5b58\u5728<\/td>\n<\/tr>\n<tr>\n<td>DEL key\uff08\u901a\u7528\uff09<\/td>\n<td>\u5220\u9664\u6307\u5b9a\u7684 key<\/td>\n<\/tr>\n<tr>\n<td>EXPIRE key seconds\uff08\u901a\u7528\uff09<\/td>\n<td>\u7ed9\u6307\u5b9a key \u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u66f4\u591a Redis String \u547d\u4ee4\u4ee5\u53ca\u8be6\u7ec6\u4f7f\u7528\u6307\u5357\uff0c\u8bf7\u67e5\u770b Redis \u5b98\u7f51\u5bf9\u5e94\u7684\u4ecb\u7ecd\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=L6%2BCMUn8Nc7iDhyJsN7Bog%3D%3D.KOqLxOBSiVqGneo7a2lChR0hWPKlXX3VQ3ZJubT5YylVkAiBN123U2Bn8SvsaBdP\" rel=\"nofollow\">https:\/\/redis.io\/commands\/?gr&#8230;<\/a> \u3002<\/p>\n<p><strong>\u57fa\u672c\u64cd\u4f5c<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; SET key value\nOK\n&gt; GET key\n&quot;value&quot;\n&gt; EXISTS key\n(integer) 1\n&gt; STRLEN key\n(integer) 5\n&gt; DEL key\n(integer) 1\n&gt; GET key\n(nil)<\/code><\/pre>\n<p><strong>\u6279\u91cf\u8bbe\u7f6e<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; MSET key1 value1 key2 value2\nOK\n&gt; MGET key1 key2 # \u6279\u91cf\u83b7\u53d6\u591a\u4e2a key \u5bf9\u5e94\u7684 value\n1) &quot;value1&quot;\n2) &quot;value2&quot;<\/code><\/pre>\n<p><strong>\u8ba1\u6570\u5668\uff08\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u4e3a\u6574\u6570\u7684\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528\uff09\uff1a<\/strong><\/p>\n<pre><code class=\"bash\">&gt; SET number 1\nOK\n&gt; INCR number # \u5c06 key \u4e2d\u50a8\u5b58\u7684\u6570\u5b57\u503c\u589e\u4e00\n(integer) 2\n&gt; GET number\n&quot;2&quot;\n&gt; DECR number # \u5c06 key \u4e2d\u50a8\u5b58\u7684\u6570\u5b57\u503c\u51cf\u4e00\n(integer) 1\n&gt; GET number\n&quot;1&quot;<\/code><\/pre>\n<p><strong>\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff08\u9ed8\u8ba4\u4e3a\u6c38\u4e0d\u8fc7\u671f\uff09<\/strong>\uff1a<\/p>\n<pre><code class=\"bash\">&gt; EXPIRE key 60\n(integer) 1\n&gt; SETNX key 60 value # \u8bbe\u7f6e\u503c\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\nOK\n&gt; TTL key\n(integer) 56<\/code><\/pre>\n<h3>\u5e94\u7528\u573a\u666f<\/h3>\n<p><strong>\u9700\u8981\u5b58\u50a8\u5e38\u89c4\u6570\u636e\u7684\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u4e3e\u4f8b \uff1a\u7f13\u5b58 session\u3001token\u3001\u56fe\u7247\u5730\u5740\u3001\u5e8f\u5217\u5316\u540e\u7684\u5bf9\u8c61(\u76f8\u6bd4\u8f83\u4e8e Hash \u5b58\u50a8\u66f4\u8282\u7701\u5185\u5b58)\u3002<\/li>\n<li>\u76f8\u5173\u547d\u4ee4 \uff1a <code>SET<\/code>\u3001<code>GET<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u9700\u8981\u8ba1\u6570\u7684\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u4e3e\u4f8b \uff1a\u7528\u6237\u5355\u4f4d\u65f6\u95f4\u7684\u8bf7\u6c42\u6570\uff08\u7b80\u5355\u9650\u6d41\u53ef\u4ee5\u7528\u5230\uff09\u3001\u9875\u9762\u5355\u4f4d\u65f6\u95f4\u7684\u8bbf\u95ee\u6570\u3002<\/li>\n<li>\u76f8\u5173\u547d\u4ee4 \uff1a<code>SET<\/code>\u3001<code>GET<\/code>\u3001 <code>INCR<\/code>\u3001<code>DECR<\/code> \u3002<\/li>\n<\/ul>\n<p><strong>\u5206\u5e03\u5f0f\u9501<\/strong><\/p>\n<p>\u5229\u7528 <code>SETNX key value<\/code> \u547d\u4ee4\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u6700\u7b80\u6613\u7684\u5206\u5e03\u5f0f\u9501\uff08\u5b58\u5728\u4e00\u4e9b\u7f3a\u9677\uff0c\u901a\u5e38\u4e0d\u5efa\u8bae\u8fd9\u6837\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\uff09\u3002<\/p>\n<h2>List\uff08\u5217\u8868\uff09<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>Redis \u4e2d\u7684 List \u5176\u5b9e\u5c31\u662f\u94fe\u8868\u6570\u636e\u7ed3\u6784\u7684\u5b9e\u73b0\u3002\u6211\u5728 <a href=\"https:\/\/link.segmentfault.com\/?enc=wC9YTR8MPjyYy36WAkOFsQ%3D%3D.4Qx00eMcxViavIrhhRpKodWK3rs0cUt5kTaZgpqgKd%2BlfnBD8p2MJPm6Jn3bxhDaWrQC2Mo7hLPVPd9IOnbspwh5qG%2B7CtQmfJ0lVfTZzb4%3D\" rel=\"nofollow\" title=\"\u7ebf\u6027\u6570\u636e\u7ed3\u6784 :\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\">\u7ebf\u6027\u6570\u636e\u7ed3\u6784 :\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217<\/a> \u8fd9\u7bc7\u6587\u7ae0\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u94fe\u8868\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u6211\u8fd9\u91cc\u5c31\u4e0d\u591a\u505a\u4ecb\u7ecd\u4e86\u3002<\/p>\n<p>\u8bb8\u591a\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\u90fd\u5185\u7f6e\u4e86\u94fe\u8868\u7684\u5b9e\u73b0\u6bd4\u5982 Java \u4e2d\u7684 <code>LinkedList<\/code>\uff0c\u4f46\u662f C \u8bed\u8a00\u5e76\u6ca1\u6709\u5b9e\u73b0\u94fe\u8868\uff0c\u6240\u4ee5 Redis \u5b9e\u73b0\u4e86\u81ea\u5df1\u7684\u94fe\u8868\u6570\u636e\u7ed3\u6784\u3002Redis \u7684 List \u7684\u5b9e\u73b0\u4e3a\u4e00\u4e2a <strong>\u53cc\u5411\u94fe\u8868<\/strong>\uff0c\u5373\u53ef\u4ee5\u652f\u6301\u53cd\u5411\u67e5\u627e\u548c\u904d\u5386\uff0c\u66f4\u65b9\u4fbf\u64cd\u4f5c\uff0c\u4e0d\u8fc7\u5e26\u6765\u4e86\u90e8\u5206\u989d\u5916\u7684\u5185\u5b58\u5f00\u9500\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224187\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u547d\u4ee4<\/th>\n<th>\u4ecb\u7ecd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>RPUSH key value1 value2 &#8230;<\/td>\n<td>\u5728\u6307\u5b9a\u5217\u8868\u7684\u5c3e\u90e8\uff08\u53f3\u8fb9\uff09\u6dfb\u52a0\u4e00\u4e2a\u6216\u591a\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>LPUSH key value1 value2 &#8230;<\/td>\n<td>\u5728\u6307\u5b9a\u5217\u8868\u7684\u5934\u90e8\uff08\u5de6\u8fb9\uff09\u6dfb\u52a0\u4e00\u4e2a\u6216\u591a\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>LSET key index value<\/td>\n<td>\u5c06\u6307\u5b9a\u5217\u8868\u7d22\u5f15 index \u4f4d\u7f6e\u7684\u503c\u8bbe\u7f6e\u4e3a value<\/td>\n<\/tr>\n<tr>\n<td>LPOP key<\/td>\n<td>\u79fb\u9664\u5e76\u83b7\u53d6\u6307\u5b9a\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20(\u6700\u5de6\u8fb9)<\/td>\n<\/tr>\n<tr>\n<td>RPOP key<\/td>\n<td>\u79fb\u9664\u5e76\u83b7\u53d6\u6307\u5b9a\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20(\u6700\u53f3\u8fb9)<\/td>\n<\/tr>\n<tr>\n<td>LLEN key<\/td>\n<td>\u83b7\u53d6\u5217\u8868\u5143\u7d20\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>LRANGE key start end<\/td>\n<td>\u83b7\u53d6\u5217\u8868 start \u548c end \u4e4b\u95f4 \u7684\u5143\u7d20<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u66f4\u591a Redis List \u547d\u4ee4\u4ee5\u53ca\u8be6\u7ec6\u4f7f\u7528\u6307\u5357\uff0c\u8bf7\u67e5\u770b Redis \u5b98\u7f51\u5bf9\u5e94\u7684\u4ecb\u7ecd\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=HbCz0%2F91%2Bng%2FwqtYXvDsGA%3D%3D.f0kIZcCeh%2FloXxHzXBZOZLtbIgOCtXHXg9YXKngU5pHz25xtT%2Fxx0bHFVqdhSp1%2F\" rel=\"nofollow\">https:\/\/redis.io\/commands\/?gr&#8230;<\/a> \u3002<\/p>\n<p><strong>\u901a\u8fc7 <code>RPUSH\/LPOP<\/code> \u6216\u8005 <code>LPUSH\/RPOP<\/code>\u5b9e\u73b0\u961f\u5217<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; RPUSH myList value1\n(integer) 1\n&gt; RPUSH myList value2 value3\n(integer) 3\n&gt; LPOP myList\n&quot;value1&quot;\n&gt; LRANGE myList 0 1\n1) &quot;value2&quot;\n2) &quot;value3&quot;\n&gt; LRANGE myList 0 -1\n1) &quot;value2&quot;\n2) &quot;value3&quot;<\/code><\/pre>\n<p><strong>\u901a\u8fc7 <code>RPUSH\/RPOP<\/code>\u6216\u8005<code>LPUSH\/LPOP<\/code> \u5b9e\u73b0\u6808<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; RPUSH myList2 value1 value2 value3\n(integer) 3\n&gt; RPOP myList2 # \u5c06 list\u7684\u5934\u90e8(\u6700\u53f3\u8fb9)\u5143\u7d20\u53d6\u51fa\n&quot;value3&quot;<\/code><\/pre>\n<p>\u6211\u4e13\u95e8\u753b\u4e86\u4e00\u4e2a\u56fe\u65b9\u4fbf\u5927\u5bb6\u7406\u89e3 <code>RPUSH<\/code> , <code>LPOP<\/code> , <code>lpush<\/code> , <code>RPOP<\/code> \u547d\u4ee4\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224188\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u901a\u8fc7 <code>LRANGE<\/code> \u67e5\u770b\u5bf9\u5e94\u4e0b\u6807\u8303\u56f4\u7684\u5217\u8868\u5143\u7d20<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; RPUSH myList value1 value2 value3\n(integer) 3\n&gt; LRANGE myList 0 1\n1) &quot;value1&quot;\n2) &quot;value2&quot;\n&gt; LRANGE myList 0 -1\n1) &quot;value1&quot;\n2) &quot;value2&quot;\n3) &quot;value3&quot;<\/code><\/pre>\n<p>\u901a\u8fc7 <code>LRANGE<\/code> \u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u57fa\u4e8e List \u5b9e\u73b0\u5206\u9875\u67e5\u8be2\uff0c\u6027\u80fd\u975e\u5e38\u9ad8\uff01<\/p>\n<p><strong>\u901a\u8fc7 <code>LLEN<\/code> \u67e5\u770b\u94fe\u8868\u957f\u5ea6<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; LLEN myList\n(integer) 3<\/code><\/pre>\n<h3>\u5e94\u7528\u573a\u666f<\/h3>\n<p><strong>\u4fe1\u606f\u6d41\u5c55\u793a<\/strong><\/p>\n<ul>\n<li>\u4e3e\u4f8b \uff1a\u6700\u65b0\u6587\u7ae0\u3001\u6700\u65b0\u52a8\u6001\u3002<\/li>\n<li>\u76f8\u5173\u547d\u4ee4 \uff1a <code>LPUSH<\/code>\u3001<code>LRANGE<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u6d88\u606f\u961f\u5217<\/strong><\/p>\n<p>Redis List \u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u7528\u6765\u505a\u6d88\u606f\u961f\u5217\uff0c\u53ea\u662f\u529f\u80fd\u8fc7\u4e8e\u7b80\u5355\u4e14\u5b58\u5728\u5f88\u591a\u7f3a\u9677\uff0c\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a\u3002<\/p>\n<p>\u76f8\u5bf9\u6765\u8bf4\uff0cRedis 5.0 \u65b0\u589e\u52a0\u7684\u4e00\u4e2a\u6570\u636e\u7ed3\u6784 <code>Stream<\/code> \u66f4\u9002\u5408\u505a\u6d88\u606f\u961f\u5217\u4e00\u4e9b\uff0c\u53ea\u662f\u529f\u80fd\u4f9d\u7136\u975e\u5e38\u7b80\u964b\u3002\u548c\u4e13\u4e1a\u7684\u6d88\u606f\u961f\u5217\u76f8\u6bd4\uff0c\u8fd8\u662f\u6709\u5f88\u591a\u6b20\u7f3a\u7684\u5730\u65b9\u6bd4\u5982\u6d88\u606f\u4e22\u5931\u548c\u5806\u79ef\u95ee\u9898\u4e0d\u597d\u89e3\u51b3\u3002<\/p>\n<h2>Hash\uff08\u54c8\u5e0c\uff09<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>Redis \u4e2d\u7684 Hash \u662f\u4e00\u4e2a String \u7c7b\u578b\u7684 field-value\uff08\u952e\u503c\u5bf9\uff09 \u7684\u6620\u5c04\u8868\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u5b58\u50a8\u5bf9\u8c61\uff0c\u540e\u7eed\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u8fd9\u4e2a\u5bf9\u8c61\u4e2d\u7684\u67d0\u4e9b\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n<p>Hash \u7c7b\u4f3c\u4e8e JDK1.8 \u524d\u7684 <code>HashMap<\/code>\uff0c\u5185\u90e8\u5b9e\u73b0\u4e5f\u5dee\u4e0d\u591a(\u6570\u7ec4 + \u94fe\u8868)\u3002\u4e0d\u8fc7\uff0cRedis \u7684 Hash \u505a\u4e86\u66f4\u591a\u4f18\u5316\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224189\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u547d\u4ee4<\/th>\n<th>\u4ecb\u7ecd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>HSET key field value<\/td>\n<td>\u8bbe\u7f6e\u6307\u5b9a\u54c8\u5e0c\u8868\u4e2d\u6307\u5b9a\u5b57\u6bb5\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>HSETNX key field value<\/td>\n<td>\u53ea\u6709\u6307\u5b9a\u5b57\u6bb5\u4e0d\u5b58\u5728\u65f6\u8bbe\u7f6e\u6307\u5b9a\u5b57\u6bb5\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>HMSET key field1 value1 field2 value2 &#8230;<\/td>\n<td>\u540c\u65f6\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a field-value (\u57df-\u503c)\u5bf9\u8bbe\u7f6e\u5230\u6307\u5b9a\u54c8\u5e0c\u8868\u4e2d<\/td>\n<\/tr>\n<tr>\n<td>HGET key field<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u54c8\u5e0c\u8868\u4e2d\u6307\u5b9a\u5b57\u6bb5\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>HMGET key field1 field2 &#8230;<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u54c8\u5e0c\u8868\u4e2d\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u6307\u5b9a\u5b57\u6bb5\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>HGETALL key<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u54c8\u5e0c\u8868\u4e2d\u6240\u6709\u7684\u952e\u503c\u5bf9<\/td>\n<\/tr>\n<tr>\n<td>HEXISTS key field<\/td>\n<td>\u67e5\u770b\u6307\u5b9a\u54c8\u5e0c\u8868\u4e2d\u6307\u5b9a\u7684\u5b57\u6bb5\u662f\u5426\u5b58\u5728<\/td>\n<\/tr>\n<tr>\n<td>HDEL key field1 field2 &#8230;<\/td>\n<td>\u5220\u9664\u4e00\u4e2a\u6216\u591a\u4e2a\u54c8\u5e0c\u8868\u5b57\u6bb5<\/td>\n<\/tr>\n<tr>\n<td>HLEN key<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u54c8\u5e0c\u8868\u4e2d\u5b57\u6bb5\u7684\u6570\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u66f4\u591a Redis Hash \u547d\u4ee4\u4ee5\u53ca\u8be6\u7ec6\u4f7f\u7528\u6307\u5357\uff0c\u8bf7\u67e5\u770b Redis \u5b98\u7f51\u5bf9\u5e94\u7684\u4ecb\u7ecd\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=nMuzNHXswnsOtUQXKspy8w%3D%3D.raBEM%2Bg%2FT4CfJONI38OUfJoMOhzhTROBRd7wtFKY%2BIQBu0ptZD484QOjKspih%2BkO\" rel=\"nofollow\">https:\/\/redis.io\/commands\/?gr&#8230;<\/a> \u3002<\/p>\n<p><strong>\u6a21\u62df\u5bf9\u8c61\u6570\u636e\u5b58\u50a8<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; HMSET userInfoKey name &quot;guide&quot; description &quot;dev&quot; age &quot;24&quot;\nOK\n&gt; HEXISTS userInfoKey name # \u67e5\u770b key \u5bf9\u5e94\u7684 value\u4e2d\u6307\u5b9a\u7684\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002\n(integer) 1\n&gt; HGET userInfoKey name # \u83b7\u53d6\u5b58\u50a8\u5728\u54c8\u5e0c\u8868\u4e2d\u6307\u5b9a\u5b57\u6bb5\u7684\u503c\u3002\n&quot;guide&quot;\n&gt; HGET userInfoKey age\n&quot;24&quot;\n&gt; HGETALL userInfoKey # \u83b7\u53d6\u5728\u54c8\u5e0c\u8868\u4e2d\u6307\u5b9a key \u7684\u6240\u6709\u5b57\u6bb5\u548c\u503c\n1) &quot;name&quot;\n2) &quot;guide&quot;\n3) &quot;description&quot;\n4) &quot;dev&quot;\n5) &quot;age&quot;\n6) &quot;24&quot;\n&gt; HSET userInfoKey name &quot;GuideGeGe&quot;\n&gt; HGET userInfoKey name\n&quot;GuideGeGe&quot;<\/code><\/pre>\n<h3>\u5e94\u7528\u573a\u666f<\/h3>\n<p><strong>\u5bf9\u8c61\u6570\u636e\u5b58\u50a8\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u4e3e\u4f8b \uff1a\u7528\u6237\u4fe1\u606f\u3001\u5546\u54c1\u4fe1\u606f\u3001\u6587\u7ae0\u4fe1\u606f\u3001\u8d2d\u7269\u8f66\u4fe1\u606f\u3002<\/li>\n<li>\u76f8\u5173\u547d\u4ee4 \uff1a<code>HSET<\/code> \uff08\u8bbe\u7f6e\u5355\u4e2a\u5b57\u6bb5\u7684\u503c\uff09\u3001<code>HMSET<\/code>\uff08\u8bbe\u7f6e\u591a\u4e2a\u5b57\u6bb5\u7684\u503c\uff09\u3001<code>HGET<\/code>\uff08\u83b7\u53d6\u5355\u4e2a\u5b57\u6bb5\u7684\u503c\uff09\u3001<code>HMGET<\/code>\uff08\u83b7\u53d6\u591a\u4e2a\u5b57\u6bb5\u7684\u503c\uff09\u3002<\/li>\n<\/ul>\n<h2>Set\uff08\u96c6\u5408\uff09<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>Redis \u4e2d\u7684 Set \u7c7b\u578b\u662f\u4e00\u79cd\u65e0\u5e8f\u96c6\u5408\uff0c\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6ca1\u6709\u5148\u540e\u987a\u5e8f\u4f46\u90fd\u552f\u4e00\uff0c\u6709\u70b9\u7c7b\u4f3c\u4e8e Java \u4e2d\u7684 <code>HashSet<\/code> \u3002\u5f53\u4f60\u9700\u8981\u5b58\u50a8\u4e00\u4e2a\u5217\u8868\u6570\u636e\uff0c\u53c8\u4e0d\u5e0c\u671b\u51fa\u73b0\u91cd\u590d\u6570\u636e\u65f6\uff0cSet \u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c\u5e76\u4e14 Set \u63d0\u4f9b\u4e86\u5224\u65ad\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5728\u4e00\u4e2a Set \u96c6\u5408\u5185\u7684\u91cd\u8981\u63a5\u53e3\uff0c\u8fd9\u4e2a\u4e5f\u662f List \u6240\u4e0d\u80fd\u63d0\u4f9b\u7684\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u57fa\u4e8e Set \u8f7b\u6613\u5b9e\u73b0\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e2a\u7528\u6237\u6240\u6709\u7684\u5173\u6ce8\u4eba\u5b58\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\uff0c\u5c06\u5176\u6240\u6709\u7c89\u4e1d\u5b58\u5728\u4e00\u4e2a\u96c6\u5408\u3002\u8fd9\u6837\u7684\u8bdd\uff0cSet \u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u7684\u5b9e\u73b0\u5982\u5171\u540c\u5173\u6ce8\u3001\u5171\u540c\u7c89\u4e1d\u3001\u5171\u540c\u559c\u597d\u7b49\u529f\u80fd\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u4e5f\u5c31\u662f\u6c42\u4ea4\u96c6\u7684\u8fc7\u7a0b\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224190\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u547d\u4ee4<\/th>\n<th>\u4ecb\u7ecd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>SADD key member1 member2 &#8230;<\/td>\n<td>\u5411\u6307\u5b9a\u96c6\u5408\u6dfb\u52a0\u4e00\u4e2a\u6216\u591a\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>SMEMBERS key<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u96c6\u5408\u4e2d\u7684\u6240\u6709\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>SCARD key<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u96c6\u5408\u7684\u5143\u7d20\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>SISMEMBER key member<\/td>\n<td>\u5224\u65ad\u6307\u5b9a\u5143\u7d20\u662f\u5426\u5728\u6307\u5b9a\u96c6\u5408\u4e2d<\/td>\n<\/tr>\n<tr>\n<td>SINTER key1 key2 &#8230;<\/td>\n<td>\u83b7\u53d6\u7ed9\u5b9a\u6240\u6709\u96c6\u5408\u7684\u4ea4\u96c6<\/td>\n<\/tr>\n<tr>\n<td>SINTERSTORE destination key1 key2 &#8230;<\/td>\n<td>\u5c06\u7ed9\u5b9a\u6240\u6709\u96c6\u5408\u7684\u4ea4\u96c6\u5b58\u50a8\u5728 destination \u4e2d<\/td>\n<\/tr>\n<tr>\n<td>SUNION key1 key2 &#8230;<\/td>\n<td>\u83b7\u53d6\u7ed9\u5b9a\u6240\u6709\u96c6\u5408\u7684\u5e76\u96c6<\/td>\n<\/tr>\n<tr>\n<td>SUNIONSTORE destination key1 key2 &#8230;<\/td>\n<td>\u5c06\u7ed9\u5b9a\u6240\u6709\u96c6\u5408\u7684\u5e76\u96c6\u5b58\u50a8\u5728 destination \u4e2d<\/td>\n<\/tr>\n<tr>\n<td>SDIFF key1 key2 &#8230;<\/td>\n<td>\u83b7\u53d6\u7ed9\u5b9a\u6240\u6709\u96c6\u5408\u7684\u5dee\u96c6<\/td>\n<\/tr>\n<tr>\n<td>SDIFFSTORE destination key1 key2 &#8230;<\/td>\n<td>\u5c06\u7ed9\u5b9a\u6240\u6709\u96c6\u5408\u7684\u5dee\u96c6\u5b58\u50a8\u5728 destination \u4e2d<\/td>\n<\/tr>\n<tr>\n<td>SPOP key count<\/td>\n<td>\u968f\u673a\u79fb\u9664\u5e76\u83b7\u53d6\u6307\u5b9a\u96c6\u5408\u4e2d\u4e00\u4e2a\u6216\u591a\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>SRANDMEMBER key count<\/td>\n<td>\u968f\u673a\u83b7\u53d6\u6307\u5b9a\u96c6\u5408\u4e2d\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u66f4\u591a Redis Set \u547d\u4ee4\u4ee5\u53ca\u8be6\u7ec6\u4f7f\u7528\u6307\u5357\uff0c\u8bf7\u67e5\u770b Redis \u5b98\u7f51\u5bf9\u5e94\u7684\u4ecb\u7ecd\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=hc7%2F00JXn%2BvwFRb73TxcSQ%3D%3D.GbR5Ni7F1fqDvopDbWlMyibzvNIPgyGCta6JjdKj7qOPwRXt4nQNvL8n9Kul%2FrOG\" rel=\"nofollow\">https:\/\/redis.io\/commands\/?gr&#8230;<\/a> \u3002<\/p>\n<p><strong>\u57fa\u672c\u64cd\u4f5c<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; SADD mySet value1 value2\n(integer) 2\n&gt; SADD mySet value1 # \u4e0d\u5141\u8bb8\u6709\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u6dfb\u52a0\u5931\u8d25\n(integer) 0\n&gt; SMEMBERS mySet\n1) &quot;value1&quot;\n2) &quot;value2&quot;\n&gt; SCARD mySet\n(integer) 2\n&gt; SISMEMBER mySet value1\n(integer) 1\n&gt; SADD mySet2 value2 value3\n(integer) 2<\/code><\/pre>\n<ul>\n<li><code>mySet<\/code> : <code>value1<\/code>\u3001<code>value2<\/code> \u3002<\/li>\n<li><code>mySet2<\/code> \uff1a <code>value2<\/code> \u3001<code>value3<\/code> \u3002<\/li>\n<\/ul>\n<p><strong>\u6c42\u4ea4\u96c6<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; SINTERSTORE mySet3 mySet mySet2\n(integer) 1\n&gt; SMEMBERS mySet3\n1) &quot;value2&quot;<\/code><\/pre>\n<p><strong>\u6c42\u5e76\u96c6<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; SUNION mySet mySet2\n1) &quot;value3&quot;\n2) &quot;value2&quot;\n3) &quot;value1&quot;<\/code><\/pre>\n<p><strong>\u6c42\u5dee\u96c6<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; SDIFF mySet mySet2 # \u5dee\u96c6\u662f\u7531\u6240\u6709\u5c5e\u4e8e mySet \u4f46\u4e0d\u5c5e\u4e8e A \u7684\u5143\u7d20\u7ec4\u6210\u7684\u96c6\u5408\n1) &quot;value1&quot;<\/code><\/pre>\n<h3>\u5e94\u7528\u573a\u666f<\/h3>\n<p><strong>\u9700\u8981\u5b58\u653e\u7684\u6570\u636e\u4e0d\u80fd\u91cd\u590d\u7684\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u4e3e\u4f8b\uff1a\u7f51\u7ad9 UV \u7edf\u8ba1\uff08\u6570\u636e\u91cf\u5de8\u5927\u7684\u573a\u666f\u8fd8\u662f <code>HyperLogLog<\/code>\u66f4\u9002\u5408\u4e00\u4e9b\uff09\u3001\u6587\u7ae0\u70b9\u8d5e\u3001\u52a8\u6001\u70b9\u8d5e\u7b49\u573a\u666f\u3002<\/li>\n<li>\u76f8\u5173\u547d\u4ee4\uff1a<code>SCARD<\/code>\uff08\u83b7\u53d6\u96c6\u5408\u6570\u91cf\uff09 \u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224191\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u9700\u8981\u83b7\u53d6\u591a\u4e2a\u6570\u636e\u6e90\u4ea4\u96c6\u3001\u5e76\u96c6\u548c\u5dee\u96c6\u7684\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u4e3e\u4f8b \uff1a\u5171\u540c\u597d\u53cb(\u4ea4\u96c6)\u3001\u5171\u540c\u7c89\u4e1d(\u4ea4\u96c6)\u3001\u5171\u540c\u5173\u6ce8(\u4ea4\u96c6)\u3001\u597d\u53cb\u63a8\u8350\uff08\u5dee\u96c6\uff09\u3001\u97f3\u4e50\u63a8\u8350\uff08\u5dee\u96c6\uff09 \u3001\u8ba2\u9605\u53f7\u63a8\u8350\uff08\u5dee\u96c6+\u4ea4\u96c6\uff09 \u7b49\u573a\u666f\u3002<\/li>\n<li>\u76f8\u5173\u547d\u4ee4\uff1a<code>SINTER<\/code>\uff08\u4ea4\u96c6\uff09\u3001<code>SINTERSTORE<\/code> \uff08\u4ea4\u96c6\uff09\u3001<code>SUNION<\/code> \uff08\u5e76\u96c6\uff09\u3001<code>SUNIONSTORE<\/code>\uff08\u5e76\u96c6\uff09\u3001<code>SDIFF<\/code>\uff08\u4ea4\u96c6\uff09\u3001<code>SDIFFSTORE<\/code> \uff08\u4ea4\u96c6\uff09\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224192\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u9700\u8981\u968f\u673a\u83b7\u53d6\u6570\u636e\u6e90\u4e2d\u7684\u5143\u7d20\u7684\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u4e3e\u4f8b \uff1a\u62bd\u5956\u7cfb\u7edf\u3001\u968f\u673a\u3002<\/li>\n<li>\u76f8\u5173\u547d\u4ee4\uff1a<code>SPOP<\/code>\uff08\u968f\u673a\u83b7\u53d6\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u5e76\u79fb\u9664\uff0c\u9002\u5408\u4e0d\u5141\u8bb8\u91cd\u590d\u4e2d\u5956\u7684\u573a\u666f\uff09\u3001<code>SRANDMEMBER<\/code>\uff08\u968f\u673a\u83b7\u53d6\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u9002\u5408\u5141\u8bb8\u91cd\u590d\u4e2d\u5956\u7684\u573a\u666f\uff09\u3002<\/li>\n<\/ul>\n<h2>Sorted Set\uff08\u6709\u5e8f\u96c6\u5408\uff09<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>Sorted Set \u7c7b\u4f3c\u4e8e Set\uff0c\u4f46\u548c Set \u76f8\u6bd4\uff0cSorted Set \u589e\u52a0\u4e86\u4e00\u4e2a\u6743\u91cd\u53c2\u6570 <code>score<\/code>\uff0c\u4f7f\u5f97\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u80fd\u591f\u6309 <code>score<\/code> \u8fdb\u884c\u6709\u5e8f\u6392\u5217\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7 <code>score<\/code> \u7684\u8303\u56f4\u6765\u83b7\u53d6\u5143\u7d20\u7684\u5217\u8868\u3002\u6709\u70b9\u50cf\u662f Java \u4e2d <code>HashMap<\/code> \u548c <code>TreeSet<\/code> \u7684\u7ed3\u5408\u4f53\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224193\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u547d\u4ee4<\/th>\n<th>\u4ecb\u7ecd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ZADD key score1 member1 score2 member2 &#8230;<\/td>\n<td>\u5411\u6307\u5b9a\u6709\u5e8f\u96c6\u5408\u6dfb\u52a0\u4e00\u4e2a\u6216\u591a\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>ZCARD KEY<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u6709\u5e8f\u96c6\u5408\u7684\u5143\u7d20\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>ZSCORE key member<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u6709\u5e8f\u96c6\u5408\u4e2d\u6307\u5b9a\u5143\u7d20\u7684 score \u503c<\/td>\n<\/tr>\n<tr>\n<td>ZINTERSTORE destination numkeys key1 key2 &#8230;<\/td>\n<td>\u5c06\u7ed9\u5b9a\u6240\u6709\u6709\u5e8f\u96c6\u5408\u7684\u4ea4\u96c6\u5b58\u50a8\u5728 destination \u4e2d\uff0c\u5bf9\u76f8\u540c\u5143\u7d20\u5bf9\u5e94\u7684 score \u503c\u8fdb\u884c SUM \u805a\u5408\u64cd\u4f5c\uff0cnumkeys \u4e3a\u96c6\u5408\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>ZUNIONSTORE destination numkeys key1 key2 &#8230;<\/td>\n<td>\u6c42\u5e76\u96c6\uff0c\u5176\u5b83\u548c ZINTERSTORE \u7c7b\u4f3c<\/td>\n<\/tr>\n<tr>\n<td>ZDIFF destination numkeys key1 key2 &#8230;<\/td>\n<td>\u6c42\u5dee\u96c6\uff0c\u5176\u5b83\u548c ZINTERSTORE \u7c7b\u4f3c<\/td>\n<\/tr>\n<tr>\n<td>ZRANGE key start end<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u6709\u5e8f\u96c6\u5408 start \u548c end \u4e4b\u95f4\u7684\u5143\u7d20\uff08score \u4ece\u4f4e\u5230\u9ad8\uff09<\/td>\n<\/tr>\n<tr>\n<td>ZREVRANGE key start end<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u6709\u5e8f\u96c6\u5408 start \u548c end \u4e4b\u95f4\u7684\u5143\u7d20\uff08score \u4ece\u9ad8\u5230\u5e95\uff09<\/td>\n<\/tr>\n<tr>\n<td>ZREVRANK key member<\/td>\n<td>\u83b7\u53d6\u6307\u5b9a\u6709\u5e8f\u96c6\u5408\u4e2d\u6307\u5b9a\u5143\u7d20\u7684\u6392\u540d(score \u4ece\u5927\u5230\u5c0f\u6392\u5e8f)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u66f4\u591a Redis Sorted Set \u547d\u4ee4\u4ee5\u53ca\u8be6\u7ec6\u4f7f\u7528\u6307\u5357\uff0c\u8bf7\u67e5\u770b Redis \u5b98\u7f51\u5bf9\u5e94\u7684\u4ecb\u7ecd\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=uO7GFOblk65BWcsI1R4C9w%3D%3D.oTWoCf%2BaS3Ml%2BbUsCAZkH%2B%2B6ZprR3Ce8T3Eb16XIHEBf1EsO7kJRpEZ6%2BZgADpSK\" rel=\"nofollow\">https:\/\/redis.io\/commands\/?gr&#8230;<\/a> \u3002<\/p>\n<p><strong>\u57fa\u672c\u64cd\u4f5c<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; ZADD myZset 2.0 value1 1.0 value2\n(integer) 2\n&gt; ZCARD myZset\n2\n&gt; ZSCORE myZset value1\n2.0\n&gt; ZRANGE myZset 0 1\n1) &quot;value2&quot;\n2) &quot;value1&quot;\n&gt; ZREVRANGE myZset 0 1\n1) &quot;value1&quot;\n2) &quot;value2&quot;\n&gt; ZADD myZset2 4.0 value2 3.0 value3\n(integer) 2\n<\/code><\/pre>\n<ul>\n<li><code>myZset<\/code> : <code>value1<\/code>(2.0)\u3001<code>value2<\/code>(1.0) \u3002<\/li>\n<li><code>myZset2<\/code> \uff1a <code>value2<\/code> \uff084.0\uff09\u3001<code>value3<\/code>(3.0) \u3002<\/li>\n<\/ul>\n<p><strong>\u83b7\u53d6\u6307\u5b9a\u5143\u7d20\u7684\u6392\u540d<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; ZREVRANK myZset value1\n0\n&gt; ZREVRANK myZset value2\n1<\/code><\/pre>\n<p><strong>\u6c42\u4ea4\u96c6<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; ZINTERSTORE myZset3 2 myZset myZset2\n1\n&gt; ZRANGE myZset3 0 1 WITHSCORES\nvalue2\n5<\/code><\/pre>\n<p><strong>\u6c42\u5e76\u96c6<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; ZUNIONSTORE myZset4 2 myZset myZset2\n3\n&gt; ZRANGE myZset4 0 2 WITHSCORES\nvalue1\n2\nvalue3\n3\nvalue2\n5<\/code><\/pre>\n<p><strong>\u6c42\u5dee\u96c6<\/strong> \uff1a<\/p>\n<pre><code class=\"bash\">&gt; ZDIFF 2 myZset myZset2 WITHSCORES\nvalue1\n2<\/code><\/pre>\n<h3>\u5e94\u7528\u573a\u666f<\/h3>\n<p><strong>\u9700\u8981\u968f\u673a\u83b7\u53d6\u6570\u636e\u6e90\u4e2d\u7684\u5143\u7d20\u6839\u636e\u67d0\u4e2a\u6743\u91cd\u8fdb\u884c\u6392\u5e8f\u7684\u573a\u666f<\/strong><\/p>\n<ul>\n<li>\u4e3e\u4f8b \uff1a\u5404\u79cd\u6392\u884c\u699c\u6bd4\u5982\u76f4\u64ad\u95f4\u9001\u793c\u7269\u7684\u6392\u884c\u699c\u3001\u670b\u53cb\u5708\u7684\u5fae\u4fe1\u6b65\u6570\u6392\u884c\u699c\u3001\u738b\u8005\u8363\u8000\u4e2d\u7684\u6bb5\u4f4d\u6392\u884c\u699c\u3001\u8bdd\u9898\u70ed\u5ea6\u6392\u884c\u699c\u7b49\u7b49\u3002<\/li>\n<li>\u76f8\u5173\u547d\u4ee4 \uff1a<code>ZRANGE<\/code> (\u4ece\u5c0f\u5230\u5927\u6392\u5e8f) \u3001 <code>ZREVRANGE<\/code> \uff08\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\uff09\u3001<code>ZREVRANK<\/code> (\u6307\u5b9a\u5143\u7d20\u6392\u540d)\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224194\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=%2Ftpd0kP6Ah8S0%2BmbWk0VlA%3D%3D.t6pzGHSy%2BnVMsXXRXcsc%2FkYLT0NdkjTtWRYsQpyEUdneCGCvozKufiEqc0x4QtJQYcYWG48z%2BQqY%2BUumZFDSHcQJrDm6%2BaioEp6PZZnQMEJQ%2FTLzv42ihI8VIbOOmnQFWalcaI3no5EzC%2Fc0XdBTQJuosygd3isUCrsJ0AG5EJ8fbd%2FlIi%2FfJ%2BC1Yxmbhp%2BQYoUzfTEYqM4OIUMEBJ8OIvWI%2FspWu81aSdi5iw9JBINM4BCM384ri1JmRG3oTEgDDPULq%2BFRfM7aIrrgrzCdG1%2FBqzprfLzQkKgs2QGuSco%3D\" rel=\"nofollow\">\u300aJava \u9762\u8bd5\u6307\u5317\u300b<\/a> \u7684\u300c\u6280\u672f\u9762\u8bd5\u9898\u7bc7\u300d\u5c31\u6709\u4e00\u7bc7\u6587\u7ae0\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Sorted Set \u6765\u8bbe\u8ba1\u5236\u4f5c\u4e00\u4e2a\u6392\u884c\u699c\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042224195\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u9700\u8981\u5b58\u50a8\u7684\u6570\u636e\u6709\u4f18\u5148\u7ea7\u6216\u8005\u91cd\u8981\u7a0b\u5ea6\u7684\u573a\u666f<\/strong> \u6bd4\u5982\u4f18\u5148\u7ea7\u4efb\u52a1\u961f\u5217\u3002<\/p>\n<ul>\n<li>\u4e3e\u4f8b \uff1a\u4f18\u5148\u7ea7\u4efb\u52a1\u961f\u5217\u3002<\/li>\n<li>\u76f8\u5173\u547d\u4ee4 \uff1a<code>ZRANGE<\/code> (\u4ece\u5c0f\u5230\u5927\u6392\u5e8f) \u3001 <code>ZREVRANGE<\/code> \uff08\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\uff09\u3001<code>ZREVRANK<\/code> (\u6307\u5b9a\u5143\u7d20\u6392\u540d)\u3002<\/li>\n<\/ul>\n<h2>\u53c2\u8003<\/h2>\n<ul>\n<li>Redis Data Structures \uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=WBtk%2FEgVoRnaw%2B7zG64VrQ%3D%3D.HlMsGSyvuh2jOrKTNwSIzJKLwSHcFT9kO%2FTziTsmwB41kS%2FHiyVo%2F5GbXaKH9jToopAZ0hSVNOyEl8Dpw8eDXA%3D%3D\" rel=\"nofollow\">https:\/\/redis.com\/redis-enter&#8230;<\/a> \u3002<\/li>\n<li>Redis Commands \uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=GNis80RPCIalFpvRTwJONQ%3D%3D.qrNpbw5XMj6LhsInu62%2F8%2FRNUS8XNy6nIzxyNlvUrGM%3D\" rel=\"nofollow\">https:\/\/redis.io\/commands\/<\/a> \u3002<\/li>\n<li>Redis Data types tutorial\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=zTOCmhVQq73Ymn2cC3NuPw%3D%3D.ZqbAq0b5mmGBublp0GOoStVuwONUcCF5jB05C4XkvJQ%2BOXX%2F6WZa4XIZIl3nkxpjHPkkXw7be5SPEWFEmXbq%2Fg%3D%3D\" rel=\"nofollow\">https:\/\/redis.io\/docs\/manual\/&#8230;<\/a> \u3002<\/li>\n<li>Redis \u5b58\u50a8\u5bf9\u8c61\u4fe1\u606f\u662f\u7528 Hash \u8fd8\u662f String : <a href=\"https:\/\/segmentfault.com\/a\/1190000040032006\">https:\/\/segmentfault.com\/a\/11&#8230;<\/a><\/li>\n<\/ul>\n<h2>\u540e\u8bb0<\/h2>\n<p>\u4e13\u6ce8 Java \u539f\u521b\u5e72\u8d27\u5206\u4eab\uff0c\u5927\u4e09\u5f00\u6e90 <a href=\"https:\/\/link.segmentfault.com\/?enc=7%2FmlKpoqhhVBgVLgUPF4Tg%3D%3D.1PMO%2BE84lq0FEz%2B8f6he3wLy7D5EP2ZOFfDo7AVmTsA%3D\" rel=\"nofollow\">JavaGuide<\/a>\u00a0\uff08\u300cJava\u5b66\u4e60+\u9762\u8bd5\u6307\u5357\u300d\u4e00\u4efd\u6db5\u76d6\u5927\u90e8\u5206 Java \u7a0b\u5e8f\u5458\u6240\u9700\u8981\u638c\u63e1\u7684\u6838\u5fc3\u77e5\u8bc6\u3002\u51c6\u5907 Java \u9762\u8bd5\uff0c\u9996\u9009 JavaGuide\uff01\uff09\uff0c\u76ee\u524d\u5df2\u7ecf 120k+ Star\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042015044\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u539f\u521b\u4e0d\u6613\uff0c\u6b22\u8fce\u70b9\u8d5e\u5206\u4eab\uff0c<a href=\"https:\/\/segmentfault.com\/u\/javaguide\">\u6b22\u8fce\u5173\u6ce8\u6211\u5728\u601d\u5426\u7684\u8d26\u53f7<\/a>\uff0c\u6211\u4f1a\u6301\u7eed\u5206\u4eab\u539f\u521b\u5e72\u8d27\uff01\u52a0\u6cb9\uff0c\u51b2\uff01<\/p>\n<p>\u5982\u679c\u672c\u6587\u5bf9\u4f60\u6709\u5e2e\u52a9\u7684\u8bdd\uff0c\u6b22\u8fce\u70b9\u8d5e\u5206\u4eab\uff0c\u8fd9\u5bf9\u6211\u7ee7\u7eed\u5206\u4eab&#038;\u521b\u4f5c\u4f18\u8d28\u6587\u7ae0\u975e\u5e38\u91cd\u8981\u3002\u611f\u8c22 \ud83d\ude4f\ud83c\udffb<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042224181\">https:\/\/segmentfault.com\/a\/1190000042224181<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u53d1\u4e8e\uff1aRedis 5 \u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\u8be6\u89e3 &#8211; JavaGuide \u76f8\u5173\u6587\u7ae0\uff1aRedis\u5e38\u89c1\u9762\u8bd5\u9898 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006025","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006025"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006025\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}