humanmade/hm-swr-cache
Composer 安装命令:
composer require humanmade/hm-swr-cache
包简介
WordPress stale-while-revalidate cache plugin
关键字:
README 文档
README
A soft expiring cache with background updating is a specialized caching strategy that allows cached data to remain available even after the expiration period. With regular caching, data becomes unavailable or leads to a 'cache miss' as soon as it expires. This can be costly, particularly with complex data fetching operations that require significant time.
In a soft expiring cache, when the cache period expires, the cached data remains accessible while a background update operation occurs. During this time, the user or application continues to see the stale cached data until the new updated data is ready. This approach ensures data is always available for end consumers with minimized latency from cache rebuild, providing a continuously smooth experience. This is also called a stale-while-revalidate cache strategy, thus the name.
Note: As the cache is updated via a wp_schedule_single_event() call, using a scalable job system such as
Cavalcade is recommended as the default WordPress pseudo-cron slows
the page load running the scheduled event.
Requirements
- PHP 8.1+ is supported (technically it might run on PHP 7.1+).
- WordPress 6.4.2+ is supported (technically it might run on older versions, your experience is welcome).
Installation
Just install the plugin like any other plugin. Installation as a MU plugin is supported as well, and via composer
composer config repositories.hm-swr-cache vcs git@github.com:humanmade/hm-swr-cache.git composer require humanmade/hm-swr-cache:^0.3 --no-update composer update humanmade/hm-swr-cache --prefer-source
These commands configure Composer to use a custom Git repository for the hm-swr-cache package, add this package as a requirement without immediately installing it, and finally update and install the package from the source repository.
Usage
All that is needed is for you to create a callback function that will be called with $callback_args as the first
parameter by a scheduled task that is triggered when the cache expires after $cache_expiry seconds.
The following code goes where you need to use the cached data. On the first request the cache will be empty and scheduled to be generated, so your implementation needs handle that event. After the first generation, there will either be stale or current data to display:
// false is returned if the data isn't in the cache yet $data = SwrCache\get( $cache_key, $cache_group, __NAMESPACE__ . '\\my_callback', $callback_args, $cache_expiry ); if ( ! $data) { // handle initial empty cache load }
The callback is what you need to implement to generate the cacheable data, an array in the example below.
You must return a WP_Error object in case of issues, this will generate an RuntimeException in the cron event,
resulting in a cron log entry.
function my_callback( array $args ) : WP_Error|array { // Example validation handling and cache generation. if ( ! isset( $args['url'] ) ) { return new WP_Error( 'missing_arg', 'url' ); } $data = example_api_request( $args ); if ( failed_validation( $data ) ) { return new WP_Error( 'invalid_data', 'Data failed validation', $data ); } return $data; }
Internally a self-expiring and self-managed lock system ensures the callback is just processed once, avoiding race conditions. If, for whatever reason (for example due to a fatal error) the callback doesn't complete, then the lock self-expires in 1 minute.
Flushing the cache
Engineers can clear the cache as well as any locks by calling the cache_delete_group( $cache_group ) function,
which might be useful after saving a settings page for example. The cachegroup should first be
registrered for flushing using register_cache_group( $cache_group ) before it can be flushed.
Difference with other plugins
- WP-TLC-Transients In contrast to WP-TLC-Transients, HM Swr Cache is not hitting the database in any circumstance as both the lock, cached data and cache expiry are all stored using
wp_cachefunctions. The syntax is also quite different.
I think that's it!
humanmade/hm-swr-cache 适用场景与选型建议
humanmade/hm-swr-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「wordpress」 「expiry」 「soft-expiring-cache」 「background-updating」 「caching-strategy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 humanmade/hm-swr-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 humanmade/hm-swr-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 humanmade/hm-swr-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple embargo & expiry for SiteTree
repository php library
Laravel Eloquent boolean & timestamp flagged attributes behavior.
Laravel 5 - Repositories to the database layer
Symfony bundle for password policy enforcements including password history, expiry, and validation
It handles password expiry of users.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2025-02-17