阅读memcached代码

来源:互联网 发布:数组的长度是多少 编辑:程序博客网 时间:2024/05/13 08:22

cache_lock锁保护对象: item_xx, assoc_xx相关的操作。 即保护hashtable的访问

 

item会链接到两个位置,

1. hashtable,这是为了方便查询

2. heads和tails(items.c中的全局变量),这两个是用来做LRU使用的。

 

head和tail作为一个队列的头和尾。 head总是指向最新添加的item, tails指向的是最久的那个item。head和tail直接使用双向链表链接。

 

item通过h_next域指向hastable,而next和prev域,用来将自己链接到heads和tails中。

 

item的数据结构如下:

/**
 * 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 */
    void * end[];
    /* 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;

 

 

原创粉丝点击