跳到内容

PHP 文件缓存适配器

编辑此页

文件系统适配器 类似,此缓存实现将缓存条目写入磁盘,但与文件系统缓存适配器不同,PHP 文件缓存适配器作为原生 PHP 代码写入和读取这些缓存文件。例如,缓存值 ['my', 'cached', 'array'] 将写入类似于以下的缓存文件

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php return [

    // the cache item expiration
    0 => 9223372036854775807,

    // the cache item contents
    1 => [
        0 => 'my',
        1 => 'cached',
        2 => 'array',
    ],

];

注意

此适配器需要启用 opcache.enable php.ini 设置。由于缓存项作为原生 PHP 代码包含和解析,并且由于 OPcache 处理文件包含的方式,此适配器有可能比其他基于文件系统的缓存快得多。

警告

虽然它支持更新,并且因为它使用 OPcache 作为后端,但此适配器更适合主要追加的需求。在其他场景中使用它可能会导致定期重置 OPcache 内存,从而可能导致性能下降。

PhpFilesAdapter 可以选择性地提供命名空间、默认缓存生命周期和缓存目录路径作为构造函数参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;

$cache = new PhpFilesAdapter(

    // a string used as the subdirectory of the root cache directory, where cache
    // items will be stored
    $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 files are deleted)
    $defaultLifetime = 0,

    // the main cache directory (the application needs read-write permissions on it)
    // if none is specified, a directory is created inside the system temporary directory
    $directory = null
);

注意

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

这项工作,包括代码示例,根据 Creative Commons BY-SA 3.0 许可获得许可。
目录
    版本