sin square / silex-doctrine-cache-provider
适用于Silex框架的Doctrine缓存提供者
v1.0.9
2017-07-19 14:15 UTC
Requires
- php: ~5.6|~7.0
- doctrine/cache: ^1.0
- pimple/pimple: ~3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- phpunit/phpunit: ^5.7
README
安装
使用composer
{ "require": { "sinsquare/silex-doctrine-cache-provider": "1.*" } }
使用
您可以使用该提供者与Pimple或Silex一起使用。
首先,您需要创建缓存配置,然后注册DoctrineCacheServiceProvider。
<?php use SinSquare\Cache\DoctrineCacheServiceProvider; $container = new Container(); $container['doctrine.cache.options'] = array( 'providers' => array( 'cache_name_1' => array( 'type' => 'void', ), ), ); $container->register(new DoctrineCacheServiceProvider());
要访问缓存,您可以使用$container['doctrine.caches'][]或$container['doctrine.cache.<cache_name>']。
<?php $container['doctrine.cache.options'] = array( 'providers' => array( 'cache_name_1' => array( 'type' => 'void', ), ), ); $container->register(new DoctrineCacheServiceProvider()); $cache = $container['doctrine.caches']['cache_name_1']; //OR $cache = $container['doctrine.caches.cache_name_1'];
选项
可用的缓存类型
APC
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'apc' ), ), );
APCu
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'apcu' ), ), );
数组
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'array' ), ), );
链
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'chain', 'caches' => array('cache_1', 'cache_2') ), 'cache_1' => array( 'type' => 'array' ), 'cache_2' => array( 'type' => 'apc' ), ), );
文件系统
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'filesystem', 'directory' => 'dir', 'extension' => 'value' //optional 'umask' => 'value' //optional ) ), );
文件系统
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'filesystem', 'directory' => 'dir', 'extension' => 'value' //optional 'umask' => 'value' //optional ) ), );
PHP文件
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'phpfile', 'directory' => 'dir', 'extension' => 'value' //optional 'umask' => 'value' //optional ) ), );
Memcache
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'memcache', 'host' => 'host', 'port' => 'port' ) ), );
Memcached
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'memcached', 'host' => 'host', 'port' => 'port' ) ), );
空
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'void' ) ), );
创建自定义缓存
要创建新的缓存提供者,您需要执行以下操作
- 创建新的CacheType
- 定义$container['doctrine.cache.factory.']
- 在配置中使用新提供者
创建新的缓存类型
<?php namespace Your\Name\Space; use Doctrine\Common\Cache\CacheProvider; class MyCustomCache extends CacheProvider { //class body with the required methods and functionality }
创建一个工厂
//you have to define this BEFORE you get a new cache, preferably before registering the provider $container['doctrine.cache.factory.customcache'] = $container->protect(function ($cacheOptions) use ($container) { $namespace = $cacheOptions["namespace"]; // return new MyCustomCache(); });
使用它
$container['doctrine.cache.options'] = array( 'providers' => array( 'cache' => array( 'type' => 'customcache' ) ), ); //getting the cache is the same as before