查詢

apcu_store()函式—用法及示例

「 快取一個變數到儲存中 」


函式用法:apcu_store() 函式用於將資料儲存到 APCu 快取中。

語法:bool apcu_store(string $key, mixed $var [, int $ttl = 0 ])

引數:

  • $key: 儲存的資料的鍵名,必須是一個字串。
  • $var: 儲存的資料值,可以是任何可序列化的 PHP 資料型別。
  • $ttl: [可選] 資料的過期時間,以秒為單位。預設值為 0,表示永不過期。

返回值:

  • 如果儲存成功,則返回 true。
  • 如果儲存失敗,則返回 false。

示例 1:

// 儲存一個字串
$data = 'Hello, World!';
apcu_store('message', $data);

// 輸出儲存的值
echo apcu_fetch('message'); // 輸出:Hello, World!

示例 2:

// 儲存一個陣列
$data = array('apple', 'banana', 'cherry');
apcu_store('fruits', $data, 3600); // 儲存 1 小時後過期

// 輸出儲存的值
print_r(apcu_fetch('fruits')); // 輸出:Array ( [0] => apple [1] => banana [2] => cherry )

示例 3:

// 儲存一個物件
class Person {
  public $name;
  function __construct($name) {
    $this->name = $name;
  }
}

$person = new Person('John Doe');
apcu_store('myperson', $person);

// 輸出儲存的值
$obj = apcu_fetch('myperson');
echo $obj->name; // 輸出:John Doe

請注意,APCu 擴充套件必須在 PHP 中啟用才能使用該函式。另外,APCu 快取僅適用於單個 PHP 程序,不適用於共享快取。

補充糾錯
上一個函式: apcu_sma_info()函式
下一個函式: AppendIterator::append()函式
熱門PHP函式
分享連結