locomotivemtl / charcoal-cache
Stash 缓存库的 Charcoal 服务提供商
Requires
- php: >=5.6.0 || >=7.0
- locomotivemtl/charcoal-config: ~0.8
- pimple/pimple: ^3.0
- psr/cache: ^1.0
- tedivm/stash: ~0.14
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^5.7 || ^6.5
- psr/log: ^1.0
- slim/http: ^0.3 || ^0.4
- squizlabs/php_codesniffer: ^3.0
README
目录
安装
-
首选(也是唯一支持的)方法是使用 Composer
$ composer require locomotivemtl/charcoal-cache
-
通过应用程序配置文件添加服务提供商并配置默认缓存服务
"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
自动注册。
依赖项
必需
- PHP 5.6+: 推荐使用 PHP 7。
- tedivm/stash: 符合 PSR-6 规范的缓存库。
- pimple/pimple: 符合 PSR-11 规范的服务容器和提供者库。
- locomotivemtl/charcoal-config: 用于配置缓存服务。
PSR
- PSR-3: 日志库的通用接口。由 Stash 支持。
- PSR-6: 缓存库的通用接口。由 Stash 实现。
- PSR-7: HTTP 消息的通用接口。由
CacheMiddleware
实现。 - PSR-11: 依赖容器通用接口。由 Pimple 实现。
依赖项
- locomotivemtl/charcoal-admin: Charcoal 应用程序的行政界面。
- locomotivemtl/charcoal-app: 适用于 Web 应用的 PSR-7 符合框架。
通过CacheMiddleware
缓存 HTTP 响应。 - locomotivemtl/charcoal-core: 收集、模型、元数据和数据库库。
用于缓存对象数据和模型元数据。
服务提供商
参数
- cache/available-drivers: 由本系统支持的已注册缓存驱动程序集合(通过
Stash\DriverList
)。
服务
- cache/config: 缓存服务的配置对象。
请参阅池配置以获取可用选项。 - cache/drivers:使用
cache/available-drivers
的缓存驱动实例集合(作为服务容器)。
这些驱动器已预先配置 - cache/builder:用于构建缓存池的
CacheBuilder
实例。 - cache/driver:
cache
使用的Stash缓存驱动程序的引用。默认为"memory"。 - cache:使用
cache/driver
和cache/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响应都会被缓存,除非
- 请求方法不是GET
- 请求URI路径以
/admin…
开头 - 请求URI包含查询字符串
- 响应不是OK(200)
忽略查询字符串
如果查询字符串不影响服务器的响应,您可以允许通过忽略所有查询参数或其中一些来缓存请求
"ignored_query": "*"
或其中一些
"ignored_query": [ "sort", "theme" ]
辅助函数
CachePoolAwareTrait
CachePoolAwareTrait
提供了一种方便的方法来避免重复/样板代码。它简单地设置并获取一个\Psr\Cache\CacheItemPoolInterface
的实例。
使用setCachePool()
分配缓存池,并使用cachePool()
检索它。
这两种方法都是受保护的;这个特性没有公开接口。
开发
要安装开发环境
$ composer install
要运行脚本(phplint,phpcs和phpunit)
$ composer test
API 文档
- 自动生成的
phpDocumentor
API文档在以下位置可用
https://locomotivemtl.github.io/charcoal-cache/docs/master/ - 自动生成的
apigen
API文档在以下位置可用
https://codedoc.pub/locomotivemtl/charcoal-cache/master/
开发依赖
编码风格
charcoal-cache模块遵循Charcoal编码风格
- PSR-1
- PSR-2
- PSR-4,因此自动加载由Composer提供。
- phpDocumentor注释。
- phpcs.xml.dist和.editorconfig用于编码规范。
可以使用
composer phpcs
执行编码风格验证/强制执行。还有一个自动修复程序,可用composer phpcbf
。