PDO 快取配接器
PDO 配接器將快取項目儲存在 SQL 資料庫的表格中。
注意
此配接器實作了 PruneableInterface,允許透過呼叫 prune()
方法手動修剪過期的快取條目。
PdoAdapter 需要 PDO 或 DSN 作為其第一個參數。您可以將命名空間、預設快取生命週期和選項陣列作為其他可選參數傳遞。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
use Symfony\Component\Cache\Adapter\PdoAdapter;
$cache = new PdoAdapter(
// a PDO connection or DSN for lazy connecting through PDO
$databaseConnectionOrDSN,
// the string prefixed to the keys of the items stored in this cache
$namespace = '',
// the default lifetime (in seconds) for cache items that do not define their
// own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
// until the database table is truncated or its rows are otherwise deleted)
$defaultLifetime = 0,
// an array of options for configuring the database table and connection
$options = []
);
儲存值的表格會在首次呼叫 save() 方法時自動建立。您也可以透過在程式碼中呼叫 createTable() 方法來明確建立此表格。
提示
當傳遞資料來源名稱 (DSN) 字串(而不是資料庫連線類別實例)時,連線將在需要時延遲載入。
本作品,包括程式碼範例,依 Creative Commons BY-SA 3.0 授權條款授權。