Memcached::decrement

(PECL memcached >= 0.1.0)

Memcached::decrement减小数值元素的值

说明

public Memcached::decrement(
    string $key,
    int $offset = 1,
    int $initial_value = 0,
    int $expiry = 0
): int|false

Memcached::decrement() 减小数值元素的值,减小多少由参数 offset 决定。如果元素的值不是数值,则会导致错误。如果减小后的值小于 0,则新值会设为 0。如果键不存在,Memcached::decrement() 会将元素设置为 initial_value 参数。

参数

key

将要减小值的元素的 key。

offset

要减少的元素值要减少的数量。

initial_value

如果 key 不存在的时候设置到元素的值。

expiry

设置的元素过期时间。

返回值

成功时返回元素的新值, 或者在失败时返回 false

示例

示例 #1 Memcached::decrement() 示例

<?php
$m
= new Memcached();
$m->addServer('localhost', 11211);

$m->set('counter', 5);
$n = $m->decrement('counter');
var_dump($n);

$n = $m->decrement('counter', 10);
var_dump($n);

var_dump($m->get('counter'));

$m->set('counter', 'abc');
$n = $m->increment('counter');
// ^ will fail due to item value not being numeric
var_dump($n);
?>

以上示例会输出:

int(4)
int(0)
int(0)
bool(false)

参见

add a note

User Contributed Notes 3 notes

up
18
Pramod Patil
9 years ago
decrement will not change TTL of the stored key/value.
up
11
Pramod Patil
9 years ago
Found possible bug :
decrement fails and returns -1 when memcached::OPT_BINARY_PROTOCOL is set to true.

tested on PECL Memcached 2.1.0 and libmemcached version 1.0.8
up
0
jbaginski
11 years ago
PECL memcached < 0.2.0

public int Memcached::decrement ( string $key [, int $offset = 1 ] )
To Top