PHP 8.5.0 Alpha 2 available for testing

Memcache::increment

memcache_increment

(PECL memcache >= 0.2.0)

Memcache::increment -- memcache_incrementIncrementa el valor de un elemento

Descripción

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

Memcache::increment() incrementa el valor de un elemento identificado por la clave key por el valor value. Si el elemento identificado por la clave key no es de tipo numérico y no puede ser convertido a número, el valor de este elemento será definido a value. Memcache::increment() no crea un elemento si no existe.

Nota:

No se debe utilizar memcache::increment() con elementos almacenados comprimidos. En este caso, la llamada a la función Memcache::get() fallará.

Parámetros

key

Clave del elemento a incrementar.

value

Incrementa el elemento por value.

Valores devueltos

Devuelve el valor del nuevo elemento en caso de éxito o false si ocurre un error.

Ejemplos

Ejemplo #1 Ejemplo con Memcache::increment()

<?php

/* API procedimental */
$memcache_obj = memcache_connect('memcache_host', 11211);
/* incrementación del contador en 2 */
$current_value = memcache_increment($memcache_obj, 'counter', 2);

/* API orientada a objetos */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
/* incrementación del contador en 3 */
$current_value = $memcache_obj->increment('counter', 3);

?>

Ver también

add a note

User Contributed Notes 5 notes

up
8
jay dot paroline at escapemg dot com
16 years ago
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.
up
2
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 get any hint that the key did not exist and still does not exist and that nothing was incremented.
up
0
perroazul64 at gmail dot com
14 years ago
When the key doesn't exist it may return either bool(false) or int(0) (I get different return values on different servers), so be careful if you check for something like ($memcache->increment($key) === false).
up
0
ian at blip dot fm
16 years ago
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.
up
-1
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, chk if its null, and if so, then ADD the variable.
To Top