{"id":407,"date":"2020-02-23T21:55:00","date_gmt":"2020-02-23T13:55:00","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=407"},"modified":"2021-06-06T22:57:37","modified_gmt":"2021-06-06T14:57:37","slug":"redis%e6%ba%90%e7%a0%81%e4%bb%8emain%e5%bc%80%e5%a7%8b11","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=407","title":{"rendered":"redis\u6e90\u7801\u4ecemain\u5f00\u59cb11"},"content":{"rendered":"\n<p>redis\u6e90\u7801\u4ecemain\u5f00\u59cb10\u4e2d\u67e5\u770b\u4e86redis\u7684\u5bf9\u8c61\u7c7b\u578b\uff0c\u672c\u7ae0\u6765\u770bredis\u7684string\u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>11 redis\u5bf9\u8c61\u4e4bstring\n\/\/ \u5b9a\u4e49redis\u5bf9\u8c61\u7684\u7ed3\u6784\u4f53\n#define REDIS_LRU_BITS 24\ntypedef struct redisObject {\n    \/\/ \u5bf9\u8c61\u7c7b\u578b \u6709\u4e94\u79cd\n    \/\/ #define REDIS_STRING 0\n    \/\/ #define REDIS_LIST 1\n    \/\/ #define REDIS_SET 2\n    \/\/ #define REDIS_ZSET 3\n    \/\/ #define REDIS_HASH 4\n    unsigned type:4;\n    unsigned encoding:4;\n    unsigned lru:REDIS_LRU_BITS; \/* lru time (relative to server.lruclock) *\/\n    int refcount;\n    void *ptr;\n} robj;\n\n\/*\n * \u521b\u5efa\u4e00\u4e2a\u65b0 robj \u5bf9\u8c61\n *\/\nrobj *createObject(int type, void *ptr) {\n\n    robj *o = zmalloc(sizeof(*o));\n\n    o->type = type;\n    o->encoding = REDIS_ENCODING_RAW;\n    o->ptr = ptr;\n    o->refcount = 1;\n\n    \/* Set the LRU to the current lruclock (minutes resolution). *\/\n    o->lru = LRU_CLOCK();\n    return o;\n}\n\n\n\u5728redis\u6e90\u7801\u4ecemain\u5f00\u59cb2\u4e2d\u6211\u4eec\u8bb2\u8ff0\u4e86initServer()\u65b9\u6cd5,\u5728\u521d\u59cb\u5316redis\u670d\u52a1\u5668\u7684\u65f6\u5019,\u521b\u5efa\u4e86\u5927\u91cf\u7528\u4e8e\u56de\u590d\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\n\n\/\/ \u4f8b\u5982,\u521b\u5efa\u4e00\u4e2aredis\u5bf9\u8c61,\u7c7b\u578b\u662fREDIS_STRING,\u8fd9\u4fbf\u662f\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u5bf9\u8c61\nshared.crlf = createObject(REDIS_STRING, sdsnew(\"\\r\\n\"));\n\/\/ \u5728\u521b\u5efa\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u65f6\u5019\u6211\u4eec\u770b\u5230\u4e86\u4f7f\u7528\u4e86sdsnew(\"\\r\\n\"),\n\/\/ sdsnew\u8fd4\u56de\u4e00\u4e2asds, sds\u662fredis\u7684\u52a8\u6001\u5b57\u7b26\u4e32\u7684\u5b9e\u73b0,\u4e0b\u9762\u6211\u4eec\u6765\u770b\u770b\u76f8\u5173\u5b9e\u73b0\n\n\/\/\u8fd9\u662fredis\u58f0\u660e\u7684\u522b\u540d\ntypedef char *sds;\n\n\/\/ \u8fd9\u662fredis\u4fdd\u5b58\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u7ed3\u6784\nstruct sdshdr {\n    int len;  \/\/ \u4f7f\u7528\u4e86buf\u591a\u5927\u7a7a\u95f4\n    int free; \/\/ \u5269\u4f59buf\u7a7a\u95f4\n    char buf&#91;]; \/\/ \u5b58\u653e\u5177\u4f53\u6570\u636e\u7684\u7a7a\u95f4\n};\n\n\/\/ \u6839\u636e\u7ed9\u5b9a\u7684\u521d\u59cb\u5316\u5b57\u8282 \u521b\u5efa\u4e00\u4e2asds\nsds sdsnew(const char *init) {\n    size_t initlen = (init == NULL) ? 0 : strlen(init);\n    return sdsnewlen(init, initlen);\n}\n\n\/\/ \u6839\u636e\u7ed9\u5b9a\u5185\u5bb9\u548c\u957f\u5ea6,\u521b\u5efasds\nsds sdsnewlen(const void *init, size_t initlen) {\n    struct sdshdr *sh;\n    if (init) {\n        \/\/ zmalloc \u4e0d\u521d\u59cb\u5316\u6240\u5206\u914d\u7684\u5185\u5b58\n        sh = zmalloc(sizeof(struct sdshdr)+initlen+1);\n    } else {\n        \/\/ zcalloc \u5c06\u5206\u914d\u7684\u5185\u5b58\u5168\u90e8\u521d\u59cb\u5316\u4e3a 0\n        sh = zcalloc(sizeof(struct sdshdr)+initlen+1);\n    }\n\n    \/\/ \u5206\u914d\u5931\u8d25\n    if (sh == NULL) return NULL;\n\n    \/\/ \u5df2\u4f7f\u7528\u957f\u5ea6\n    sh->len = initlen;\n    \/\/ \u6ca1\u6709\u5269\u4f59\u7a7a\u95f4\n    sh->free = 0;\n\n    \/\/ \u6709\u521d\u59cb\u5316\u7684\u5185\u5bb9\n    if (initlen &amp;&amp; init)\n        memcpy(sh->buf, init, initlen);\n    \/\/ \u4ee5 \\0 \u7ed3\u5c3e\n    sh->buf&#91;initlen] = '\\0';\n    \/\/ \u8fd4\u56debuf\u90e8\u5206\n    return (char*)sh->buf;\n}\n\n\/\/ \u53ef\u4ee5\u901a\u8fc7sds\u53d6\u5230\u6574\u4e2a\u7ed3\u6784\u4f53\nstruct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));\n\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/download.redis.io\/releases\/redis-3.0.0.tar.gz\" target=\"_blank\" rel=\"noopener\">\u57fa\u4e8e\u7248\u672c3.0.0\u7248\u672c,<\/a>\u70b9\u51fb\u4e0b\u8f7dhttps:\/\/download.redis.io\/releases\/redis-3.0.0.tar.gz<\/p>\n\n\n\n<p><a href=\"https:\/\/www.ccagml.com\/?p=407\">\u672c\u6587\u5730\u5740<\/a>\uff0chttps:\/\/www.ccagml.com\/?p=407<\/p>\n","protected":false},"excerpt":{"rendered":"<p>redis\u6e90\u7801\u4ecemain\u5f00\u59cb10\u4e2d\u67e5\u770b\u4e86redis\u7684\u5bf9\u8c61\u7c7b\u578b\uff0c\u672c\u7ae0\u6765\u770bredis\u7684string\u7c7b\u578b \u57fa\u4e8e\u7248\u672c3<a href=\"https:\/\/www.ccagml.com\/?p=407\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">redis\u6e90\u7801\u4ecemain\u5f00\u59cb11<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[31,22],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/407"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=407"}],"version-history":[{"count":3,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/407\/revisions"}],"predecessor-version":[{"id":415,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/407\/revisions\/415"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}