CakeFest 2024: The Official CakePHP Conference

apcu_store

(PECL apcu >= 4.0.0)

apcu_store 缓存一个变量到存储中

说明

apcu_store(string $key, mixed $var, int $ttl = 0): bool
apcu_store(array $values, mixed $unused = NULL, int $ttl = 0): array

缓存一个变量到存储中。

注意: 与 PHP 中常见的变量生命周期不同的是,通过 apcu_store() 存储的变量可以在多个 request 之间共享(直到该变量从 cache 中被删除)。

参数

key

使用此名称存储变量。key 是唯一的,因此当多次使用同样的 key 存储变量时,后一次会覆盖前一次的值。

var

被存储的变量

ttl

变量生存时间(Time To Live);被存储的 var 经过 ttl 秒后,会从存储中被删除(下一次请求时)。如果没提供 ttl (或 ttl0 ),该变量会一直存在直到手动删除它,或者其他原因导致该变量从缓存中消失(清除,重启等等。)。

values

数组索引作为 key,数组值作为被存储的 var。

返回值

成功时返回 true, 或者在失败时返回 false。 第二种语法返回包含存储失败的 key 的数组。

示例

示例 #1 apcu_store() 示例

<?php
$bar
= 'BAR';
apcu_store('foo', $bar);
var_dump(apcu_fetch('foo'));
?>

以上示例会输出:

string(3) "BAR"

参见

add a note

User Contributed Notes 1 note

up
0
info at qmegas dot info
3 years ago
Be careful when updating same key with ttl set during same request. For example:
<?php
for ($i = 0; $i < 20; $i++) {
apcu_store('test', $i, 10);
sleep(1);
}
?>

After 10 seconds the key will become not available and won't be updated. Tested on Windows and Linux platforms. Not sure if it's a bug or undocumented behavior.
To Top