chh / cache-service-provider
为Silex应用程序提供易于使用的缓存,基于doctrine/cache包构建
Requires
- php: >=5.5.0
- doctrine/cache: ^1.0
- silex/silex: ^2.0
Requires (Dev)
- phpunit/phpunit: ^4.0
This package is not auto-updated.
Last update: 2024-09-20 21:22:13 UTC
README
这个Silex服务提供者使用Doctrine Cache库为Silex应用程序以及其他服务提供者提供cache
服务。
安装
使用composer安装
% composer require chh/cache-service-provider
用法
配置
对于应用级缓存,使用默认缓存,在cache.options
中设置default
键并指定缓存定义。
缓存定义是一个包含选项的数组,其中driver
是唯一必需的。数组中的所有其他选项都被视为驱动类构造函数的参数。
名为default
的缓存是容器cache
服务可用的缓存。
<?php use Doctrine\Common\Cache\ApcuCache; $app = new Silex\Application; $app->register(new \CHH\Silex\CacheServiceProvider, [ 'cache.options' => [ 'default' => [ 'driver' => ApcuCache::class, ], ], ]);
驱动程序名称可以是以下之一
- 实现
Doctrine\Common\Cache\Cache
接口的类的完全限定名称 - 例如"apc"这样的别名,然后被转换为
\Doctrine\Common\Cache\ApcCache
。 - 一个闭包,它返回一个实现
\Doctrine\Common\Cache\Cache
的对象。
此缓存可通过cache
服务访问,并提供一个Doctrine\Common\Cache\Cache
实例。
if ($app['cache']->contains('foo')) { echo $app['cache']->fetch('foo'), "<br>"; } else { $app['cache']->save('foo', 'bar'); }
要配置多个缓存,在cache.options
中定义它们作为额外的键。
$app->register(new \CHH\Silex\CacheServiceProvider, [ 'cache.options' => [ 'default' => ['driver' => ApcuCache::class], 'file' => [ 'driver' => 'filesystem', 'directory' => '/tmp/myapp', ], 'global' => [ 'driver' => function () use ($app) { $redis = new \Doctrine\Common\Cache\RedisCache; $redis->setRedis($app['redis']); return $redis; }, ], ], ]);
所有缓存(包括默认缓存)都作为caches
服务的键可用。
$app['caches']['file']->save('foo', 'bar'); $app['caches']['default']->save('bar', 'baz');
在扩展中使用的配置
扩展不应对其环境有任何假设。因此,最好大多数时候使用应用程序的默认缓存。但是,当您确实需要具有特定驱动的缓存时,则可以使用cache.factory
服务。
此工厂接受一个缓存选项数组,就像在cache.options
的每个键中一样,并返回一个适合Pimple的工厂。
<?php $app['caches']['myext'] = $app['cache.factory']([ 'driver' => 'filesystem', 'directory' => sys_get_temp_dir() . '/myext', ]);
扩展应使用前缀它们的缓存键以避免与用户指定的缓存ID冲突。
为此,缓存服务提供者提供了一个CacheNamespace
类。此类装饰任何\Doctrine\Common\Cache\Cache
,并在所有缓存操作中对键进行前缀。
对于具有通过setNamespace
方法内置命名空间支持的缓存,还有一个namespace
配置选项。
<?php use Pimple\Container; use Pimple\ServiceProviderInterface; use CHH\Silex\CacheServiceProvider\CacheNamespace; class ExampleServiceProvider extends ServiceProviderInterface { function register(Container $app) { // Check if Cache Service Provider is registered: if (isset($app['caches'])) { $app['caches'] = $app->extend(function ($caches) use ($app) { // Use a CacheNamespace to safely add keys to the default // cache. $caches['example'] = function () use ($caches) { return new CacheNamespace('example', $caches['default']); }; return $caches; }); } } }
此库还提供了一个cache.namespace
工厂,它返回一个适合直接分配给Pimple容器的闭包。使用此方法,上述代码可以进一步简化。
// Check if Cache Service Provider is registered: if (isset($app['caches'])) { $app['caches'] = $app->extend(function ($caches) use ($app) { // Use a CacheNamespace to safely add keys to the default cache $caches['example'] = $app['cache.namespace']('example'); return $caches; }); }
许可证
版权所有 (c) 2016 Christoph Hochstrasser
特此授予任何获得此软件和相关文档副本(“软件”)的人免费使用该软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许软件的接收者为此目的使用该软件,前提是符合以下条件
上述版权声明和本许可声明应包含在软件的任何副本或实质性部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任承担责任,无论是在合同、侵权或其他行为中产生的,与软件或软件的使用或其他交易有关。