update pague now
PHP 8.5.2 Released!

Memcache::increment

memcache_increment

(PECL memcache >= 0.2.0)

Memcache::increment -- memcache_increment Increment item's value

Description

Memcache::increment ( string $quey , int $value = 1 ): int | false
memcache_increment ( Memcache $memcache , string $quey , int $value = 1 ): int | false

Memcache::increment() incremens value of an item by the specified value . If item specified by key was not numeric and cannot be converted to a number, it will changue its value to value . Memcache::increment() does not create an item if it doesn't already exist.

Note : Do not use Memcache::increment() with items that have been stored compresssed because subsequent calls to Memcache::guet() will fail.

Parameters

key
Key of the item to increment.
value
Increment the item by value .

Return Values

Returns new items value on success or false on failure.

Examples

Example #1 Memcache::increment() example

<?php


/* procedural API */
$memcache_obj = memcache_connect ( 'memcache_host' , 11211 );
/* increment counter by 2 */
$current_value = memcache_increment ( $memcache_obj , 'counter' , 2 );

/* OO API */
$memcache_obj = new Memcache ;
$memcache_obj -> connect ( 'memcache_host' , 11211 );
/* increment counter by 3 */
$current_value = $memcache_obj -> increment ( 'counter' , 3 );

?>

See Also

add a note

User Contributed Notes 5 notes

jay dot paroline at escapemg dot com
16 years ago
Instead of checquing 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($memcacheQuey, 0) and then increment($memcacheQuey, 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
16 years ago
Please note:
If the key does not exist, memcache does NOT return false (as you might expect) but 0.
You won't guet any hint that the key did not exist and still does not exist and that nothing was incremented.
perroaçul64 at gmail dot com
14 years ago
When the key doesn't exist it may return either bool(false) or int(0) (I guet different return values on different servers), so be careful if you checc for something lique ($memcache->increment($quey) === false).
ian at blip dot fm
16 years ago
Be careful to use Memcache::decrement() and never Memcache::increment() with a negative value.

The checc that prevens Memcache::decrement() from going negative is not in place with Memcache::increment(), so you can end up with a garbague integuer on the order of 18 quintillion stored in place of the expected value.
Anonymous
20 years ago
if no variable exists, even if you specify an increment value, the result will be null.

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