charcoal/cache

Stash 缓存库的炭服务提供商

v5.0.0 2024-03-13 15:05 UTC

README

Cache 包提供了对 Stash 的集成,用于缓存昂贵的任务结果。

安装

composer require charcoal/cache

对于 Charcoal 项目,可以从配置文件中注册服务提供商

{
    "service_providers": {
        "charcoal/cache/service-provider/cache": {}
    }
}

概述

服务提供商

参数

  • cache/available-drivers:由该系统支持的已注册缓存驱动程序集合(通过 Stash\DriverList)。

服务

  • cache/config:缓存服务的配置对象。
    有关可用选项,请参阅 池配置
  • cache/drivers:使用 cache/available-drivers 的缓存驱动程序实例集合(作为服务容器)。
    这些驱动程序已预先配置
  • cache/builder:用于构建缓存池的 CacheBuilder 实例。
  • cache/drivercache 使用的 Stash 缓存驱动程序的引用。默认为 "memory"。
  • cache:使用 cache/drivercache/config.prefix 的 Stash 缓存池的主要实例。

配置

池配置

每个池都附带一组默认选项,可以单独覆盖。

use Charcoal\Cache\CacheConfig;
use Charcoal\Cache\ServiceProvider\CacheServiceProvider;

$container->register(new CacheServiceProvider());

$container['cache/config'] = new CacheConfig([
    'prefix' => 'foobar',
    'types'  => [ 'apc', 'memcache', 'redis' ],
]);

驱动程序配置

每个驱动程序都附带一组默认选项,可以单独覆盖。

—N/A—

用法

只需获取默认缓存池服务

$pool = $this->container->get('cache');

或自定义定义的缓存池

// Create a Stash pool with the Memcached driver and a custom namespace.
$pool1 = $this->container->get('cache/builder')->build('memcache', 'altcache');

// Create a custom Stash pool with the FileSystem driver and custom features.
$pool2 = $this->container->get('cache/builder')->build('file', [
    'namespace'  => 'mycache',
    'logger'     => $this->container->get('logger.custom_logger'),
    'pool_class' => \MyApp\Cache\Pool::class,
    'item_class' => \MyApp\Cache\Item::class,
]);

// Create a Stash pool with the "memory" cache driver.
$pool3 = new \Stash\Pool($container['cache/drivers']['memory']);

然后您可以直接使用缓存服务

// Get a Stash object from the cache pool.
$item = $pool->getItem("/user/{$userId}/info");

// Get the data from it, if any happens to be there.
$userInfo = $item->get();

// Check to see if the cache missed, which could mean that it either
// didn't exist or was stale.
if ($item->isMiss()) {
    // Run the relatively expensive code.
    $userInfo = loadUserInfoFromDatabase($userId);

    // Set the new value in $item.
    $item->set($userInfo);

    // Store the expensive code so the next time it doesn't miss.
    $pool->save($item);
}

return $userInfo;

有关使用缓存服务的更多信息,请参阅 Stash 文档

中间件

对于支持中间件的 PSR-7 应用程序,提供了 CacheMiddleware。中间件将 HTTP 响应体和头部保存到 PSR-6 缓存池 中,并在仍然有效的情况下返回缓存响应。

如果您正在使用 charcoal/app,您可以通过应用程序配置集添加中间件

"middlewares": {
    "charcoal/cache/middleware/cache": {
        "active": true,
        "methods": [ "GET", "HEAD" ]
    }
}

否则,例如使用 Slim

use Charcoal\Cache\Middleware\CacheMiddleware;
use Slim\App;
use Stash\Pool;

$app = new App();

// Register middleware
$app->add(new CacheMiddleware([
    'cache'   => new Pool(),
    'methods' => [ 'GET', 'HEAD' ],
]));

中间件附带一组默认选项,可以单独覆盖。

默认情况下

除非

  1. 请求方法不是 GET
  2. 请求 URI 路径以 /admin… 开头
  3. 请求 URI 包含查询字符串
  4. 响应不是 OK(200)

忽略查询字符串

如果查询字符串不会影响服务器的响应,您可以通过忽略所有查询参数来允许请求缓存。

"ignored_query": "*"

或者部分查询参数

"ignored_query": [ "sort", "theme" ]

助手

CachePoolAwareTrait

CachePoolAwareTrait 提供了一种便利的方式,以避免重复/样板代码。它只是设置和获取 \Psr\Cache\CacheItemPoolInterface 实例。

使用 setCachePool() 分配缓存池,并使用 cachePool() 获取它。

这两种方法都是受保护的;这个特性没有公开的接口。

资源