zoukankan      html  css  js  c++  java
  • 【memcached】memcached中flags字段的作用

    我们一般只注意到memcached的数据结构是key,value,今天看memcached源代码的时候,盯上了flags,没看明白。后来问了一下同事,说PHP当中使用flags标记,标识memcached数据是否需要经过压缩处理。

    例如:

    bool Memcache::add ( string $key, mixed $var [, int $flag [, int $expire]] )

    flag

    Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).

    看了一下memcached的协议,是这样定义一个item的:

    Each item sent by the server looks like this:

    VALUE <key> <flags> <bytes> [<cas unique>]
    <data block>

    - <key> is the key for the item being sent

    - <flags> is the flags value set by the storage command

    - <bytes> is the length of the data block to follow, *not* including
    its delimiting

    - <cas unique> is a unique 64-bit integer that uniquely identifies
    this specific item.

    - <data block> is the data for this item.
    在memcached1.2.1之前为flags预留了16位,到了1.2.1以后预留了32位。对于服务器端而言,并不清楚你设置这些标记的作用。它并不知道你的数据是压缩过的,还是序列化存储的。

    其实这只是客户端自己的一种定义,我们看Pecl的memcached模块的源代码:

    #define MMC_SERIALIZED 1
    #define MMC_COMPRESSED 2

    if (flags & MMC_COMPRESSED) {
            unsigned long data_len;

            if (!mmc_compress(&data, &data_len, value, value_len TSRMLS_CC)) {
                /* mmc_server_seterror(mmc, "Failed to compress data", 0); */
                return -1;
            }

            /* was enough space saved to motivate uncompress processing on get */
            if (data_len < value_len * (1 - pool->min_compress_savings)) {
                value = data;
                value_len = data_len;
            }
            else {
                flags &= ~MMC_COMPRESSED;
                efree(data);
                data = NULL;
            }
        }

    当flags包含MMC_COMPRESSED就对数据进行压缩处理。

    也就是说,如果我们自己写一个memcached的client,也可以定义出更多的格式,json,xml或者别的。

    其它关于 flags的文章请看

    http://www.cnblogs.com/sunli/archive/2009/03/18/1415168.htm

  • 相关阅读:
    Django匆匆一眼却解答了多年疑惑
    2020年度总结,似乎没有什么大的长进,似乎也得到了一些收获
    Django搭建示例项目实战与避坑细节
    真香,理解记忆法学习Python基础语法
    如何让文科生5分钟写上Python
    Django官方为什么没有标准项目结构
    用PyCharm打个专业的招呼
    MySQL/MariaDB读写分离配置
    Mysql/Mariadb主从复制
    图解CentOS系统启动流程
  • 原文地址:https://www.cnblogs.com/bcphp/p/7411415.html
Copyright © 2011-2022 走看看