downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Memcache::pconnect> <Memcache::getVersion
Last updated: Fri, 13 Nov 2009

view this page in

Memcache::increment

(PECL memcache >= 0.2.0)

Memcache::increment項目の値を増やす

説明

int Memcache::increment ( string $key [, int $value ] )

Memcache::increment() は、項目の値を value だけ増やします。 もし key に対応する値が数値ではなく、かつ 数値に変換できなかった場合は、その新しい値は value となります。 Memcache::increment() は、指定した項目が 存在しない場合に項目を作成することは ありません

注意: 圧縮して格納されている項目に対して Memcache::increment() を使用しないでください。 なぜなら、それ以降の Memcache::get() のコールが 失敗してしまうからです。

memcache_increment() 関数を使用することも可能です。

パラメータ

key

値を増やす項目のキー。

value

項目の値を value だけ増やします。 オプションのパラメータで、デフォルト値は 1 です。

返り値

項目の新しい値か、失敗した場合には FALSE を返します。

例1 Memcache::increment() の例

<?php

/* 手続き型の API */
$memcache_obj memcache_connect('memcache_host'11211);
/* カウンタを 2 増やします */
$current_value memcache_increment($memcache_obj'counter'2);

/* オブジェクト指向の API */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host'11211);
/* カウンタを 3 増やします */
$current_value $memcache_obj->increment('counter'3);

?>

参考



Memcache::pconnect> <Memcache::getVersion
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
Memcache::increment
ian at blip dot fm
30-Jun-2009 11:28
Be careful to use Memcache::decrement() and never Memcache::increment() with a negative value.

The check that prevents Memcache::decrement() from going negative is not in place with Memcache::increment(), so you can end up with a garbage integer on the order of 18 quintillion stored in place of the expected value.
jay dot paroline at escapemg dot com
13-May-2009 01:34
Instead of checking the value before incrementing, you can simply ADD it instead before incrementing each time. If it's already there, your ADD is ignored, and if it's not there, it's set.

If you add($memcacheKey, 0) and then increment($memcacheKey, 1) in that order, you avoid all possible race conditions. If two threads are running this code concurrently, you will always end up with your value being 2 no matter which order the threads execute in.
Anonymous
13-May-2009 01:16
Please note:
If the key does not exist, memcache does NOT return false (as you might expect) but 0.
You won't get any hint that the key did not exist and still does not exist and that nothing was incremented.
15-Jun-2005 07:54
if no variable exists, even if you specify an increment value, the result will be null.

if you're using this for a mutex, chk if its null, and if so, then ADD the variable.

Memcache::pconnect> <Memcache::getVersion
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites