duckdev/wp-cache-helper
Composer 安装命令:
composer require duckdev/wp-cache-helper
包简介
Helper library for the WordPress object cache and transients with group flush support, per-prefix scoping, and a swappable driver layer.
README 文档
README
WP Cache Helper
WP Cache Helper is a small WordPress library that wraps the object cache and transient APIs with a callback-style
remember() helper, group-flush support for the object cache (delegating to core's wp_cache_flush_group() on
WP 6.1+ backends that support it, with a version-sentinel fallback for backends that don't), and per-prefix
scoping so multiple consumers on the same site never collide.
Inspired by WP Cache Remember.
📖 Full documentation: docs.duckdev.com/wp-libraries/wp-cache-helper/overview
Requirements
- PHP 7.4 or higher
- WordPress 6.1+
- Composer
Installation
composer require duckdev/wp-cache-helper
The library autoloads under the DuckDev\Cache\ namespace via PSR-4.
Architecture
The library is organised as a tiny container wired up by the entry class DuckDev\Cache\Cache. The folder layout
mirrors the namespace:
src/
├── Cache.php # Container + entry point
├── Contracts/
│ ├── ObjectCacheInterface.php
│ └── TransientCacheInterface.php
├── Storage/
│ ├── ObjectCache.php # wp_cache_* wrapper + version-based group flush
│ └── TransientCache.php # (site_)transient wrapper
├── Support/
│ └── KeyPrefixer.php # Shared key + group prefixing
└── Exceptions/
└── CacheException.php
Services receive their collaborators by constructor injection so they can be unit-tested without WordPress in the loop. Construction has no side effects.
Usage
Initialisation
Each container instance is scoped to a single prefix. Pass any non-empty string the first time you ask for it; the same prefix returns the same instance on subsequent calls:
$cache = \DuckDev\Cache\Cache::get_instance( 'my_plugin' );
You can also instantiate directly (useful for tests where you want to inject custom drivers):
$cache = new \DuckDev\Cache\Cache( 'my_plugin' );
Every key, group, and the {prefix}_can_cache toggle filter are namespaced under the supplied prefix.
Provided helpers
| Method | Backed by | Purpose |
|---|---|---|
remember() |
Object cache | Read, or compute + cache on miss. |
forget() |
Object cache | Read then delete; return a default on miss. |
persist() |
Transients | Read, or compute + cache on miss. |
cease() |
Transients | Read then delete; return a default on miss. |
flush_group() |
Object cache | Invalidate every entry in a group. |
flush() |
Object cache | Flush the entire object cache. Last resort. |
object_cache() / transient_cache() |
— | Access the underlying driver for finer-grained control. |
Every callback-based helper checks the return value with is_wp_error() and skips caching when one is returned, so a
transient API failure is not memorised.
Disabling caching
For debugging, return false from the {prefix}_can_cache filter:
add_filter( 'my_plugin_can_cache', '__return_false' );
The second argument is the cache type — 'object' or 'transient' — so the two can be toggled independently.
Cache::remember()
Retrieve a value from the object cache. If it doesn't exist, run the $callback to generate and cache the value.
$cache = \DuckDev\Cache\Cache::get_instance( 'my_plugin' ); function get_latest_posts() { global $cache; return $cache->remember( 'latest_posts', function () { return new WP_Query( array( 'posts_per_page' => 5, 'orderby' => 'post_date', 'order' => 'desc', ) ); }, 'queries', HOUR_IN_SECONDS ); }
Unlike a naive wp_cache_get()-then-fall-back pattern, remember() distinguishes a legitimately cached 0, '',
[], or false from a true miss — the callback only runs when nothing was cached.
Cache::forget()
Retrieve a value from the object cache then delete it. Returns $default on miss.
$error_message = $cache->forget( 'form_errors', 'flash', false ); if ( $error_message ) { echo 'An error occurred: ' . esc_html( $error_message ); }
Cache::persist()
Same shape as remember() but backed by the transient API.
$cache->persist( 'latest_tweets_' . $user_id, function () use ( $user_id ) { return get_latest_tweets_for_user( $user_id ); }, false, 15 * MINUTE_IN_SECONDS );
Pass true for the third argument to use site-wide (multisite) transients.
Note: transients use boolean false as the miss sentinel, so a legitimately cached false value is indistinguishable
from a miss. Reach for remember() if you need to cache false.
Cache::cease()
Transient counterpart to forget().
Cache::flush_group()
Invalidate every entry stored under a group, without touching the rest of the object cache. On WP 6.1+ with a
persistent object cache backend that advertises flush_group support (via wp_cache_supports( 'flush_group' )),
this delegates straight to wp_cache_flush_group(). Otherwise it falls back to incrementing a per-group version
sentinel — old entries become unreadable on next access.
Cache::flush()
Wrapper for wp_cache_flush() with a fallback to $wp_object_cache->flush() when the function is disabled by a
drop-in. Clears every group on the site, so use only as a last resort.
Upgrading from 1.x
- PHP minimum is now 7.4. PHP 5.6/7.0–7.3 are no longer supported.
- The constructor now requires a prefix:
new Cache( 'my_plugin' ). In 1.x the prefix was a hardcodedduckdev_cacheshared across every consumer. - The
can_cachefilter is now{prefix}_can_cache(e.g.my_plugin_can_cache) rather than the sharedduckdev_cache_can_cache. remember()andforget()now correctly treat a cached0/''/[]/falseas a hit instead of re-running the callback.
The public method surface (remember, forget, persist, cease, flush_group, flush) is otherwise unchanged.
Development
composer install composer test # PHPUnit composer phpcs # WordPress Coding Standards
Credits
- Maintained by Joel James
License
duckdev/wp-cache-helper 适用场景与选型建议
duckdev/wp-cache-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 311 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「wordpress」 「object-cache」 「transients」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 duckdev/wp-cache-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 duckdev/wp-cache-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 duckdev/wp-cache-helper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
WordPress mu-plugin disabling Redis Object Cache for WordPress at various occasions.
Laravel 5 - Repositories to the database layer
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.
Fast websites are more accessible, more sustainable and are giving a better UX. This is the code which accelerates figuren.theater and its WordPress Multisite Network.
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 311
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2021-08-08