Memcache::increment
(PECL memcache >= 0.2.0)
Memcache::increment — Increment item's value
Описание
$key
[, int $value
= 1
] )
Memcache::increment() increments 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 change its value to value
.
Memcache::increment() does not
create an item if it doesn't already exist.
Also you can use memcache_increment() function.Замечание:
Do not use Memcache::increment() with items that have been stored compressed because subsequent calls to Memcache::get() will fail.
Список параметров
-
key
-
Key of the item to increment.
-
value
-
Increment the item by
value
.
Возвращаемые значения
Returns new items value on success или FALSE
в случае возникновения ошибки.
Примеры
Пример #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);
?>
Смотрите также
- Memcache::decrement() - Decrement item's value
- Memcache::replace() - Replace value of the existing item
- Функция Memcache::add() - Add an item to the server
- Функция Memcache::addServer() - Add a memcached server to connection pool
- Функция Memcache::close() - Close memcached server connection
- Функция Memcache::connect() - Open memcached server connection
- Функция Memcache::decrement() - Decrement item's value
- Функция Memcache::delete() - Delete item from the server
- Функция Memcache::flush() - Flush all existing items at the server
- Функция Memcache::get() - Retrieve item from the server
- Функция Memcache::getExtendedStats() - Get statistics from all servers in pool
- Функция Memcache::getServerStatus() - Returns server status
- Функция Memcache::getStats() - Get statistics of the server
- Функция Memcache::getVersion() - Return version of the server
- Функция Memcache::increment() - Increment item's value
- Функция Memcache::pconnect() - Open memcached server persistent connection
- Функция Memcache::replace() - Replace value of the existing item
- Функция Memcache::set() - Store data at the server
- Функция Memcache::setCompressThreshold() - Enable automatic compression of large values
- Функция Memcache::setServerParams() - Changes server parameters and status at runtime
Коментарии
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.
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.
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.
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.
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).