PHP8 apcu_cas

2023-08-28 15:28 更新

(PECL apcu >= 4.0.0)

apcu_cas — 用新值更新舊值

說明

apcu_cas(string $key, int $old, int $new): bool

apcu_cas() 如果舊參數(shù)與當(dāng)前存儲的值與新參數(shù)的值匹配,則更新已存在的整數(shù)值。

參數(shù)

key

正在更新的值的key。

old

舊值(當(dāng)前存儲的值)。

new

要更新到的新值。

返回值

成功時返回 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() - 緩存一個變量到存儲中


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號