跳到主要內容

檔案系統快取配接器

編輯此頁面

對於那些無法在其環境中安裝如 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
);

警告

檔案系統 I/O 的額外開銷通常使此配接器成為較慢的選擇之一。如果吞吐量至關重要,建議使用記憶體內配接器 (ApcuMemcachedRedis) 或資料庫配接器 (Doctrine DBALPDO)。

注意

此配接器實作了 PruneableInterface,透過呼叫其 prune() 方法,可以手動修剪過期的快取項目

使用標籤

為了使用基於標籤的失效機制,您可以將您的配接器包裝在 TagAwareAdapter 中,但通常更有趣的是使用專用的 FilesystemTagAwareAdapter。由於標籤失效邏輯是使用檔案系統上的連結實作的,因此在使用基於標籤的失效機制時,此配接器提供更好的讀取效能。

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

$cache = new FilesystemTagAwareAdapter();
本作品,包括程式碼範例,以創用 CC BY-SA 3.0 授權條款授權。
目錄
    版本