The lowest byte of the int is reserved for pecl/memcache internal usage (e.g. to indicate compression and serialization status).
Memcache::set
(PECL memcache:0.2-2.1.2)
Memcache::set — データをサーバに格納する
説明
Memcache::set() は、キー key に var という値を 関連付け、memcached サーバに格納します。パラメータ expire は、データの有効期限を秒単位で指定します。もし 0 を指定した場合は その項目が期限切れになることはありません (これは、その項目のデータが memcached サーバ上にずっと残り続けることを保証するものではありません。 他の項目をキャッシュするための場所を確保するためにサーバから 削除されてしまうこともあります)。 (zlib を使用して) その場でのデータの圧縮を行いたい場合は、 flag の値として、定数 MEMCACHE_COMPRESSED を指定します。
memcache_set() 関数を使用することも可能です。注意: リソース型の変数 (たとえばファイル記述子や接続記述子など) はキャッシュに保存できないことを覚えておきましょう。これは、 シリアライズした状態ではそれらのデータを適切に表すことが できないためです。
パラメータ
- key
-
項目に関連付けられたキー。
- var
-
格納する値。文字列および整数値はそのままの形式で、それ以外の型は シリアライズされて格納されます。
- flag
-
項目を圧縮して格納する場合に MEMCACHE_COMPRESSED を使用します (zlib を使用します)。
- expire
-
項目の有効期限。ゼロの場合は有効期限なし (いつまでも有効) となります。Unix タイムスタンプ形式、あるいは現在からの 秒数で指定することが可能ですが、後者の場合は秒数が 2592000 (30 日) を超えることはできません。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
例
例1 Memcache::set() の例
<?php
/* 手続き型の API */
/* memcached サーバに接続します */
$memcache_obj = memcache_connect('memcache_host', 11211);
/*
キー 'var_key' の項目の値を設定します。
flag の値として 0 を使用し、圧縮は使用しません。
有効期限は 30 秒です。
*/
memcache_set($memcache_obj, 'var_key', 'some variable', 0, 30);
echo memcache_get($memcache_obj, 'var_key');
?>
例2 Memcache::set() の例
<?php
/* オブジェクト指向の API */
$memcache_obj = new Memcache;
/* memcached サーバに接続します */
$memcache_obj->connect('memcache_host', 11211);
/*
キー 'var_key' に対応する値を設定します。その際、データの圧縮を行います。
有効期限は 50 秒です。
*/
$memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
echo $memcache_obj->get('var_key');
?>
Memcache::set
14-Apr-2008 11:45
09-Apr-2008 11:07
I ran into problems using the MEMCACHE_COMPRESSED flag when storing small amounts of data, such as an integers.
For expample.
Memcache::set('integer', 123456, MEMCACHE_COMPRESSED);
//would return true
Memcache::get('integer');
//would return false
This problem went away when I removed the MEMCACHE_COMPRESSED flag for values that were small.
19-Jul-2007 05:20
This is just two minor things about memcache that might not be perfectly clear, the limits on key and data sizes and what happen to flags in the memcache protocol.
* There is a max key size of 250 anything bigger gets truncated. There is also a (1MB - 42 bytes) limit on the data.
* In the memcache protocol there is a 16bit, 32bit in newer version, flag that you can set to whatever you want because memcache doesn't do anything with the flags. The php api doesn't let you get the flags because php uses the flags for php's own use such as "MEMCACHE_COMPRESSED" and I decided to test if it was doing something because it wasn't part of the memcache protocol.
<?php
$memcache = new Memcache();
$memcache->connect("127.0.0.1", 11211);
// Since memcache truncates the keys at 250 bytes both the get "250 a's" and "251 a's" will find the key in the cache
echo "*** Truncate key test ***<br>";
echo "set 251: " . ($memcache->set(str_repeat("a", 251), "value", 0, 1) ? "t" : "f") . "<br>";
echo "get 249: " . (($ret = $memcache->get(str_repeat("a", 249))) !== false ? "'$ret'" : "f") . "<br>";
echo "get 250: " . (($ret = $memcache->get(str_repeat("a", 250))) !== false ? "'$ret'" : "f") . "<br>";
echo "get 251: " . (($ret = $memcache->get(str_repeat("a", 251))) !== false ? "'$ret'" : "f") . "<br>";
echo "delete: " . ($memcache->delete(str_repeat("a", 250)) ? "t" : "f") . "<br><br>";
echo "*** Compress value test ***<br>";
echo "set 1024*1024-42: " . ($memcache->set("test", str_repeat("a", 1024*1024-42), 0, 1) ? "t" : "f") . "<br>";
echo "set 1024*1024-41: " . ($memcache->set("test", str_repeat("a", 1024*1024-41), 0, 1) ? "t" : "f") . "<br>";
echo "set 1024*1024 compressed: " . ($memcache->set("test", str_repeat("a", 1024*1024), MEMCACHE_COMPRESSED, 1) ? "t" : "f") . "<br>";
echo "delete: " . ($memcache->delete("test") ? "t" : "f") . "<br>";
$memcache->close();
?>
Output:
*** Truncate key test ***
set 251: t
get 249: f
get 250: 'value'
get 251: 'value'
delete: t
*** Compress value test ***
set 1024*1024-42: t
set 1024*1024-41: f
set 1024*1024 compressed: t
delete: t
