{"id":6340,"date":"2024-07-18T15:51:49","date_gmt":"2024-07-18T07:51:49","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6340"},"modified":"2024-07-18T15:51:49","modified_gmt":"2024-07-18T07:51:49","slug":"redis-%e8%b7%b3%e8%b7%83%e8%a1%a8%e7%9a%84%e4%bd%9c%e7%94%a8","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6340","title":{"rendered":"Redis \u8df3\u8dc3\u8868\u7684\u4f5c\u7528"},"content":{"rendered":"<p>\u5728 Redis \u4e2d\uff0c\u8df3\u8dc3\u8868\uff08Skiplist\uff09\u4e3b\u8981\u7528\u4e8e\u5b9e\u73b0\u6709\u5e8f\u96c6\u5408\uff08Sorted Set\uff09\u6570\u636e\u7ed3\u6784\u3002\u6709\u5e8f\u96c6\u5408\u5728 Redis \u4e2d\u7684\u547d\u4ee4\u524d\u7f00\u4e3a <code>Z<\/code>\uff0c\u4f8b\u5982 <code>ZRANGE<\/code>\u3001<code>ZADD<\/code>\u3001<code>ZREM<\/code> \u7b49\u3002\u6709\u5e8f\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u662f\u6309\u5206\u6570\uff08score\uff09\u6392\u5e8f\u7684\uff0c\u800c\u8df3\u8dc3\u8868\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u652f\u6301\u5feb\u901f\u7684\u63d2\u5165\u3001\u5220\u9664\u548c\u6309\u5206\u6570\u6392\u5e8f\u7684\u67e5\u627e\u64cd\u4f5c\u3002<\/p>\n<h3>\u8df3\u8dc3\u8868\u7684\u7528\u9014<\/h3>\n<p>\u8df3\u8dc3\u8868\u662f\u4e00\u79cd\u6982\u7387\u5e73\u8861\u7684\u6570\u636e\u7ed3\u6784\uff0c\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8e\u5e73\u8861\u6811\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u5b83\u88ab\u7528\u6765\u5b9e\u73b0 Redis \u4e2d\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u4e3b\u8981\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<ol>\n<li><strong>\u6709\u5e8f\u6570\u636e\u7684\u5feb\u901f\u67e5\u627e<\/strong>\uff1a\u8df3\u8dc3\u8868\u5141\u8bb8\u5feb\u901f\u6309\u5206\u6570\u67e5\u627e\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u67d0\u4e2a\u5206\u6570\u8303\u56f4\u5185\u7684\u6240\u6709\u5143\u7d20\u6216\u67e5\u627e\u67d0\u4e2a\u5206\u6570\u5bf9\u5e94\u7684\u5143\u7d20\u3002<\/li>\n<li><strong>\u8303\u56f4\u67e5\u8be2<\/strong>\uff1a\u8df3\u8dc3\u8868\u652f\u6301\u9ad8\u6548\u7684\u8303\u56f4\u67e5\u8be2\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u5206\u6570\u5728\u67d0\u4e2a\u8303\u56f4\u5185\u7684\u6240\u6709\u5143\u7d20\u3002<\/li>\n<li><strong>\u6392\u5e8f\u64cd\u4f5c<\/strong>\uff1a\u8df3\u8dc3\u8868\u4fdd\u6301\u5143\u7d20\u6309\u5206\u6570\u6392\u5e8f\uff0c\u652f\u6301\u6309\u5206\u6570\u7684\u5347\u5e8f\u6216\u964d\u5e8f\u904d\u5386\u3002<\/li>\n<\/ol>\n<h3>\u4e3b\u8981\u5b9e\u73b0\u6587\u4ef6<\/h3>\n<p>\u5728 Redis \u6e90\u4ee3\u7801\u4e2d\uff0c\u8df3\u8dc3\u8868\u7684\u5b9e\u73b0\u4e3b\u8981\u96c6\u4e2d\u5728 <code>zskiplist.c<\/code> \u6587\u4ef6\u4e2d\u3002\u8be5\u6587\u4ef6\u5b9a\u4e49\u4e86\u8df3\u8dc3\u8868\u7684\u6570\u636e\u7ed3\u6784\u53ca\u5176\u76f8\u5173\u64cd\u4f5c\u3002<\/p>\n<h3>\u8df3\u8dc3\u8868\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<pre><code class=\"language-c\">typedef struct zskiplistNode {\n    struct zskiplistLevel {\n        struct zskiplistNode *forward;\n        unsigned long span;\n    } level[];\n    struct zskiplistNode *backward;\n    double score;\n    robj *obj;\n} zskiplistNode;\n\ntypedef struct zskiplist {\n    struct zskiplistNode *header, *tail;\n    unsigned long length;\n    int level;\n} zskiplist;<\/code><\/pre>\n<h3>\u4e3b\u8981\u64cd\u4f5c<\/h3>\n<ol>\n<li>\n<p><strong>\u63d2\u5165\u64cd\u4f5c<\/strong>\uff1a\u5411\u8df3\u8dc3\u8868\u4e2d\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u540c\u65f6\u6309\u7167\u5206\u6570\uff08score\uff09\u6392\u5e8f\u3002<\/p>\n<pre><code class=\"language-c\">zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj);<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5220\u9664\u64cd\u4f5c<\/strong>\uff1a\u4ece\u8df3\u8dc3\u8868\u4e2d\u5220\u9664\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-c\">int zslDelete(zskiplist *zsl, double score, robj *obj);<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u8303\u56f4\u67e5\u627e<\/strong>\uff1a\u67e5\u627e\u5206\u6570\u5728\u67d0\u4e2a\u8303\u56f4\u5185\u7684\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-c\">unsigned long zslGetRank(zskiplist *zsl, double score, robj *o);\nzskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank);<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u8303\u56f4\u5220\u9664<\/strong>\uff1a\u5220\u9664\u5206\u6570\u5728\u67d0\u4e2a\u8303\u56f4\u5185\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-c\">unsigned long zslDeleteRangeByScore(zskiplist *zsl, zrangespec *range);\nunsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned int end);<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u5e94\u7528\u793a\u4f8b<\/h3>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u6709\u5e8f\u96c6\u5408\u5b58\u50a8\u4e86\u5b66\u751f\u7684\u6210\u7ee9\uff0c\u4f7f\u7528\u8df3\u8dc3\u8868\u53ef\u4ee5\u5b9e\u73b0\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li><strong>\u63d2\u5165\u5b66\u751f\u6210\u7ee9<\/strong>\uff1a<code>ZADD students 95 &quot;Alice&quot;<\/code> \u5c06 Alice \u7684\u6210\u7ee9 95 \u63d2\u5165\u5230\u6709\u5e8f\u96c6\u5408 <code>students<\/code> \u4e2d\u3002<\/li>\n<li><strong>\u83b7\u53d6\u67d0\u4e2a\u5206\u6570\u8303\u56f4\u5185\u7684\u5b66\u751f<\/strong>\uff1a<code>ZRANGE students 0 100 WITHSCORES<\/code> \u83b7\u53d6\u6210\u7ee9\u5728 0 \u5230 100 \u4e4b\u95f4\u7684\u6240\u6709\u5b66\u751f\u53ca\u5176\u6210\u7ee9\u3002<\/li>\n<li><strong>\u5220\u9664\u67d0\u4e2a\u5b66\u751f\u7684\u6210\u7ee9<\/strong>\uff1a<code>ZREM students &quot;Alice&quot;<\/code> \u4ece\u6709\u5e8f\u96c6\u5408\u4e2d\u5220\u9664 Alice \u7684\u6210\u7ee9\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4f7f\u7528\u8df3\u8dc3\u8868\uff0cRedis \u5b9e\u73b0\u4e86\u9ad8\u6548\u7684\u6709\u5e8f\u96c6\u5408\u64cd\u4f5c\uff0c\u63d0\u4f9b\u4e86\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u6570\u636e\u67e5\u8be2\u548c\u64cd\u4f5c\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Redis \u4e2d\uff0c\u8df3\u8dc3\u8868\uff08Skiplist\uff09\u4e3b\u8981\u7528\u4e8e\u5b9e\u73b0\u6709\u5e8f\u96c6\u5408\uff08Sorted Set\uff09\u6570\u636e\u7ed3\u6784\u3002\u6709\u5e8f\u96c6\u5408\u5728&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6340\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[122],"class_list":["post-6340","post","type-post","status-publish","format-standard","hentry","category-123","tag-redis"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6340"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6340\/revisions"}],"predecessor-version":[{"id":6341,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6340\/revisions\/6341"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}