跳到内容

文件系统缓存适配器

编辑此页

对于那些无法在其环境中安装 APCuRedis 等工具的用户,此适配器可提供改进的应用程序性能。它将缓存项过期时间和内容存储为本地挂载文件系统上目录集合中的常规文件。

提示

通过利用临时的内存文件系统,例如 Linux 上的 tmpfs,或许多其他可用的 RAM 磁盘解决方案 之一,可以大大提高此适配器的性能。

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

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

$cache = new FilesystemAdapter(

    // 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
);

警告

文件系统 IO 的开销通常使此适配器成为较慢的选择之一。如果吞吐量至关重要,则建议使用内存适配器 (ApcuMemcachedRedis) 或数据库适配器 (Doctrine DBALPDO)。

注意

此适配器实现了 PruneableInterface,通过调用其 prune() 方法,可以手动修剪过期的缓存项

使用标签

为了使用基于标签的失效,您可以将适配器包装在 TagAwareAdapter 中,但使用专用的 FilesystemTagAwareAdapter 通常更有趣。由于标签失效逻辑是使用文件系统上的链接实现的,因此当使用基于标签的失效时,此适配器提供更好的读取性能

1
2
3
use Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter;

$cache = new FilesystemTagAwareAdapter();
本作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可获得许可。
目录
    版本