yii1tech / psr-cache
为 Yii1 应用提供对 PSR-6 兼容的缓存支持
Requires
- php: >=7.1
- psr/cache: ^1.0.0
- yiisoft/yii: ~1.1.0
Requires (Dev)
- phpunit/phpunit: ^6.0 || ^7.0 || ^8.0 || ^9.3 || ^10.0.7
- yii1tech/tagged-cache: ^1.0.0
This package is auto-updated.
Last update: 2024-09-09 17:31:11 UTC
README
Yii1 PSR-6 缓存扩展
此扩展允许与 Yii1 的 PSR-6 兼容缓存集成。
有关许可信息,请查看 LICENSE 文件。
安装
安装此扩展的首选方式是通过 composer。
运行
php composer.phar require --prefer-dist yii1tech/psr-cache
或添加
"yii1tech/psr-cache": "*"
到您的 composer.json 文件的 "require" 部分。
用法
此扩展允许与 PSR-6 兼容的缓存集成到 Yii1。它为此提供了几个工具。请选择适合您特定需求的工具。
将 PSR 缓存池包装成 Yii 缓存
PSR 缓存在 Yii 应用程序中应用最常见的情况是使用第三方缓存库。这可以通过使用 \yii1tech\psr\cache\Cache
作为 Yii 缓存组件来实现。
应用程序配置示例
<?php return [ 'components' => [ 'cache' => [ 'class' => \yii1tech\psr\cache\Cache::class, 'psrCachePool' => function () { // ... return new ExamplePsrCachePool(); // instantiate 3rd party cache library }, ], // ... ], // ... ];
将 Yii 缓存包装成 PSR 缓存池
除了引导外部缓存存储之外,PSR 缓存还有另一个用途。有时第三方库可能需要将 PSR 缓存池实例传递给它们才能运行。 \Psr\Cache\CacheItemPoolInterface
允许将标准的 Yii 缓存组件包装成一个 PSR 兼容的缓存池。
应用程序配置示例
<?php return [ 'components' => [ 'cache' => [ 'class' => \CMemCache::class, 'servers' => [ // ... ], ], \Psr\Cache\CacheItemPoolInterface::class => [ 'class' => \yii1tech\psr\cache\CacheItemPool::class, 'cache' => 'cache', ], // ... ], // ... ];
使用示例
<?php use Psr\Cache\CacheItemPoolInterface; function getCachedValue() { /** @var CacheItemPoolInterface $pool */ $pool = Yii::app()->getComponent(CacheItemPoolInterface::class); $item = $pool->getItem('example-cache-id'); if ($item->isHit()) { return $item->get(); // cache exist - return cached value } $value = Yii::app()->db->createCommand('SELECT ...')->query(); // some heave SQL query. $item->set($value) // set value to be cached ->expiresAfter(DateInterval::createFromDateString('1 hour')); // set expiration $pool->save($item); // put value into cache return $value; }
扩展接口
此扩展引入了 2 个接口,它们扩展了 PSR-6 的接口
\yii1tech\psr\cache\CacheItemPoolContract
\yii1tech\psr\cache\CacheItemContract
这些接口可用于利用 PSR 中省略的附加功能。特别是这些允许使用 Yii 缓存依赖项功能。例如
<?php use yii1tech\psr\cache\CacheItemPoolContract; function getValueCachedWithDependency() { /** @var CacheItemPoolContract $pool */ $pool = Yii::app()->getComponent(CacheItemPoolContract::class); $item = $pool->getItem('example-cache-id'); if ($item->isHit()) { return $item->get(); // cache exist - return cached value } $value = Yii::app()->db->createCommand('SELECT ...')->query(); // some heave SQL query. $item->set($value) // set value to be cached ->expiresAfter(DateInterval::createFromDateString('1 hour')) // set expiration ->depends(new CDbCacheDependency('SELECT MAX(id) FROM `items`')); // set cache dependency $pool->save($item); // put value into cache return $value; }
此外, \yii1tech\psr\cache\CacheItemPoolContract
声明了 get()
方法,可用于简化缓存池的使用,使其类似于 Symfony。例如
<?php use yii1tech\psr\cache\CacheItemContract; use yii1tech\psr\cache\CacheItemPoolContract; function getCachedValue() { /** @var CacheItemPoolContract $pool */ $pool = Yii::app()->getComponent(CacheItemPoolContract::class); return $pool->get('example-cache-id', function (CacheItemContract $item) { // enters here, only if cache is missing $item->expiresAfter(DateInterval::createFromDateString('1 hour')); // use callback argument to configure cache item: set expiration and so on $value = Yii::app()->db->createCommand('SELECT ...')->query(); // some heave SQL query. return $value; // returned value automatically saved ot cache }); }
使用缓存标签
此扩展允许通过 \yii1tech\psr\cache\CacheItemContract::tag()
方法为每个特定的缓存项设置标签。
注意! 此软件包不直接实现缓存标签功能 - 它依赖于包装的 Yii 缓存组件来支持它。所有与缓存项关联的标签都作为第五个参数传递给 \ICache::set()
方法,假设其特定实现将处理它们。因此,如果相关的缓存组件不支持它,缓存项标签的保存将 静默失败。
您可以使用 yii1tech/tagged-cache 扩展来获取一个具有标签感知功能的缓存 Yii 组件。
应用程序配置示例
<?php return [ 'components' => [ 'cache' => [ 'class' => \yii1tech\cache\tagged\MemCache::class, // use tag aware cache component 'servers' => [ // ... ], ], \Psr\Cache\CacheItemPoolInterface::class => [ 'class' => \yii1tech\psr\cache\CacheItemPool::class, 'cache' => 'cache', ], // ... ], // ... ];
标签规范示例
<?php use yii1tech\psr\cache\CacheItemContract; use yii1tech\psr\cache\CacheItemPoolContract; function getCachedValue() { /** @var CacheItemPoolContract $pool */ $pool = Yii::app()->getComponent(CacheItemPoolContract::class); return $pool->get('example-cache-id', function (CacheItemContract $item) { $item->expiresAfter(DateInterval::createFromDateString('1 hour')); $item->tag(['database', 'example']); // specify the list of tags for the item $value = Yii::app()->db->createCommand('SELECT ...')->query(); // some heave SQL query. return $value; }); }
要清除与特定标签关联的项,请使用 \yii1tech\psr\cache\CacheItemPoolContract::invalidateTags()
。例如
<?php use yii1tech\psr\cache\CacheItemPoolContract; /** @var CacheItemPoolInterface $pool */ $pool = Yii::app()->getComponent(CacheItemPoolInterface::class); $pool->invalidateTags(['database']); // clear only items tagged as "database"