CakeFest 2024: The Official CakePHP Conference

Memcache::replace

(PECL memcache >= 0.2.0)

Memcache::replace既存項目の値を置換する

説明

Memcache::replace(
    string $key,
    mixed $var,
    int $flag = ?,
    int $expire = ?
): bool

Memcache::replace() は、key に対応する既存項目の値を置換するために使用します。 指定したキーに対応する項目がない場合は、 Memcache::replace()false を返します。 それ以外の点では、Memcache::replace() の振る舞いは Memcache::set() と同じです。 memcache_replace() 関数を使用することも可能です。

パラメータ

key

項目に関連付けられたキー。

var

格納する値。文字列および整数値はそのままの形式で、それ以外の型は シリアライズされて格納されます。

flag

項目を圧縮して格納する場合に MEMCACHE_COMPRESSED を使用します (zlib を使用します)。

expire

項目の有効期限。ゼロの場合は有効期限なし (いつまでも有効) となります。Unix タイムスタンプ形式、あるいは現在からの 秒数で指定することが可能ですが、後者の場合は秒数が 2592000 (30 日) を超えることはできません。

戻り値

成功した場合に true を、失敗した場合に false を返します。

例1 Memcache::replace() の例

<?php

$memcache_obj
= memcache_connect('memcache_host', 11211);

/* 手続き型の API */
memcache_replace($memcache_obj, "test_key", "some variable", false, 30);

/* オブジェクト指向の API */
$memcache_obj->replace("test_key", "some variable", false, 30);

?>

参考

add a note

User Contributed Notes 1 note

up
10
adam.pippin [AT] ohmedia.ca
13 years ago
This page mentions that replace should be used rather than set, but gives no reason. Best information I could find was a comment by 'argyleblanket' on the set page. (http://www.php.net/manual/en/memcache.set.php#84032)

"Using set more than once for the same key seems to have unexpected results - it does not behave as a "replace," but instead seems to "set" more than one value for the same key. "get" may return any of the values.

This was tested on a multiple-server setup - behaviour may be different if you only have one server. "
To Top