Memcache::increment

memcache_increment

(PECL memcache >= 0.2.0)

Memcache::increment -- memcache_increment β€” Π˜Π½ΠΊΡ€Π΅ΠΌΠ΅Π½Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ элСмСнта

ОписаниС

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

ΠœΠ΅Ρ‚ΠΎΠ΄ Memcache::increment() ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ элСмСнта Π½Π° Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Ρƒ value. Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ элСмСнта измСнится Π½Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π° value, Ссли элСмСнт с ΠΊΠ»ΡŽΡ‡ΠΎΠΌ key Π½Π΅ числовой ΠΈ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ элСмСнта Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Ρ‚ΡŒ Π² число. ΠœΠ΅Ρ‚ΠΎΠ΄ Memcache::increment() Π½Π΅ создаёт элСмСнт, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π΅Ρ‰Ρ‘ Π½Π΅ содСрТится Π² кСшС.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Π›ΡƒΡ‡ΡˆΠ΅ Π½Π΅ Π²Ρ‹Π·Ρ‹Π²Π°Ρ‚ΡŒ ΠΌΠ΅Ρ‚ΠΎΠ΄ Memcache::increment() с элСмСнтом, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ сохранили Π² сТатом Π²ΠΈΠ΄Π΅, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΡ‡Π΅Ρ€Π΅Π΄Π½ΠΎΠΉ Π²Ρ‹Π·ΠΎΠ² ΠΌΠ΅Ρ‚ΠΎΠ΄Π° Memcache::get() Π·Π°Π²Π΅Ρ€ΡˆΠΈΡ‚ΡΡ ошибкой.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

key
ΠšΠ»ΡŽΡ‡ элСмСнта, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ трСбуСтся ΠΈΠ½ΠΊΡ€Π΅ΠΌΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ.
value
ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ value опрСдСляСт шаг увСличСния элСмСнта.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

ΠœΠ΅Ρ‚ΠΎΠ΄ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π½ΠΎΠ²ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ элСмСнта, Ссли выполнился ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ 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::decrement() - Π”Π΅ΠΊΡ€Π΅ΠΌΠ΅Π½Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ элСмСнта
  • Memcache::replace() - ЗамСняСт Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ элСмСнта кСша
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 5 notes

up
8
jay dot paroline at escapemg dot com ΒΆ
17 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 ΒΆ
17 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 ΒΆ
15 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
-2
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.