• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • Memcached::set()

    (PECL memcached >= 0.1.0)

    存储一个元素

    说明

    publicMemcached::set(string $key, mixed $value[,int $expiration]): bool

    Memcached::set()$value存储在一个memcached服务器上的$key下。$expiration参数用于控制值的过期时间。

    值可以是任何有效的非资源型php类型,因为资源类型不能被序列化存储。如果Memcached::OPT_COMPRESSION选项开启,序列化的值同样会被压缩存储。

    参数

    $key

    用于存储值的键名。

    $value

    存储的值。

    $expiration

    到期时间,默认为 0。更多信息请参见到期时间。

    返回值

    成功时返回TRUE,或者在失败时返回FALSE。如需要则使用Memcached::getResultCode()。

    范例

    Example #1 Memcached::set()示例

    <?php
    $m = new Memcached();
    $m->addServer('localhost', 11211);
    $m->set('int', 99);
    $m->set('string', 'a simple string');
    $m->set('array', array(11, 12));
    /* 'object'这个key将在5分钟后过期 */
    $m->set('object', new stdclass, time() + 300);
    var_dump($m->get('int'));
    var_dump($m->get('string'));
    var_dump($m->get('array'));
    var_dump($m->get('object'));
    ?>
    

    以上例程的输出类似于:

    int(99)
    string(15) "a simple string"
    array(2) {
      [0]=>
      int(11)
      [1]=>
      int(12)
    }
    object(stdClass)#1 (0) {
    }
    

    参见

    • Memcached::setByKey() Store an item on a specific server
    • Memcached::add() 向一个新的key下面增加一个元素
    • Memcached::replace() 替换已存在key下的元素
    @PeterN
    You said that this was wrong:
    $m->set('object', new stdclass, time() + 300);
    And instead this is the correct way:
    $m->set('object', new stdclass, 300);
    When actually, they are both correct. Expiration looks at the time given, if the number is less than or equal to 30 days(60 * 60 * 24 * 30 = 2,592,000), then it expires in that many seconds. If the number is greater than 30 days, then it expires at that UNIX time.
    In the example it shows:
    /* expire 'object' key in 5 minutes */
    $m->set('object', new stdclass, time() + 300);
    But this is wrong.
    It will not expire, at least, not for a long long time.
    So instead of time() + seconds, you use:
    $m->set('object', new stdclass, 300);
    And it will correctly expire after 5 minutes.
    Note: Using memcached 2.1.0 stable through PECL.
    Unlike Memcache, Memcached will not replace spaces in keys with underscores for you, and will instead send the key as provided which will cause an error that can result in keys and values being mismatched. You can avoid this problem by removing/replacing spaces before storage or setting the option Memcached::OPT_BINARY_PROTOCOL to true.
    00 = MEMCACHED_SUCCESS
    01 = MEMCACHED_FAILURE
    02 = MEMCACHED_HOST_LOOKUP_FAILURE // getaddrinfo() and getnameinfo() only
    03 = MEMCACHED_CONNECTION_FAILURE
    04 = MEMCACHED_CONNECTION_BIND_FAILURE // DEPRECATED see MEMCACHED_HOST_LOOKUP_FAILURE
    05 = MEMCACHED_WRITE_FAILURE
    06 = MEMCACHED_READ_FAILURE
    07 = MEMCACHED_UNKNOWN_READ_FAILURE
    08 = MEMCACHED_PROTOCOL_ERROR
    09 = MEMCACHED_CLIENT_ERROR
    10 = MEMCACHED_SERVER_ERROR // Server returns "SERVER_ERROR"
    11 = MEMCACHED_ERROR // Server returns "ERROR"
    12 = MEMCACHED_DATA_EXISTS
    13 = MEMCACHED_DATA_DOES_NOT_EXIST
    14 = MEMCACHED_NOTSTORED
    15 = MEMCACHED_STORED
    16 = MEMCACHED_NOTFOUND
    17 = MEMCACHED_MEMORY_ALLOCATION_FAILURE
    18 = MEMCACHED_PARTIAL_READ
    19 = MEMCACHED_SOME_ERRORS
    20 = MEMCACHED_NO_SERVERS
    21 = MEMCACHED_END
    22 = MEMCACHED_DELETED
    23 = MEMCACHED_VALUE
    24 = MEMCACHED_STAT
    25 = MEMCACHED_ITEM
    26 = MEMCACHED_ERRNO
    27 = MEMCACHED_FAIL_UNIX_SOCKET // DEPRECATED
    28 = MEMCACHED_NOT_SUPPORTED
    29 = MEMCACHED_NO_KEY_PROVIDED /* Deprecated. Use MEMCACHED_BAD_KEY_PROVIDED! */
    30 = MEMCACHED_FETCH_NOTFINISHED
    31 = MEMCACHED_TIMEOUT
    32 = MEMCACHED_BUFFERED
    33 = MEMCACHED_BAD_KEY_PROVIDED
    34 = MEMCACHED_INVALID_HOST_PROTOCOL
    35 = MEMCACHED_SERVER_MARKED_DEAD
    36 = MEMCACHED_UNKNOWN_STAT_KEY
    37 = MEMCACHED_E2BIG
    38 = MEMCACHED_INVALID_ARGUMENTS
    39 = MEMCACHED_KEY_TOO_BIG
    40 = MEMCACHED_AUTH_PROBLEM
    41 = MEMCACHED_AUTH_FAILURE
    42 = MEMCACHED_AUTH_CONTINUE
    43 = MEMCACHED_PARSE_ERROR
    44 = MEMCACHED_PARSE_USER_ERROR
    45 = MEMCACHED_DEPRECATED
    46 = MEMCACHED_IN_PROGRESS
    47 = MEMCACHED_SERVER_TEMPORARILY_DISABLED
    48 = MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE
    49 = MEMCACHED_MAXIMUM_RETURN /* Always add new error code before */
    11 = MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE = MEMCACHED_ERROR
    The method correctly returns false if you set the value to false. This means that in order to have proper fault checking mechanism in place you need to check the result code.
    <?php
    $Memcached = new Memcached();
    $Memcached->addServer('localhost', 11211);
    $Memcached->set('key', false);
    var_dump($Memcached->get('key'));   // boolean false
    var_dump($Memcached->getResultCode()); // int 0 which is Memcached::RES_SUCCESS 
    ?>