PHP8 apcu_store

2023-08-28 16:28 更新

(PECL apcu >= 4.0.0)

apcu_store — 緩存一個(gè)變量到存儲(chǔ)中

說(shuō)明

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

緩存一個(gè)變量到存儲(chǔ)中。

注意: 與 PHP 中常見的變量生命周期不同的是,通過(guò) apcu_store() 存儲(chǔ)的變量可以在多個(gè) request 之間共享(直到該變量從 cache 中被刪除)。

參數(shù)

key

使用此名稱存儲(chǔ)變量。key 是唯一的,因此當(dāng)多次使用同樣的 key 存儲(chǔ)變量時(shí),后一次會(huì)覆蓋前一次的值。

var

被存儲(chǔ)的變量

ttl

變量生存時(shí)間(Time To Live);被存儲(chǔ)的 var 經(jīng)過(guò) ttl 秒后,會(huì)從存儲(chǔ)中被刪除(下一次請(qǐng)求時(shí))。如果沒(méi)提供 ttl (或 ttl 為 0 ),該變量會(huì)一直存在直到手動(dòng)刪除它,或者其他原因?qū)е略撟兞繌木彺嬷邢Вㄇ宄?,重啟等等。)?/p>

values

數(shù)組索引作為 key,數(shù)組值作為被存儲(chǔ)的 var。

返回值

成功時(shí)返回 true, 或者在失敗時(shí)返回 false。 第二種語(yǔ)法返回包含存儲(chǔ)失敗的 key 的數(shù)組。

示例

示例 #1 apcu_store() 示例

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

以上示例會(huì)輸出:

string(3) "BAR"

參見

  • apcu_add() - 緩存一個(gè)新變量到存儲(chǔ)中
  • apcu_fetch() - Fetch a stored variable from the cache
  • apcu_delete() - Removes a stored variable from the cache


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)