alejoasotelo / joomla-cache-compat
Joomla 缓存包 - PSR 兼容性
1.2.1
2019-12-14 00:33 UTC
Requires
- php: ^5.3.10|~7.0
Requires (Dev)
- joomla/coding-standards: ~2.0@alpha
- joomla/test: ~1.0
- phpunit/phpunit: ^4.8.35|^5.4.3|~6.0
Suggests
- ext-apc: To use APC caching
- ext-memcached: To use Memcached caching
- ext-redis: To use Redis caching
- ext-wincache: To use WinCache caching
- ext-xcache: To use XCache caching
- joomla/registry: Registry can be used as an alternative to using an array for the package options.
Conflicts
- psr/cache: *
README
此缓存包基于 2013 年已接受的 PSR-6 版本。
已弃用
该 alejoasotelo/joomla-cache-compat
包已弃用,不再计划更新。
选项和通用用法
以下选项适用于所有缓存存储类型
- ttl - 存活时间。
use Joomla\Cache; $options = array( 'ttl' => 900, ); $cache = new Cache\Runtime($options); // Set a value in the cache. $cache->set('key', 'value'); // Get the value back. $value = $cache->get('key')->getValue(); // Remove the item from the cache. $cache->remove('key'); // Clear all the items from the cache. $cache->clear(); // Get multiple values from the cache at once. $values = $cache->getMultiple(array('key1', 'key2')); // Set multiple values from the cache at once. $values = $cache->setMultiple(array('key1' => 'value1', 'key2' => 'value2')); // Remove multiple values from the cache at once. $values = $cache->removeMultiple(array('key1', 'key2'));
缓存存储类型
以下存储类型受到支持。
Apc
use Joomla\Cache; $cache = new Cache\Apc;
文件
文件 缓存允许以下附加选项
- file.path - 缓存文件的存储路径。
- file.locking
use Joomla\Cache; $options = array( 'file.path' => __DIR__ . '/cache', ); $cache = new Cache\File($options);
Memcached
use Joomla\Cache; $cache = new Cache\Memcached;
无
use Joomla\Cache; $cache = new Cache\None;
运行时
use Joomla\Cache; $cache = new Cache\Runtime;
Wincache
use Joomla\Cache; $cache = new Cache\Wincache;
XCache
use Joomla\Cache; $cache = new Cache\XCache;
测试模拟
Cache
包提供了一个 PHPUnit 助手来模拟 Cache\Cache
对象或 Cache\Item
对象。您可以在测试类中包含以下方法的可选覆盖
Cache\Cache::get
:在您的测试类中添加名为mockCacheGet
的方法。如果省略,助手将返回Cache\Item
类的默认模拟。Cache\Item::getValue
:在您的测试类中添加名为mockCacheItemGetValue
的方法。如果省略,模拟的Cache\Item
将在调用此方法时返回"value"
。Cache\Item::isHit
:在您的测试类中添加名为mockCacheItemIsHit
的方法。如果省略,模拟的Cache\Item
将在调用此方法时返回false
。
use Joomla\Cache\Tests\Mocker as CacheMocker; class FactoryTest extends \PHPUnit_Framework_TestCase { private $instance; // // The following mocking methods are optional. // /** * Callback to mock the Cache\Item::getValue method. * * @return string */ public function mockCacheItemGetValue() { // This is the default handling. // You can override this method to provide a custom return value. return 'value'; } /** * Callback to mock the Cache\Item::isHit method. * * @return boolean */ public function mockCacheItemIsHit() { // This is the default handling. // You can override this method to provide a custom return value. return false; } /** * Callback to mock the Cache\Cache::get method. * * @param string $text The input text. * * @return string */ public function mockCacheGet($key) { // This is the default handling. // You can override this method to provide a custom return value. return $this->createMockItem(); } protected function setUp() { parent::setUp(); $mocker = new CacheMocker($this); $this->instance = new SomeClass($mocker->createMockCache()); } }
通过 Composer 安装
将 "alejoasotelo/joomla-cache-compat": "~1.0"
添加到您的 composer.json 文件中的 require 块,然后运行 composer install
。
{ "require": { "alejoasotelo/joomla-cache-compat": "~1.0" } }
或者,您可以直接从命令行运行以下命令
composer require alejoasotelo/joomla-cache-compat "~1.0"