micropackage/cache
Composer 安装命令:
composer require micropackage/cache
包简介
WordPress Cache wrapper with Object and Transient drivers
README 文档
README
🧬 About Cache
This micropackage is a wrapper for WordPress cache with two drivers implemented:
It provides a unified, object-oriented way to manipulate WordPress Cache, witch the Cache manipulator object for even easier setting and getting the cache.
💾 Installation
composer require micropackage/cache
🕹 Usage
Object Cache
Constructing the Object Cache driver:
use Micropackage\Cache\Driver\ObjectCache; $object_cache = new ObjectCache( $group = 'my_group', $expiration = DAY_IN_SECONDS ); $object_cache->set_key( 'cache_key' );
Group parameter allows you to store the cache under the same key across multiple groups. Default is empty string.
By default the expiration is set to 0 which means the transient never expires.
Available methods
| Method | Description | Returns |
|---|---|---|
set_key( (string) $key ) |
Sets the cache key | $this |
get_key() |
Gets the cache key | (string) Cache key |
set_group( (string) $group ) |
Sets the cache group | $this |
get_group |
Gets the cache group | (string) Default empty string |
set_expiration( (int) $expiration ) |
Sets the cache expiration in seconds | $this |
get_expiration() |
Gets the cache expiration | (int) Expiration seconds Default 0 which means the cache doesn't expire |
set( (mixed) $value ) |
Sets the cache | void |
add( (mixed) $value ) |
Sets the cache only if it wasn't set before |
void |
get() |
Gets the cached value | mixed|false False if not set |
force_get() |
Gets the cached value and updates the local cache from persistent cache |
mixed|false False if not set |
delete() |
Deletes the cache | void |
Transient Cache
Constructing the Transient Cache driver:
use Micropackage\Cache\Driver\Transient; $transient_cache = new Transient( $expiration = DAY_IN_SECONDS ); $transient_cache->set_key( 'cache_key' );
By default the expiration is set to 0 which means the transient never expires.
Available methods
| Method | Description | Returns |
|---|---|---|
set_key( (string) $key ) |
Sets the cache key | $this |
get_key() |
Gets the cache key | (string) Cache key |
set_expiration( (int) $expiration ) |
Sets the cache expiration in seconds | $this |
get_expiration() |
Gets the cache expiration | (int) Expiration seconds Default 0 which means the cache doesn't expire |
set( (mixed) $value ) |
Sets the cache | void |
add( (mixed) $value ) |
Sets the cache only if it wasn't set before | void |
get() |
Gets the cached value | mixed|false False if not set |
delete() |
Deletes the cache | void |
Cache manipulator
The Cache manipulator object allows you to use the collect method to easily get/store the cache value.
See the below example with Object Cache (you can pass the Transient Driver as well).
use Micropackage\Cache\Cache; use Micropackage\Cache\Driver\ObjectCache; $driver = new ObjectCache( $group = 'my_group', $expiration = DAY_IN_SECONDS ); $cache = new Cache( $driver, $cache_key = 'extremaly_important_thing' ); $the_thing = $cache->collect( function() { return 'The value was not set apparently'; } );
The collect method takes a callable function as an argument. If the cache wasn't set for the key provided in cache construtor, the callable is called which should return the value for cache. The value is stored and returned.
Using variables from outside the callable:
$some_var = 'I am awesome!'; $the_thing = $cache->collect( function() use ( $some_var ) { return $some_var; } );
📦 About the Micropackage project
Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.
The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.
Micropackages are maintained by BracketSpace.
📖 Changelog
📃 License
This software is released under MIT license. See the LICENSE file for more information.
micropackage/cache 适用场景与选型建议
micropackage/cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34.77k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2020 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 micropackage/cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 micropackage/cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 34.77k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-24