ecfectus / cache
一个遵循PSR 6和PHP 7规范的缓存包
Requires
- php: >=7.0.0
- ecfectus/events: dev-master
- ecfectus/manager: dev-master
- psr/cache: ^1.0
- symfony/cache: dev-master
Requires (Dev)
- phpdocumentor/phpdocumentor: 2.*
- phpunit/phpunit: ^5.5.0
- predis/predis: ^1.1
This package is not auto-updated.
Last update: 2024-09-18 19:19:17 UTC
README
一个使用Symfony Cache并受Laravel启发的遵循PSR 6和PHP 7规范的缓存包。
通过使用我们的Manager包,缓存包提供了一种简单的方式访问多个缓存存储。
每个缓存存储都实现了PSR 6标准,以及我们自己的扩展CacheItemPoolInterface,其中包含一些更简单的访问缓存存储的方法。
包含的存储包括:
null array file apcu pdo redis phpfiles phparray chained
因为我们的包提供了管理功能,所以您只需将其添加到管理器中即可包含自己的存储。
$cache = new CacheManager($config); $cache->extend('mystore', function(){ return new MyStore();//must implement Ecfectus\Cache\CacheItemPoolInterface });
然后要使用任何存储,只需调用store
方法
$cache->store('mystore')->get('itemkey', 'default');
或要访问默认存储,只需在管理器上直接调用所需的方法
$cache->get('itemkey', 'default');
每个存储都提供了所有PSR6方法,以及我们额外的CacheItemPoolInterface方法,这些方法减少了CacheItem
对象的冗长性,并直接返回项
$item = $cache->getItem('key'); if($item->isHit()){ $value = $item->get(); }else{ $value = 'default'; } //the same as $value = $cache->get('key', 'default');
配置
要配置缓存管理器,只需提供一个包含要使用的默认存储的store
数组和存储的数组。以下为每个驱动程序的默认配置:
$config = [ 'store' => 'file', 'stores' => [ 'array' => [ 'driver' => 'array', 'lifetime' => 0, 'serialize' => true ], 'null' => [ 'driver' => 'null' ], 'file' => [ 'driver' => 'file', 'path' => null, 'namespace' => '', 'lifetime' => 0 ], 'phpfiles' => [ 'driver' => 'phpfiles', 'path' => null, 'namespace' => '', 'lifetime' => 0 ], 'phparray' => [ 'driver' => 'phparray', 'path' => null, 'fallback' => 'file' ], 'apcu' => [ 'driver' => 'apcu', 'namespace' => '', 'lifetime' => 0, 'version' => null ], 'pdo' => [ 'driver' => 'pdo', 'connection' => '', 'namespace' => '', 'lifetime' => 0, 'options' => [ 'db_table' => 'cache_items', 'db_id_col' => 'item_id', 'db_data_col' => 'item_data', 'db_lifetime_col' => 'item_lifetime', 'db_time_col' => 'item_time', 'db_username' => '', 'db_password' => '', 'db_connection_options' => [] ] ], 'redis' => [ 'driver' => 'redis', 'namespace' => '', 'lifetime' => 0 ], 'chain' => [ 'driver' => 'chain', 'stores' => [ 'array', 'file' ], 'lifetime' => 0 ] ] ];
存储数组中的每个键在通过store
方法访问时用于引用存储,并且每个存储必须提供一个driver
。所有内置驱动程序都已在上面引用。
需要连接Redis或PDO时,可以使用以下方法将它们添加到缓存管理器中。
$cache->setPdoConnection('connection_name', $pdoInstance); $cache->setRedisConnection('connection_name', $redisInstance);
现在,当需要一个connection
配置时,您可以通过引用connection_name
来使用它。
您可以将整个配置提供给CacheManager实例,只有内置的驱动程序类型将通过extend
添加,如果您的配置提供了一个不在包中包含的驱动程序,您将需要通过上述讨论的extend
方法自己添加它。