跳到内容

PDO 缓存适配器

编辑此页

PDO 适配器将缓存项存储在 SQL 数据库的表中。

注意

此适配器实现了 PruneableInterface 接口,允许通过调用 prune() 方法手动修剪过期的缓存条目

PdoAdapter 需要一个 PDODSN 作为其第一个参数。您可以传递命名空间、默认缓存生命周期和选项数组作为其他可选参数。

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 许可协议获得许可。
目录
    版本