Memcached 项开销

tintown 阅读:32 2024-08-20 17:24:36 评论:0

有人知道每个项目需要多少开销吗?
当我插入 1 个字节的键时,'stats' 命令显示每个项目消耗了 65 个字节。

这是预期的和固定的吗?

谢谢,
彼得

请您参考如下方法:

是的,Memcache 自己的数据结构每项占用超过 50 字节。这在一定程度上取决于您的数据和 key ,因此在 64 位机器上假设 60 多个字节。

查看memcache的代码就可以看到:https://github.com/memcached/memcached/blob/master/memcached.h

以下是构成项目的内容:

/** 
 * Structure for storing items within memcached. 
 */ 
typedef struct _stritem { 
    struct _stritem *next; 
    struct _stritem *prev; 
    struct _stritem *h_next;    /* hash chain next */ 
    rel_time_t      time;       /* least recent access */ 
    rel_time_t      exptime;    /* expire time */ 
    int             nbytes;     /* size of data */ 
    unsigned short  refcount; 
    uint8_t         nsuffix;    /* length of flags-and-length string */ 
    uint8_t         it_flags;   /* ITEM_* above */ 
    uint8_t         slabs_clsid;/* which slab class we're in */ 
    uint8_t         nkey;       /* key length, w/terminating null and padding */ 
    /* this odd type prevents type-punning issues when we do 
     * the little shuffle to save space when not using CAS. */ 
    union { 
        uint64_t cas; 
        char end; 
    } data[]; 
    /* if it_flags & ITEM_CAS we have 8 bytes CAS */ 
    /* then null-terminated key */ 
    /* then " flags length\r\n" (no terminating null) */ 
    /* then data with terminating \r\n (no terminating null; it's binary!) */ 
} item; 

有 3 个指针,在 64 位机器上总和为 3 * 8 = 24 字节。有两个时间戳,应该是 32 位,所以它们总共有 8 个字节。下面的 int 应该是 8 字节,short 应该是 2,而 u_int8 应该是单个字节,所以它们总和为 14。

这总共有 46 个字节。

如果启用 CAS,则有一个 64 位的 CAS 值,加起来为 8 个字节:总共 54 个字节

现在变得困惑:以下是字符数据,其中标志和数据长度打印为小数(代码可以在 https://github.com/memcached/memcached/blob/master/items.c ,第 80 行找到)。鉴于标志为“0”,键为一个字符,数据为空,这使得:
  • 1 字节 key + 1 字节 0
  • 1 字节“空间”,1 字节标志“0”,1 字节“空间”,1 字节“0”
  • 2 字节“\r\n”(换行)
  • 2 字节用于终止数据

  • 这是另一个 10 字节,总共 64 字节用于最小可能的内存缓存项目大小(启用 CAS,您可以使用 -C 选项禁用它)。

    如果你得到 65 字节,也许你的客户设置了一个“10”或这样的标志。


    标签:Memcached
    声明

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    关注我们

    一个IT知识分享的公众号