apcu_store
(PECL apcu >= 4.0.0)
apcu_store — 缓存一个变量到存储中
说明
缓存一个变量到存储中。
注意: 与 PHP 中常见的变量生命周期不同的是,通过 apcu_store() 存储的变量可以在多个 request 之间共享(直到该变量从 cache 中被删除)。
参数
key
-
使用此名称存储变量。
key
是唯一的,因此当多次使用同样的key
存储变量时,后一次会覆盖前一次的值。 var
-
被存储的变量
ttl
-
变量生存时间(Time To Live);被存储的
var
经过ttl
秒后,会从存储中被删除(下一次请求时)。如果没提供ttl
(或ttl
为0
),该变量会一直存在直到手动删除它,或者其他原因导致该变量从缓存中消失(清除,重启等等。)。 values
-
数组索引作为 key,数组值作为被存储的 var。
示例
示例 #1 apcu_store() 示例
<?php
$bar = 'BAR';
apcu_store('foo', $bar);
var_dump(apcu_fetch('foo'));
?>
以上示例会输出:
string(3) "BAR"
参见
- apcu_add() - 缓存一个新变量到存储中
- apcu_fetch() - Fetch a stored variable from the cache
- apcu_delete() - Removes a stored variable from the cache
+添加备注
用户贡献的备注 1 note
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.