thecodingmachine / stash-universal-module
Stash 的跨框架模块
Requires
- php: >=7.0
- container-interop/service-provider: ^0.4
- tedivm/stash: ~0.14.0
- thecodingmachine/common-factories: ^0.4
Requires (Dev)
- mnapoli/simplex: ~0.3.0
- phpunit/phpunit: ^5.0
- satooshi/php-coveralls: ^1.0
README
Stash 通用模块
此包将 Stash(PHP PSR-6 兼容的缓存库)集成到任何兼容 container-interop/service-provider 框架/容器的框架/容器中。
安装
composer require thecodingmachine/stash-universal-module
安装完成后,您需要将 TheCodingMachine\StashServiceProvider
注册到您的容器中。
如果您的容器支持 thecodingmachine/discovery 集成,则无需执行任何操作。否则,请参阅您的框架或容器的文档以了解如何注册 服务提供者。
简介
此服务提供者旨在创建一个 PSR-6 缓存池 Psr\Cache\CacheItemPoolInterface
实例。
开箱即用,该实例应使用合理的默认值即可使用。我们尽量让默认值对大多数开发者可用,同时为服务器提供最佳性能。
使用方法
use Psr\Cache\CacheItemPoolInterface $cachePool = $container->get(CacheItemPoolInterface::class); echo $cachePool->getItem('my_cached_value')->get();
默认值
默认情况下
- 默认缓存池是由以下组成的复合池
- 一个临时(内存中)驱动,用于快速访问已获取的值
- 一个 APC 驱动(或如果 APC 不可用,则使用文件系统驱动作为后备)
配置
重要:此服务提供者接受构造函数中的可选参数:一个“后缀”,如果需要多个不同实例,则可以使用该后缀。
use Psr\Cache\CacheItemPoolInterface // Let's assume we are using Simplex as our container $container = new Simplex\Container(); // Registers a default service provider $container->register(new StashServiceProvider()); // Registers another service provider for a shared memcache pool $container->register(new StashServiceProvider('shared')); // Lets configure the second service provider. $container['stash.shared.memcache.options'] = [ 'servers' => ['127.0.0.1', '11211'] ]; // Let's override the composite options to put an ephemeral driver and the memcache driver next. $container['stash.composite.options'] = function(ContainerInterface $container) { return [ $container->get(Ephemeral::class), $container->get(Memcache::class) ]; } $defaultCachePool = $container->get(CacheItemPoolInterface::class); //... do stuff with the default pool // The shared memcache pool can be accessed by suffixing the instance with ".shared". $sharedCachePool = $container->get(CacheItemPoolInterface::class.'.shared'); //... do stuff
当此服务提供者查找服务时,它将首先查找以包名称为前缀的服务,然后查找直接的服务。因此,如果此文档中提到 stash.apc.options
条目被使用,服务提供者将首先查找 thecodingmachine.stash-universal-module.stash.apc.options
,然后查找 stash.apc.options
。这允许您保持容器整洁(只有一个 stash.apc.options
条目),并且在有多个服务提供者使用该 stash.apc.options
条目并且您想传递不同值的情况下,您仍然可以只为该服务提供者编辑 thecodingmachine.stash-universal-module.stash.apc.options
。
预期值/服务
此 服务提供者 预期以下配置/服务可用
提供的服务
此 服务提供者 提供以下服务
扩展服务
此 服务提供者 不扩展任何服务。