- PHP 手册
- 函数参考
- 影响 PHP 行为的扩展
- APCu
- APCu 函数
apcu_cas
(PECL apcu >= 4.0.0)
apcu_cas — Updates an old value with a new value
说明
apcu_cas(string$key
, int $old
, int $new
): bool
apcu_cas() updates an already existing integer value if the
old
parameter matches the currently stored value
with the value of the new
parameter.
参数
-
key
-
The key of the value being updated.
-
old
-
The old value (the value currently stored).
-
new
-
The new value to update to.
返回值
成功时返回 true
, 或者在失败时返回 false
。
范例
示例 #1 apcu_cas() example
<?php
apcu_store('foobar', 2);
echo '$foobar = 2', PHP_EOL;
echo '$foobar == 1 ? 2 : 1 = ', (apcu_cas('foobar', 1, 2) ? 'ok' : 'fail'), PHP_EOL;
echo '$foobar == 2 ? 1 : 2 = ', (apcu_cas('foobar', 2, 1) ? 'ok' : 'fail'), PHP_EOL;
echo '$foobar = ', apcu_fetch('foobar'), PHP_EOL;
echo '$f__bar == 1 ? 2 : 1 = ', (apcu_cas('f__bar', 1, 2) ? 'ok' : 'fail'), PHP_EOL;
apcu_store('perfection', 'xyz');
echo '$perfection == 2 ? 1 : 2 = ', (apcu_cas('perfection', 2, 1) ? 'ok' : 'epic fail'), PHP_EOL;
echo '$foobar = ', apcu_fetch('foobar'), PHP_EOL;
?>
以上例程的输出类似于:
$foobar = 2 $foobar == 1 ? 2 : 1 = fail $foobar == 2 ? 1 : 2 = ok $foobar = 1 $f__bar == 1 ? 2 : 1 = fail $perfection == 2 ? 1 : 2 = epic fail $foobar = 1
参见
- apcu_dec() - Decrease a stored number
- apcu_store() - 缓存一个变量到存储中
User Contributed Notes 1 note
up down -5 Anonymous ¶4 years ago
The output in the example says:
$f__bar == 1 ? 2 : 1 = fail
but in reality it should be:
$f__bar == 1 ? 2 : 1 = ok
the first time the code is ran as the cache is empty and apcu_cas allows the key to be inserted.
add a note
官方地址:https://www.php.net/manual/en/function.apcu-cas.php