{"id":201277,"date":"2025-05-10T08:32:07","date_gmt":"2025-05-10T00:32:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201277\/"},"modified":"2025-05-10T08:32:07","modified_gmt":"2025-05-10T00:32:07","slug":"redis%e4%b8%93%e5%b1%9e%e9%93%be%e8%a1%a8ziplist%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201277\/","title":{"rendered":"redis\u4e13\u5c5e\u94fe\u8868ziplist\u7684\u4f7f\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>redis\u4e13\u5c5e\u94fe\u8868ziplist\u7684\u4f7f\u7528<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aredis\u4e13\u5c5e\u94fe\u8868ziplist\u7684\u4f7f\u7528\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<h2>\u95ee\u9898\u629b\u51fa<\/h2>\n<p>\u7528\u8fc7 Python \u7684\u5217\u8868\u5417\uff1f\u5c31\u662f\u90a3\u79cd\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\u6570\u636e\u7684\uff0c\u652f\u6301\u968f\u673a\u8bfb\u53d6\u7684\u6570\u636e\u7ed3\u6784\u3002<br \/> \u6ca1\u6709\u7528\u8fc7\u7684\u8bdd\u90a3\u5c31\u6ca1\u529e\u6cd5\u4e86\u3002<\/p>\n<p>\u672c\u8d28\u4e0a\u8fd9\u79cd\u5217\u8868\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u3001\u94fe\u8868\u4f5c\u4e3a\u5176\u5e95\u5c42\u7ed3\u6784\uff0c\u4e0d\u77e5\u9053Python\u4e2d\u7684\u5217\u8868\u662f\u4ee5\u4ec0\u4e48\u4f5c\u4e3a\u5e95\u5c42\u7ed3\u6784\u7684\u3002<br \/> \u4f46\u662fredis\u7684\u5217\u8868\u65e2\u4e0d\u662f\u7528\u94fe\u8868\uff0c\u4e5f\u4e0d\u662f\u7528\u6570\u7ec4\u4f5c\u4e3a\u5176\u5e95\u5c42\u5b9e\u73b0\u7684\uff0c\u539f\u56e0\u4e5f\u663e\u800c\u6613\u89c1\uff1a\u6570\u7ec4\u4e0d\u65b9\u4fbf\uff0c\u5f04\u4e2a\u4e8c\u7ef4\u7684\uff1f\u67d4\u6027\u7684\uff1f\u600e\u4e48\u5199\uff1f\u94fe\u8868\u53ef\u4ee5\u5b9e\u73b0\uff0c\u901a\u7528\u94fe\u8868\u561b\uff0c\u6570\u636e\u57df\u653e void* \u5c31\u53ef\u4ee5\u5b9e\u73b0\u5217\u8868\u529f\u80fd\u3002\u4f46\u662f\uff0c\u94fe\u8868\u7684\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\uff0c\u5bb9\u6613\u9020\u6210\u5185\u5b58\u788e\u7247\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u5927\u73af\u5883\u4e0b\uff0c\u79c9\u627f\u7740\u201c\u80fd\u7701\u5c31\u7701\u201d\u7684\u6307\u5bfc\u601d\u60f3\uff0c\u8bf7\u4f60\u8bbe\u8ba1\u4e00\u6b3e\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<h2>\u7ed3\u6784\u8bbe\u8ba1<\/h2>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167255104563b11a85aa34d.png\" class=\"aligncenter\"><\/p>\n<p>\u8fd9\u4e2a\u56fe\u91cc\u8981\u6ce8\u610f\uff0c\u53f3\u4fa7\u662f\u6ca1\u6709\u8bb0\u5f55\u201c\u5f53\u524d\u5143\u7d20\u7684\u5927\u5c0f\u201d\u7684<\/p>\n<p>\u8fd9\u4e2a\u56fe\u633a\u8be6\u7ec6\u54c8\uff0c\u90fd\u7701\u5f97\u6211\u5bf9\u6bcf\u4e00\u4e2a\u5b57\u6bb5\u91ca\u4e49\u4e86\uff0c\u6574\u633a\u597d\u3002<\/p>\n<p>\u5176\u4ed6\u8bdd\uff0c\u6587\u4ef6\u5f00\u5934\u7684\u6ce8\u91ca\u4e5f\u8bb2\u7684\u5f88\u6e05\u695a\u4e86\u3002\uff08ziplist.c\uff09<\/p>\n<pre>\n\/* The ziplist is a specially encoded dually linked list that is designed\n * to be very memory efficient. It stores both strings and integer values,\n * where integers are encoded as actual integers instead of a series of\n * characters. It allows push and pop operations on either side of the list\n * in O(1) time. However, because every operation requires a reallocation of\n * the memory used by the ziplist, the actual complexity is related to the\n * amount of memory used by the ziplist.\n *\n * ----------------------------------------------------------------------------\n *\n * ZIPLIST OVERALL LAYOUT\n * ======================\n *\n * The general layout of the ziplist is as follows:\n *\n * \n   \n    \n     \n      \n       \n         ... \n        \n         \n           * * NOTE: all fields are stored in little endian, if not specified otherwise. * * \n          \n            is an unsigned integer to hold the number of bytes that * the ziplist occupies, including the four bytes of the zlbytes field itself. * This value needs to be stored to be able to resize the entire structure * without the need to traverse it first. * * \n           \n             is the offset to the last entry in the list. This allows * a pop operation on the far side of the list without the need for full * traversal. * * \n            \n              is the number of entries. When there are more than * 2^16-2 entries, this value is set to 2^16-1 and we need to traverse the * entire list to know how many items it holds. * * \n             \n               is a special entry representing the end of the ziplist. * Is encoded as a single byte equal to 255. No other normal entry starts * with a byte set to the value of 255. * * ZIPLIST ENTRIES * =============== * * Every entry in the ziplist is prefixed by metadata that contains two pieces * of information. First, the length of the previous entry is stored to be * able to traverse the list from back to front. Second, the entry encoding is * provided. It represents the entry type, integer or string, and in the case * of strings it also represents the length of the string payload. * So a complete entry is stored like this: * * \n              \n               \n                \n                  * * Sometimes the encoding represents the entry itself, like for small integers * as we'll see later. In such a case the \n                 \n                   part is missing, and we * could have just: * * \n                  \n                   \n                     * * The length of the previous entry, \n                    \n                     , is encoded in the following way: * If this length is smaller than 254 bytes, it will only consume a single * byte representing the length as an unsinged 8 bit integer. When the length * is greater than or equal to 254, it will consume 5 bytes. The first byte is * set to 254 (FE) to indicate a larger value is following. The remaining 4 * bytes take the length of the previous entry as value. * * So practically an entry is encoded in the following way: * * \n                     \n                      \n                       \n                         * * Or alternatively if the previous entry length is greater than 253 bytes * the following encoding is used: * * 0xFE \n                        \n                         \n                           * * The encoding field of the entry depends on the content of the * entry. When the entry is a string, the first 2 bits of the encoding first * byte will hold the type of encoding used to store the length of the string, * followed by the actual length of the string. When the entry is an integer * the first 2 bits are both set to 1. The following 2 bits are used to specify * what kind of integer will be stored after this header. An overview of the * different types and encodings is as follows. The first byte is always enough * to determine the kind of entry. * * |00pppppp| - 1 byte * String value with length less than or equal to 63 bytes (6 bits). * \"pppppp\" represents the unsigned 6 bit length. * |01pppppp|qqqqqqqq| - 2 bytes * String value with length less than or equal to 16383 bytes (14 bits). * IMPORTANT: The 14 bit number is stored in big endian. * |10000000|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes * String value with length greater than or equal to 16384 bytes. * Only the 4 bytes following the first byte represents the length * up to 2^32-1. The 6 lower bits of the first byte are not used and * are set to zero. * IMPORTANT: The 32 bit number is stored in big endian. * |11000000| - 3 bytes * Integer encoded as int16_t (2 bytes). * |11010000| - 5 bytes * Integer encoded as int32_t (4 bytes). * |11100000| - 9 bytes * Integer encoded as int64_t (8 bytes). * |11110000| - 4 bytes * Integer encoded as 24 bit signed (3 bytes). * |11111110| - 2 bytes * Integer encoded as 8 bit signed (1 byte). * |1111xxxx| - (with xxxx between 0000 and 1101) immediate 4 bit integer. * Unsigned integer from 0 to 12. The encoded value is actually from * 1 to 13 because 0000 and 1111 can not be used, so 1 should be * subtracted from the encoded 4 bit value to obtain the right value. * |11111111| - End of ziplist special entry. * * Like for the ziplist header, all the integers are represented in little * endian byte order, even when this code is compiled in big endian systems. * * EXAMPLES OF ACTUAL ZIPLISTS * =========================== * * The following is a ziplist containing the two elements representing * the strings \"2\" and \"5\". It is composed of 15 bytes, that we visually * split into sections: * * [0f 00 00 00] [0c 00 00 00] [02 00] [00 f3] [02 f6] [ff] * | | | | | | * zlbytes zltail entries \"2\" \"5\" end * * The first 4 bytes represent the number 15, that is the number of bytes * the whole ziplist is composed of. The second 4 bytes are the offset * at which the last ziplist entry is found, that is 12, in fact the * last entry, that is \"5\", is at offset 12 inside the ziplist. * The next 16 bit integer represents the number of elements inside the * ziplist, its value is 2 since there are just two elements inside. * Finally \"00 f3\" is the first entry representing the number 2. It is * composed of the previous entry length, which is zero because this is * our first entry, and the byte F3 which corresponds to the encoding * |1111xxxx| with xxxx between 0001 and 1101. We need to remove the \"F\" * higher order bits 1111, and subtract 1 from the \"3\", so the entry value * is \"2\". The next entry has a prevlen of 02, since the first entry is * composed of exactly two bytes. The entry itself, F6, is encoded exactly * like the first entry, and 6-1 = 5, so the value of the entry is 5. * Finally the special entry FF signals the end of the ziplist. * * Adding another element to the above string with the value \"Hello World\" * allows us to show how the ziplist encodes small strings. We'll just show * the hex dump of the entry itself. Imagine the bytes as following the * entry that stores \"5\" in the ziplist above: * * [02] [0b] [48 65 6c 6c 6f 20 57 6f 72 6c 64] * * The first byte, 02, is the length of the previous entry. The next * byte represents the encoding in the pattern |00pppppp| that means * that the entry is a string of length \n                          \n                           , so 0B means that * an 11 bytes string follows. From the third byte (48) to the last (64) * there are just the ASCII characters for \"Hello World\". * * ---------------------------------------------------------------------------- * * Copyright (c) 2009-2012, Pieter Noordhuis \n                           \n                             * Copyright (c) 2009-2017, Salvatore Sanfilippo \n                            \n                              * All rights reserved. *\/\n                            \n                           \n                          \n                         \n                        \n                       \n                      \n                     \n                    \n                   \n                  \n                 \n                \n               \n              \n             \n            \n           \n          \n         \n        \n       \n      \n     \n    \n   <\/pre>\n<p>\u770b\u5b8c\u4e86\u4e48\uff1f\u63a5\u4e0b\u6765\u5c31\u662f\u57fa\u64cd\u9636\u6bb5\u4e86\uff0c\u5bf9\u4e8e\u4efb\u4f55\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u57fa\u64cd\u65e0\u975e\u589e\u5220\u67e5\u6539\u3002<\/p>\n<h3>\u5b9e\u9645\u8282\u70b9<\/h3>\n<pre>\ntypedef struct zlentry {\n    unsigned int prevrawlensize; \/* Bytes used to encode the previous entry len*\/\n    unsigned int prevrawlen;     \/* Previous entry len. *\/\n    unsigned int lensize;        \/* Bytes used to encode this entry type\/len.\n                                    For example strings have a 1, 2 or 5 bytes\n                                    header. Integers always use a single byte.*\/\n    unsigned int len;            \/* Bytes used to represent the actual entry.\n                                    For strings this is just the string length\n                                    while for integers it is 1, 2, 3, 4, 8 or\n                                    0 (for 4 bit immediate) depending on the\n                                    number range. *\/\n    unsigned int headersize;     \/* prevrawlensize + lensize. *\/\n    unsigned char encoding;      \/* Set to ZIP_STR_* or ZIP_INT_* depending on\n                                    the entry encoding. However for 4 bits\n                                    immediate integers this can assume a range\n                                    of values and must be range-checked. *\/\n    unsigned char *p;            \/* Pointer to the very start of the entry, that\n                                    is, this points to prev-entry-len field. *\/\n} zlentry;\n<\/pre>\n<h2>\u57fa\u672c\u64cd\u4f5c<\/h2>\n<p>\u6211\u89c9\u5f97\u8fd9\u5f20\u56fe\u8fd8\u662f\u8981\u518d\u6446\u4e00\u4e0b\uff1a<br \/><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167255104563b11a85aa34d.png\" class=\"aligncenter\"><br \/>\u8fd9\u4e2a\u56fe\u91cc\u8981\u6ce8\u610f\uff0c\u53f3\u4fa7\u662f\u6ca1\u6709\u8bb0\u5f55\u201c\u5f53\u524d\u5143\u7d20\u7684\u5927\u5c0f\u201d\u7684<\/p>\n<h3>\u589e<\/h3>\n<p>\u771f\u5b9e\u63d2\u5165\u7684\u662f\u8fd9\u4e2a\u51fd\u6570\uff1a<\/p>\n<p>\u8bb2\u771f\uff0c\u5934\u76ae\u6709\u70b9\u53d1\u9ebb\u3002\u90a3\u4e48\u6211\u4eec\u7b49\u4e0b\u8fd8\u662f\u7528\u8001\u5957\u8def\uff0c\u6309\u6b65\u9aa4\u62c6\u5f00\u6765\u770b\u3002<\/p>\n<pre>\n\/* Insert item at \"p\". *\/\nunsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {\n    size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen;\n    unsigned int prevlensize, prevlen = 0;\n    size_t offset;\n    int nextdiff = 0;\n    unsigned char encoding = 0;\n    long long value = 123456789; \/* initialized to avoid warning. Using a value\n                                    that is easy to see if for some reason\n                                    we use it uninitialized. *\/\n    zlentry tail;\n\n    \/* Find out prevlen for the entry that is inserted. *\/\n    if (p[0] != ZIP_END) {\n        ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);\n    } else {\n        unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);\n        if (ptail[0] != ZIP_END) {\n            prevlen = zipRawEntryLength(ptail);\n        }\n    }\n\n    \/* See if the entry can be encoded *\/\n    if (zipTryEncoding(s,slen,&amp;value,&amp;encoding)) {\n        \/* 'encoding' is set to the appropriate integer encoding *\/\n        reqlen = zipIntSize(encoding);\n    } else {\n        \/* 'encoding' is untouched, however zipStoreEntryEncoding will use the\n         * string length to figure out how to encode it. *\/\n        reqlen = slen;\n    }\n    \/* We need space for both the length of the previous entry and\n     * the length of the payload. *\/\n    reqlen += zipStorePrevEntryLength(NULL,prevlen);\n    reqlen += zipStoreEntryEncoding(NULL,encoding,slen);\n\n    \/* When the insert position is not equal to the tail, we need to\n     * make sure that the next entry can hold this entry's length in\n     * its prevlen field. *\/\n    int forcelarge = 0;\n    nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;\n    if (nextdiff == -4 &amp;&amp; reqlen \n\n<p>\u5bf9\u201c\u94fe\u8868\u201d\u63d2\u5165\u6570\u636e\u6709\u51e0\u4e2a\u6b65\u9aa4\uff1f<br>\n1\u3001\u504f\u79fb<br>\n2\u3001\u63d2\u8fdb\u53bb<br>\n3\u3001\u7f1d\u5408<\/p>\n<p>\u90a3\u8fd9\u4e2a\u201c\u5217\u8868\u201d\uff0c\u6bd4\u8f83\u7279\u6b8a\u4e00\u70b9\uff0c\u7279\u6b8a\u5728\u54ea\u91cc\uff1f\u7279\u6b8a\u5728\u5b83\u6bd4\u8f83\u7d27\u51d1\uff0c\u800c\u4e14\u6570\u636e\u7c7b\u578b\uff0c\u5176\u5b9e\u4e5f\u5c31\u4e24\u79cd\uff0c\u8981\u4e48integer\uff0c\u8981\u4e48string\u3002\u6240\u4ee5\u5b83\u7684\u6b65\u9aa4\u662f\uff1f<br>\n1\u3001\u6570\u636e\u91cd\u65b0\u7f16\u7801<br>\n2\u3001\u89e3\u6790\u6570\u636e\u5e76\u5206\u914d\u7a7a\u95f4<br>\n3\u3001\u63a5\u5165\u6570\u636e<\/p>\n<p><strong>\u91cd\u65b0\u7f16\u7801<\/strong><\/p>\n<p>\u4ec0\u4e48\u662f\u91cd\u65b0\u7f16\u7801\uff1f\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u662f\u4e0d\u662f\u9700\u8981\u5bf9\uff1a\u201c\u524d\u4e00\u4e2a\u5143\u7d20\u7684\u5927\u5c0f\u3001\u672c\u8eab\u5927\u5c0f\u3001\u5f53\u524d\u5143\u7d20\u7f16\u7801\u201d \u8fd9\u4e9b\u6570\u636e\u8fdb\u884c\u4e00\u4e2a\u7edf\u8ba1\uff0c\u7136\u540e\u4e00\u5e76\u63d2\u5165\u3002\u5c31\u7f16\u8fd9\u4e2a\u3002<\/p>\n<p>\u63d2\u5165\u4f4d\u7f6e\u65e0\u975e\u4e09\u4e2a\uff0c\u5934\u4e2d\u5c3e\u3002<br>\n\u5934\uff1a\u524d\u4e00\u4e2a\u5143\u7d20\u5927\u5c0f\u4e3a0\uff0c\u56e0\u4e3a\u524d\u9762\u6ca1\u6709\u5143\u7d20\u3002<br>\n\u4e2d\uff1a\u5f85\u63d2\u5165\u4f4d\u7f6e\u540e\u4e00\u4e2a\u5143\u7d20\u8bb0\u5f55\u7684\u201c\u524d\u4e00\u4e2a\u5143\u7d20\u5927\u5c0f\u201d\uff0c\u5f53\u7136\uff0c\u4e4b\u540e\u672c\u8eab\u5927\u5c0f\u5c31\u6210\u4e3a\u4e86\u540e\u4e00\u4e2a\u5143\u7d20\u773c\u4e2d\u7684\u201c\u524d\u4e00\u4e2a\u5143\u7d20\u5927\u5c0f\u201d\u3002<br>\n\u5c3e\uff1a\u90a3\u5c31\u8981\u628a\u4e09\u4e2a\u5b57\u6bb5\u52a0\u8d77\u6765\u4e86\u3002<\/p>\n<p>\u5177\u4f53\u600e\u4e48\u91cd\u65b0\u7f16\u7801\u5c31\u4e0d\u770b\u4e86\u5427\uff0c\u8fd9\u7bc7\u672c\u6765\u5c31\u5df2\u7ecf\u5f88\u957f\u4e86\u3002<\/p>\n<p><strong>\u89e3\u6790\u6570\u636e<\/strong><\/p>\n<p>\u518d\u5f80\u4e0b\u5c31\u662f\u89e3\u6790\u6570\u636e\u4e86\u3002<br>\n\u9996\u5148\u5c1d\u8bd5\u5c06\u6570\u636e\u89e3\u6790\u4e3a\u6574\u6570\uff0c\u5982\u679c\u53ef\u4ee5\u89e3\u6790\uff0c\u5c31\u6309\u7167\u538b\u7f29\u5217\u8868\u6574\u6570\u7c7b\u578b\u7f16\u7801\u5b58\u50a8\uff1b\u5982\u679c\u89e3\u6790\u5931\u8d25\uff0c\u5c31\u6309\u7167\u538b\u7f29\u5217\u8868\u5b57\u8282\u6570\u7ec4\u7c7b\u578b\u7f16\u7801\u5b58\u50a8\u3002<\/p>\n<p>\u89e3\u6790\u4e4b\u540e\uff0c\u6570\u503c\u5b58\u50a8\u5728 value \u4e2d\uff0c\u7f16\u7801\u683c\u5f0f\u5b58\u50a8\u5728 encoding\u4e2d\u3002\u5982\u679c\u89e3\u6790\u6210\u529f\uff0c\u8fd8\u8981\u8ba1\u7b97\u6574\u6570\u6240\u5360\u5b57\u8282\u6570\u3002\u53d8\u91cf reqlen \u5b58\u50a8\u5f53\u524d\u5143\u7d20\u6240\u9700\u7a7a\u95f4\u5927\u5c0f\uff0c\u518d\u7d2f\u52a0\u5176\u4ed6\u4e24\u4e2a\u5b57\u6bb5\u7684\u7a7a\u95f4\u5927\u5c0f\uff0c\u5c31\u662f\u672c\u8282\u70b9\u6240\u9700\u7a7a\u95f4\u5927\u5c0f\u4e86\u3002<\/p>\n<p><strong>\u91cd\u65b0\u5206\u914d\u7a7a\u95f4<\/strong><\/p>\n<p>\u770b\u6ce8\u91ca\u8fd9\u67b6\u52bf\uff0c\u548b\u6ef4\uff0c\u8fd8\u5b58\u5728\u6ca1\u5730\u65b9\u7ed9\u5b83\u585e\uff1f<\/p>\n<p>\u6765\u6211\u4eec\u770b\u770b\u3002<\/p>\n<p>\u8fd9\u91cc\u7684\u5206\u914d\u7a7a\u95f4\u4e0d\u662f\u7b80\u5355\u7684\u5c31\u65b0\u63d2\u8fdb\u6765\u7684\u6570\u636e\u591a\u5c11\u7a7a\u95f4\u5c31\u5206\u914d\u591a\u5c11\uff0c\u5982\u679c\u6ca1\u6709\u4ed4\u7ec6\u9605\u8bfb\u4e0a\u9762\u90a3\u6bb5\u82f1\u6587\u7684\u8bdd\uff0c\u55ef\uff0c\u53ef\u4ee5\u9009\u62e9\u7ed5\u56de\u53bb\u4ed4\u7ec6\u9605\u8bfb\u4e00\u4e0b\u90a3\u4e2a\u8282\u70b9\u7ec4\u6210\u3002\u7279\u522b\u662f\u90a3\u4e2a\uff1a<\/p>\n\n<pre>\n\/*\n* The length of the previous entry, \n    \n     , is encoded in the following way:\n* If this length is smaller than 254 bytes, it will only consume a single\n* byte representing the length as an unsinged 8 bit integer. When the length\n* is greater than or equal to 254, it will consume 5 bytes. The first byte is\n* set to 254 (FE) to indicate a larger value is following. The remaining 4\n* bytes take the length of the previous entry as value.\n*\/\n\n    <\/pre>\n<p>\u6240\u4ee5\u8fd9\u4e2a previous \u5c31\u662f\u4e2a\u4e0d\u786e\u5b9a\u56e0\u7d20\u3002\u6709\u53ef\u80fd\u4eba\u5bb6\u672c\u6765\u662f 1 1 \u6392\u5217\u7684\uff0c\u4e2d\u95f4\u63d2\u8fdb\u6765\u4e00\u4e2a\u4e4b\u540e\u53d8\u6210 1 1 5 \u6392\u5217\u4e86\uff1b\u4e5f\u6709\u53ef\u80fd\u4eba\u5bb6\u662f1 5 \u6392\u5217\u7684\u30015 1 \u6392\u5217\u7684\uff0c\u603b\u4e4b\u5c31\u662f\u4e0d\u786e\u5b9a\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5728 entryX \u7684\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u6570\u636e\u4e4b\u540e\uff0centryX+1 \u7684 previous \u53ef\u80fd\u4e0d\u53d8\uff0c\u53ef\u80fd\u52a0\u56db\uff0c\u4e5f\u53ef\u80fd\u51cf\u56db\uff0c\u8c01\u4e5f\u8bf4\u4e0d\u51c6\u3002\u8bf4\u4e0d\u51c6\u90a3\u4e0d\u5c31\u5f97\u6d4b\u4e00\u4e0b\u561b\u3002\u6240\u4ee5\u5c31\u6d4b\u4e00\u4e0b\uff0c\u4ec5\u6b64\u800c\u5df2\u3002<\/p>\n<p><strong>\u63a5\u5165\u6570\u636e<\/strong><\/p>\n<p>\u6570\u636e\u600e\u4e48\u63a5\u5165\uff1f\u9274\u4e8e\u8fd9\u91cc\u771f\u5fc3\u4e0d\u662f\u94fe\u8868\uff0c\u662f\u5217\u8868\u3002<br \/>\n\u6240\u4ee5\uff0c\u6309\u6570\u7ec4\u90a3\u4e00\u5957\u6765\u3002\u5bf9\u3002<\/p>\n<p>\u5f88\u9ebb\u70e6\u5427\u3002\u5176\u5b9e\u4e0d\u9ebb\u70e6\uff0c\u4f60\u5728redis\u91cc\u89c1\u8fc7\u5b83\u7ed9\u4f60\u4e2d\u95f4\u63d2\u5165\u7684\u673a\u4f1a\u4e86\u5417\uff1f\u66f4\u4e0d\u8981\u8bf4\u5934\u63d2\u4e86\uff0c\u4f60\u89c1\u8fc7\u5b83\u7ed9\u4f60\u5934\u63d2\u7684\u673a\u4f1a\u4e86\u5417\uff1f<\/p>\n<p>\u63d2\u4e2a\u9898\u5916\u8bdd\uff1a\u5927\u6570\u636e\u63d2\u5165\u65f6\uff0c\u6570\u7ec4\u4e0d\u4e00\u5b9a\u8f93\u7ed9\u94fe\u8868\u3002\u5728\u5c3e\u63d2\u7684\u65f6\u5019\uff0c\u6570\u7ec4\u7684\u4f18\u52bf\u662f\u8fdc\u8d85\u94fe\u8868\u7684\uff08\u5f53\u7136\uff0c\u4ec5\u9650\u4e8e\u5c3e\u63d2\uff09\u3002\u5728\u6211\u4e24\u4e2a\u6708\u524d\u7684\u535a\u5ba2\u91cc\u6709\u505a\u8fc7\u8fd9\u4e00\u7cfb\u5217\u7684\u5b9e\u9a8c\u3002<\/p>\n<p>\u5220\u5c31\u4e0d\u5199\u4e86\u5427\uff0c\u589e\u7684\u9006\u64cd\u4f5c\uff0c\u4ece\u7cfb\u5217\u5f00\u59cb\u5c31\u6ca1\u5199\u8fc7\u5220\u3002\u4e0d\u8fc7\u8fd9\u91cc\u5220\u5c31\u4e0d\u53ef\u907f\u514d\u7684\u5927\u91cf\u6570\u636e\u8fdb\u884c\u590d\u5236\u4e86\uff08\u5982\u679c\u4e0d\u771f\u5220\uff0c\u53ea\u662f\u505a\u4e2a\u5220\u9664\u6807\u5fd7\u5462\uff1f\u8fd9\u6837\u4f1a\u7701\u65f6\u95f4\uff0c\u4f46\u662f\u65f6\u5019\u4f1a\u9020\u6210\u5185\u5b58\u788e\u7247\u5316\u3002\u4e0d\u8fc7\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a\u5b9a\u671f\u8c03\u6574\u5185\u5b58\u7684\u51fd\u6570\uff0c\u6bd4\u65b9\u8bf4\u91cd\u7528\u4e09\u5206\u4e4b\u4e00\u7684\u5757\u4e4b\u540e\u7d27\u51d1\u4e00\u4e0b\uff1f\u5185\u5b58\u4e0d\u591f\u7528\u7684\u65f6\u5019\u7d27\u51d1\u4e00\u4e0b\uff1fSTL\u5c31\u662f\u8fd9\u4e48\u5e72\u7684\uff09\u3002<\/p>\n<p>\u67e5\u4e5f\u6ca1\u5565\u597d\u8bb2\u7684\u4e86\u5427\uff0c\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u7684\u5e94\u7528\u573a\u666f\u4e00\u822c\u5c31\u662f\u5bf9\u952e\u8fdb\u884c\u68c0\u7d22\uff0c\u8fd9\u91cc\u5c31\u662f\u4e2a\u503c\uff0c\u4e0d\u4e00\u6837\u7684\u662f\u8fd9\u4e2a\u503c\u662f\u4e00\u4e32\u7684\u3002<br \/>\n\u6240\u4ee5\u9664\u4e86\u63d0\u4f9b\u539f\u6709\u7684\u524d\u540e\u5411\u904d\u5386\u4e4b\u5916\uff0c\u8fd8\u63d0\u4f9b\u4e86 range \u67e5\u8be2\uff0c\u4e0d\u96be\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>redis\u4e13\u5c5e\u94fe\u8868ziplist&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201277","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201277","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201277"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201277\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}