locomotivemtl/charcoal-cache

Stash 缓存库的 Charcoal 服务提供商

0.2.3 2021-02-17 20:52 UTC

This package is auto-updated.

Last update: 2024-08-29 04:20:10 UTC


README

License Latest Stable Version Code Quality Coverage Status SensioLabs Insight Build Status

Charcoal 提供服务的 Stash 缓存库.

目录

安装

  1. 首选(也是唯一支持的)方法是使用 Composer

    $ composer require locomotivemtl/charcoal-cache
  2. 通过应用程序配置文件添加服务提供商并配置默认缓存服务

    "service_providers": {
        "charcoal/cache/service-provider/cache": {}
    },
    
    "cache": {
        "prefix": "foobar",
        "types": [ "apc", "memcache", "redis" ]
    }

    或通过服务容器

    $container->register(new \Charcoal\Cache\ServiceProvider\CacheServiceProvider());
    
    $container['cache/config'] = new \Charcoal\Cache\CacheConfig([
    	'prefix' => 'foobar',
    	'types'  => [ 'apc', 'memcache', 'redis' ],
    ]);

如果您使用 locomotivemtl/charcoal-app,则 CacheServiceProvider 将由 AppServiceProvider 自动注册。

依赖项

必需

PSR

  • PSR-3: 日志库的通用接口。由 Stash 支持。
  • PSR-6: 缓存库的通用接口。由 Stash 实现。
  • PSR-7: HTTP 消息的通用接口。由 CacheMiddleware 实现。
  • PSR-11: 依赖容器通用接口。由 Pimple 实现。

依赖项

服务提供商

参数

  • 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缓存池的主实例。

配置

池配置

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

驱动器配置

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

—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文档以获取有关使用缓存服务的更多信息。

中间件

CacheMiddleware可用于支持中间件的PSR-7应用程序。中间件将HTTP响应体和标题保存到PSR-6缓存池,并在有效的情况下返回该缓存响应。

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

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

否则,使用Slim,例如

$app = new \Slim\App();

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

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

默认情况下

所有HTTP响应都会被缓存,除非

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

忽略查询字符串

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

"ignored_query": "*"

或其中一些

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

辅助函数

CachePoolAwareTrait

CachePoolAwareTrait提供了一种方便的方法来避免重复/样板代码。它简单地设置并获取一个\Psr\Cache\CacheItemPoolInterface的实例。

使用setCachePool()分配缓存池,并使用cachePool()检索它。

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

开发

要安装开发环境

$ composer install

要运行脚本(phplint,phpcs和phpunit)

$ composer test

API 文档

开发依赖

编码风格

charcoal-cache模块遵循Charcoal编码风格

可以使用composer phpcs执行编码风格验证/强制执行。还有一个自动修复程序,可用composer phpcbf

致谢

许可证

  • Charcoal遵循MIT许可。有关详细信息,请参阅LICENSE文件。
  • Stash遵循BSD许可。有关详细信息,请参阅LICENSE文件。