跳到内容

PHP 数组缓存适配器

编辑此页

此适配器是用于静态数据(例如,应用程序配置)的高性能缓存,经过优化并预加载到 OPcache 内存存储中。它适用于预热后主要为只读的任何数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;

// somehow, decide it's time to warm up the cache!
if ($needsWarmup) {
    // some static values
    $values = [
        'stats.products_count' => 4711,
        'stats.users_count' => 1356,
    ];

    $cache = new PhpArrayAdapter(
        // single file where values are cached
        __DIR__ . '/somefile.cache',
        // a backup adapter, if you set values after warm-up
        new FilesystemAdapter()
    );
    $cache->warmUp($values);
}

// ... then, use the cache!
$cacheItem = $cache->getItem('stats.users_count');
echo $cacheItem->get();

注意

此适配器需要开启 opcache.enable php.ini 设置。

此作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可协议获得许可。
目录
    版本