italystrap/cache
Composer 安装命令:
composer require italystrap/cache
包简介
Simple PSR-16 cache implementations for WordPress transient the OOP way
README 文档
README
PSR-16 & PSR-6 Cache implementations for WordPress transient and cache the OOP way
Version 2.0 is a BC breaks please read the following documentation
Table Of Contents
Installation
The best way to use this package is through Composer:
composer require italystrap/cache
This package adheres to the SemVer specification and will be fully backward compatible between minor versions.
For more information about the WordPress API:
Why this package?
Initially I created this package to use the PSR-16 cache interface in WordPress, but starting from this issue #4 I decided to extract the drivers and also add the PSR-6 cache interface too.
From version 2.0 I also added the PSR-6 implementation, so you can also use the Pool class to cache your data if you need it.
So this package now support both the PSR-16 and PSR-6 cache interfaces.
The driver required by this package uses the Transients and Object Cache APIs from WordPress to store the data, but if you need to use other APIs, you can create your own driver because just implements the interface \ItalyStrap\Storage\CacheInterface from Storage API.
Moving from Version 1 to Version 2
The first important thing is from the version 2 you need to pass the driver object and the expiration object to the constructor of the class you want to use.
SimpleCache and Pool are the two classes that need the driver and the expiration to be passed to the constructor.
The driver is an object wrapper for the WordPress Transient API and the WordPress Object Cache API.
The expiration object is used to set the expiration time of the cache.
Below in the documentation you will find the name of the drivers that you can use and the expiration object.
The second important thing is that the driver must implement the CacheInterface from Cache API, this way if you need to create your own driver you can do it simply by implementing the interface.
Why the needs of an Expiration object?
The expiration object is used to set the expiration time of the cache.
Because I want to be as close as possible to the PSR-16 and PSR-6 specifications, I have created an object that is responsible for setting the expiration time, this way I can reuse the same logic across all PSR-16 and PSR-6 implementations and I didn't need to create more methods that are not in the specifications.
Basic Usage
Timer constants
Inside WordPress there are some constants that can be used to express time in seconds. Here is a list of them:
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;
Or you can use the built-in constant from 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;
Or you can use the built-in PHP function strtotime() to express time in seconds.
Common usage with builtin 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); }
The data you can save can be anything that is supported by the Serialization API. In short, you can save any scalar value, array, object.
Common usage with the Pool cache
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
Common usage with the SimpleCache
Only if you need to parse binary data (as for example image files) you can use the BinaryCacheDecorator class, but remember that if the file is more than 1MB it is better to not use this to save in the database.
And because you are a good developer, you will not save binary data in the database, and you will use the Transient class instead of the BinaryCacheDecorator class.
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
Deleting cache with 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
Deleting cache with 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
Check cache exists with 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
Check cache exists with 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
Saving multiple cache with 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
Fetching multiple cache with 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
Deleting multiple cache with 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
Clearing cache with SimpleCache
This method do not clear the entire WordPress cache, only the cache used by client with ::set() and ::setMultiple() methods.
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() will flush 'special_data_to_save', 'key' and 'key2'.
Other examples
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...
You could also use a Bridge to use the \Psr\SimpleCache\CacheInterface and inject the \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 the Factory to simplify the creation of the Cache
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();
Contributing
All feedback / bug reports / pull requests are welcome.
License
Copyright (c) 2019 Enea Overclokk, ItalyStrap
This code is licensed under the MIT.
Credits
italystrap/cache 适用场景与选型建议
italystrap/cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.17k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2020 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「wordpress」 「psr」 「psr-6」 「transient」 「psr-16」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 italystrap/cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 italystrap/cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 italystrap/cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Laravel 5 - Repositories to the database layer
Error handler based on Booboo with HTML and JSON support
Mvc5 PSR-7 HTTP Messages
Nimo Is your Middleware Organizer
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
统计信息
- 总下载量: 29.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-02