italystrap / cache
WordPress Transient 的简单 PSR-16 缓存实现,面向对象方式
Requires
- php: >=7.4
- fig/cache-util: ^1.0
- italystrap/storage: ^1.0
- psr/cache: ^1.0
- psr/clock: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- cache/integration-tests: ^0.17.0
- codeception/module-asserts: ^1.0
- codeception/module-db: ^1.2
- codeception/module-phpbrowser: ^1.0
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- humanmade/psalm-plugin-wordpress: ^3.0.0-alpha1
- infection/codeception-adapter: ^0.4.1
- infection/infection: ^0.26.6
- italystrap/debug: ^2.1
- lucatume/function-mocker-le: ^1.0
- lucatume/wp-browser: ^3.1
- phpbench/phpbench: ^1.2
- phpcompatibility/php-compatibility: ^9.3
- phpmetrics/phpmetrics: ^2.8
- phpspec/prophecy-phpunit: ^2.0
- rector/rector: ^0.15.17
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^5.6
Provides
- psr/cache-implementation: 1.0|2.0|3.0
- psr/simple-cache-implementation: 1.0|2.0|3.0
This package is auto-updated.
Last update: 2024-09-15 05:18:20 UTC
README
PSR-16 & PSR-6 缓存实现,用于 WordPress Transient 和面向对象的缓存
版本 2.0 引入了 BC 破坏,请阅读以下文档
目录
安装
最佳使用此包的方式是通过 Composer
composer require italystrap/cache
此包遵循 SemVer 规范,并在次要版本之间实现完全向后兼容。
有关 WordPress API 的更多信息
为什么选择这个包?
最初,我创建这个包是为了在 WordPress 中使用 PSR-16 缓存接口,但从此问题 #4 开始,我决定提取驱动程序,并也添加了 PSR-6 缓存接口。
从版本 2.0 开始,我也添加了 PSR-6 实现,因此如果您需要,您也可以使用 Pool
类来缓存您的数据。
因此,此包现在支持 PSR-16 和 PSR-6 缓存接口。
此包所需的驱动程序使用 WordPress 的 Transients 和 Object Cache API 来存储数据,但如果您需要使用其他 API,您可以创建自己的驱动程序,因为只需实现 Storage API 中的 \ItalyStrap\Storage\CacheInterface
接口。
从版本 1 迁移到版本 2
首先重要的事情是,从版本 2 开始,您需要将驱动程序对象和过期对象传递给您想要使用的类的构造函数。
SimpleCache
和 Pool
是两个需要将驱动程序和过期参数传递给构造函数的类。
驱动程序是 WordPress Transient API 和 WordPress Object Cache API 的对象包装。
过期对象用于设置缓存的过期时间。
以下文档中列出了您可以使用的驱动程序和过期对象。
第二重要的事情是,驱动程序必须实现 Cache API 中的 CacheInterface
,这样,如果您需要创建自己的驱动程序,可以简单地通过实现接口来完成。
为什么需要过期对象?
过期对象用于设置缓存的过期时间。
因为我希望尽可能接近 PSR-16 和 PSR-6 规范,我创建了一个负责设置过期时间的对象,这样我可以在所有 PSR-16 和 PSR-6 实现中重用相同的逻辑,并且不需要创建规范中未定义的方法。
基本用法
定时器常量
在 WordPress 中,有一些常量可以用来表示秒。以下是一个列表
const MINUTE_IN_SECONDS = 60; // (seconds) const HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS; const DAY_IN_SECONDS = 24 * HOUR_IN_SECONDS; const WEEK_IN_SECONDS = 7 * DAY_IN_SECONDS; const MONTH_IN_SECONDS = 30 * DAY_IN_SECONDS; const YEAR_IN_SECONDS = 365 * DAY_IN_SECONDS;
或者,您可以使用 ItalyStrap\Cache\ExpirationInterface
中的内置常量
use ItalyStrap\Cache\ExpirationInterface; $expirationTime = ExpirationInterface::MINUTE_IN_SECONDS; $expirationTime = ExpirationInterface::HOUR_IN_SECONDS; $expirationTime = ExpirationInterface::DAY_IN_SECONDS; $expirationTime = ExpirationInterface::WEEK_IN_SECONDS; $expirationTime = ExpirationInterface::MONTH_IN_SECONDS; $expirationTime = ExpirationInterface::YEAR_IN_SECONDS;
或者,您可以使用内置的 PHP 函数 strtotime()
来表示秒。
使用内置WordPress Transients API的常见用法
if (false === ($special_data_to_save = \get_transient('special_data_to_save'))) { // It wasn't there, so regenerate the data and save the transient $special_data_to_save = ['some-key' => 'come value']; \set_transient('special_data_to_save', $special_data_to_save, 12 * HOUR_IN_SECONDS); }
您可以保存的数据可以是支持序列化API的任何内容。简而言之,您可以保存任何标量值、数组或对象。
与Pool缓存的常见用法
use ItalyStrap\Cache\Pool; use ItalyStrap\Cache\Expiration; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $pool = new Pool($driver, $expiration); // Pass the pool object to other classes that need to save data // then retrieve the data from the pool $item = $pool->getItem('special_data_to_save'); if (!$item->isHit()) { // It wasn't there, so regenerate the data and save the transient $item->set(['some-key' => 'some value']); $item->expiresAfter(12 * HOUR_IN_SECONDS); $pool->save($item); } $special_data_to_save = $item->get(); ['some-key' => 'some value'] === $special_data_to_save; // True
与SimpleCache的常见用法
只有当您需要解析二进制数据(例如图像文件)时,您才能使用BinaryCacheDecorator
类,但请记住,如果文件大小超过1MB,最好不要使用此方法将其保存到数据库中。
作为优秀的开发者,您不会将二进制数据保存到数据库中,而会使用Transient
类而不是BinaryCacheDecorator
类。
use ItalyStrap\Cache\Expiration; use ItalyStrap\Cache\SimpleCache; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $cache = new SimpleCache($driver, $expiration); // Pay attention to `SimpleCacheInterface::get()` method because if there is no value will return `null` and not `false` as the WordPress Transient API does. if (null === ($special_data_to_save = $cache->get('special_data_to_save'))) { // It wasn't there, so regenerate the data and save the transient $special_data_to_save = ['some-key' => 'some value']; $cache->set('special_data_to_save', $special_data_to_save, 12 * HOUR_IN_SECONDS); } ['some-key' => 'some value'] === $special_data_to_save; // True
使用Pool删除缓存
use ItalyStrap\Cache\Pool; use ItalyStrap\Cache\Expiration; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $pool = new Pool($driver, $expiration); $item = $pool->getItem('special_data_to_save'); $item->set(['some-key' => 'some value']); $item->expiresAfter(12 * HOUR_IN_SECONDS); $pool->save($item); $pool->deleteItem('special_data_to_save'); // Return bool // `::getItem()` will return a new item instance, always $pool->getItem('special_data_to_save')->isHit(); // Return false
使用SimpleCache删除缓存
use ItalyStrap\Cache\Expiration; use ItalyStrap\Cache\SimpleCache; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $cache = new SimpleCache($driver, $expiration); $cache->set('special_data_to_save', ['some-key' => 'some value'], 12 * HOUR_IN_SECONDS); $cache->delete('special_data_to_save'); // Return bool $cache->get('special_data_to_save'); // Return null
使用Pool检查缓存是否存在
use ItalyStrap\Cache\Pool; use ItalyStrap\Cache\Expiration; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $pool = new Pool($driver, $expiration); $item = $pool->getItem('special_data_to_save'); $item->set(['some-key' => 'some value']); $item->expiresAfter(12 * HOUR_IN_SECONDS); $pool->save($item); $pool->hasItem('special_data_to_save'); // Return true // But also this will return false if the item is expired or not exists $pool->hasItem('expired_or_not_existent_value'); // Return false
使用SimpleCache检查缓存是否存在
use ItalyStrap\Cache\Expiration; use ItalyStrap\Cache\SimpleCache; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $cache = new SimpleCache($driver, $expiration); $cache->set('special_data_to_save', ['some-key' => 'some value'], 12 * HOUR_IN_SECONDS); $cache->has('special_data_to_save'); // Return true // But also this will return false if the item is expired or not exists $cache->has('expired_or_not_existent_value'); // Return false
使用SimpleCache保存多个缓存
use ItalyStrap\Cache\Expiration; use ItalyStrap\Cache\SimpleCache; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $cache = new SimpleCache($driver, $expiration); $values = [ 'key' => 'value', 'key2' => 'value2', ]; $cache->setMultiple($values, 12 * HOUR_IN_SECONDS); // Return bool
使用SimpleCache获取多个缓存
use ItalyStrap\Cache\Expiration; use ItalyStrap\Cache\SimpleCache; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $cache = new SimpleCache($driver, $expiration); $values = [ 'key' => 'value', 'key2' => 'value2', 'key3' => false, // This will be replaced with 'some default value' because the method pass a default value ]; $fetched_values = $cache->getMultiple(\array_keys($values), 'some default value'); // Return values
使用SimpleCache删除多个缓存
use ItalyStrap\Cache\Expiration; use ItalyStrap\Cache\SimpleCache; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $cache = new SimpleCache($driver, $expiration); $values = [ 'key' => 'value', 'key2' => 'value2', 'key3' => false, ]; $cache->deleteMultiple(\array_keys($values)); // Return bool
使用SimpleCache清除缓存
此方法不会清除整个WordPress缓存,只清除使用::set()和::setMultiple()方法设置的客户端使用的缓存。
use ItalyStrap\Cache\Expiration; use ItalyStrap\Cache\SimpleCache; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $cache = new SimpleCache($driver, $expiration); $cache->set('special_data_to_save',['some-key' => 'come value'], 12 * HOUR_IN_SECONDS); $values = [ 'key' => 'value', 'key2' => 'value2', ]; $cache->setMultiple($values, 12 * HOUR_IN_SECONDS); $cache->clear(); // Return bool $cache->get('special_data_to_save'); // Return null $cache->get('key'); // Return null $cache->get('key2'); // Return null
Cache::clear()将清除'special_data_to_save'、'key'和'key2'。
其他示例
use ItalyStrap\Cache\Expiration; use ItalyStrap\Cache\SimpleCache; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $cache = new SimpleCache($driver, $expiration); // Get any existing copy of our transient data if (false === ($special_data_to_save = $cache->get('special_data_to_save'))) { // It wasn't there, so regenerate the data and save the transient $cache->set('special_data_to_save', ['some-key' => 'some value'], 12 * HOUR_IN_SECONDS); } // Use the data like you would have normally... //Or // Get any existing copy of our transient data if (!$cache->has('special_data_to_save')) { // It wasn't there, so regenerate the data and save the transient $cache->set('special_data_to_save', ['some-key' => 'some value'], 12 * HOUR_IN_SECONDS); } // Use the data like you would have normally...
您还可以使用桥接器使用\Psr\SimpleCache\CacheInterface并注入\Psr\Cache\CacheItemPoolInterface。
use ItalyStrap\Cache\Pool; use ItalyStrap\Cache\Expiration; use ItalyStrap\Storage\BinaryCacheDecorator; use ItalyStrap\Storage\Transient; $driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache() $expiration = new Expiration(); $pool = new Pool($driver, $expiration); $cache = new \ItalyStrap\Cache\SimpleCacheBridge($pool); // and use the $cache as \Psr\SimpleCache\CacheInterface
使用工厂简化缓存的创建
use ItalyStrap\Cache\Factory; $cache = (new Factory())->makePool(); $cache = (new Factory())->makePoolTransient(); $cache = (new Factory())->makeSimpleCache(); $cache = (new Factory())->makeSimpleCacheTransient(); $cache = (new Factory())->makeSimpleCacheBridge(); $cache = (new Factory())->makeSimpleCacheBridgeTransient();
贡献
所有反馈/错误报告/拉取请求都受欢迎。
许可证
版权所有(c)2019 Enea Overclokk,意大利Strap
此代码根据MIT许可。