{"id":398,"date":"2020-01-26T21:22:00","date_gmt":"2020-01-26T13:22:00","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=398"},"modified":"2021-05-17T21:23:04","modified_gmt":"2021-05-17T13:23:04","slug":"redis%e6%ba%90%e7%a0%81%e4%bb%8emain%e5%bc%80%e5%a7%8b9","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=398","title":{"rendered":"redis\u6e90\u7801\u4ecemain\u5f00\u59cb9"},"content":{"rendered":"\n<p>\u672c\u7ae0\u518d\u6765\u770b\u51e0\u4e2a\u5e38\u89c1\u547d\u4ee4\uff0c\u5206\u522b\u4e3adbsize\u3001exists\u3001del\u547d\u4ee4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n1.\u8fd4\u56de\u5e93\u952e\u603b\u6570\ndbsize\n\/\/ 127.0.0.1:6379> dbsize\n\/\/ (integer) 11\n\/\/\u8fd4\u56dedb->ht&#91;0].used+db->ht&#91;1].used\nvoid dbsizeCommand(redisClient *c) {\n    addReplyLongLong(c,dictSize(c->db->dict));\n}\n#define dictSize(d) ((d)->ht&#91;0].used+(d)->ht&#91;1].used)\n\n\n\n2.\u952e\u662f\u5426\u5b58\u5728\nexists\n\/\/ 127.0.0.1:6379> exists aaa \n\/\/ (integer) 1\n\/\/ \u5148\u68c0\u67e5\u662f\u5426\u8fc7\u671f,\u518d\u68c0\u67e5\u662f\u5426\u5b58\u5728\n\nvoid existsCommand(redisClient *c) {\n    expireIfNeeded(c->db,c->argv&#91;1]);\n    if (dbExists(c->db,c->argv&#91;1])) {\n        addReply(c, shared.cone);\n    } else {\n        addReply(c, shared.czero);\n    }\n}\nint dbExists(redisDb *db, robj *key) {\n    return dictFind(db->dict,key->ptr) != NULL;\n}\n\n\n\/*\n\u8ba1\u7b97\u54c8\u5e0c\u503c,\u5224\u65ad\u662f\u5426\u5b58\u5728\n *\/\ndictEntry *dictFind(dict *d, const void *key)\n{\n    dictEntry *he;\n    unsigned int h, idx, table;\n\n    if (d->ht&#91;0].size == 0) return NULL; \/* We don't have a table at all *\/\n    if (dictIsRehashing(d)) _dictRehashStep(d);\n\n    h = dictHashKey(d, key);\n    for (table = 0; table &lt;= 1; table++) {\n        idx = h &amp; d->ht&#91;table].sizemask;\n        he = d->ht&#91;table].table&#91;idx];\n        while(he) {\n            if (dictCompareKeys(d, key, he->key))\n                return he;\n            he = he->next;\n        }\n        if (!dictIsRehashing(d)) return NULL;\n    }\n    return NULL;\n}\n\n\n3.\u5220\u9664\u952e\ndel key &#91;key \u2026]\n\/\/ 127.0.0.1:6379> del aaa bbb\n\/\/ (integer) 2\n\n\/\/\u5148\u68c0\u67e5\u8fc7\u671f,\u5728\u5224\u65ad\u5220\u9664\nvoid delCommand(redisClient *c) {\n    int deleted = 0, j;\n    for (j = 1; j &lt; c->argc; j++) {\n        expireIfNeeded(c->db,c->argv&#91;j]);\n        if (dbDelete(c->db,c->argv&#91;j])) {\n            signalModifiedKey(c->db,c->argv&#91;j]);\n            notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,\n                \"del\",c->argv&#91;j],c->db->id);\n            server.dirty++;\n            deleted++;\n        }\n    }\n    addReplyLongLong(c,deleted);\n}\n\n\nint dbDelete(redisDb *db, robj *key) {\n    if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr);\n    if (dictDelete(db->dict,key->ptr) == DICT_OK) {\n        if (server.cluster_enabled) slotToKeyDel(key);\n        return 1;\n    } else {\n        return 0;\n    }\n}\n\nint dictDelete(dict *ht, const void *key) {\n    return dictGenericDelete(ht,key,0);\n}\n\n\/\/ \u4e0edictFind\u7c7b\u4f3c,\u627e\u5230\u540e\u5220\u9664\nstatic int dictGenericDelete(dict *d, const void *key, int nofree)\n{\n    unsigned int h, idx;\n    dictEntry *he, *prevHe;\n    int table;\n    if (d->ht&#91;0].size == 0) return DICT_ERR;\n    if (dictIsRehashing(d)) _dictRehashStep(d);\n    h = dictHashKey(d, key);\n    for (table = 0; table &lt;= 1; table++) {\n        idx = h &amp; d->ht&#91;table].sizemask;\n        he = d->ht&#91;table].table&#91;idx];\n        prevHe = NULL;\n        while(he) {\n            if (dictCompareKeys(d, key, he->key)) {\n                if (prevHe)\n                    prevHe->next = he->next; \/\/ \u5982\u679c\u6709\u4e0a\u4e00\u4e2a\u8282\u70b9,\u90a3\u4e48\u4e0a\u4e00\u4e2a\u8282\u70b9\u7b49\u4e8e\u4e0b\u4e00\u4e2a\u8282\u70b9\n                else\n                    d->ht&#91;table].table&#91;idx] = he->next; \/\/ \u7b2c\u4e00\u4e2a\u8282\u70b9\u5c31\u662f\u8981\u5220\u9664\u7684,\u8bb0\u5f55\u4ece\u4e0b\u4e00\u4e2a\u8282\u70b9\u5f00\u59cb\n                if (!nofree) {\n                    dictFreeKey(d, he);\n                    dictFreeVal(d, he);\n                }\n                zfree(he);\n                d->ht&#91;table].used--;\n                return DICT_OK;\n            }\n\n            prevHe = he;\n            he = he->next;\n        }\n        if (!dictIsRehashing(d)) break;\n    }\n    return DICT_ERR;\n}\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=398\">\u672c\u6587\u5730\u5740<\/a>\uff0chttps:\/\/www.ccagml.com\/?p=398<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u518d\u6765\u770b\u51e0\u4e2a\u5e38\u89c1\u547d\u4ee4\uff0c\u5206\u522b\u4e3adbsize\u3001exists\u3001del\u547d\u4ee4 \u57fa\u4e8e\u7248\u672c3.0.0\u7248\u672c,\u70b9\u51fb\u4e0b\u8f7dhttp<a href=\"https:\/\/www.ccagml.com\/?p=398\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">redis\u6e90\u7801\u4ecemain\u5f00\u59cb9<\/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\/398"}],"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=398"}],"version-history":[{"count":2,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions\/400"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}