|
| 1 | +# WP Cache Helper |
| 2 | + |
| 3 | +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 (which [core does not provide](https://core.trac.wordpress.org/ticket/4476)), and per-prefix scoping so multiple consumers on the same site never collide. |
| 4 | + |
| 5 | +Inspired by [WP Cache Remember](https://github.com/stevegrunwell/wp-cache-remember). |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- PHP 7.4+ |
| 10 | +- WordPress 5.0+ |
| 11 | +- Composer |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```console |
| 16 | +composer require duckdev/wp-cache-helper |
| 17 | +``` |
| 18 | + |
| 19 | +Classes autoload under the `DuckDev\Cache\` namespace via PSR-4. |
| 20 | + |
| 21 | +## Initialisation |
| 22 | + |
| 23 | +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: |
| 24 | + |
| 25 | +```php |
| 26 | +$cache = \DuckDev\Cache\Cache::get_instance( 'my_plugin' ); |
| 27 | +``` |
| 28 | + |
| 29 | +You can also instantiate directly (useful for tests): |
| 30 | + |
| 31 | +```php |
| 32 | +$cache = new \DuckDev\Cache\Cache( 'my_plugin' ); |
| 33 | +``` |
| 34 | + |
| 35 | +Every key, group, and the `{prefix}_can_cache` toggle filter are namespaced under the supplied prefix. |
| 36 | + |
| 37 | +## Options |
| 38 | + |
| 39 | +There are no runtime options — the only configuration is the prefix passed to the constructor. Behaviour is instead tuned via the filter below. |
| 40 | + |
| 41 | +## Methods |
| 42 | + |
| 43 | +| Method | Backed by | Purpose | |
| 44 | +| --- | --- | --- | |
| 45 | +| `remember( $key, $callback, $group, $expiry )` | Object cache | Read, or compute + cache on miss. | |
| 46 | +| `forget( $key, $group, $default )` | Object cache | Read then delete; return `$default` on miss. | |
| 47 | +| `persist( $key, $callback, $site_wide, $expiry )` | Transients | Read, or compute + cache on miss. | |
| 48 | +| `cease( $key, $site_wide, $default )` | Transients | Read then delete; return `$default` on miss. | |
| 49 | +| `flush_group( $group )` | Object cache | Invalidate every entry in a group. | |
| 50 | +| `flush()` | Object cache | Flush the entire object cache. **Last resort.** | |
| 51 | +| `object_cache()` / `transient_cache()` | — | Access the underlying driver for finer-grained control. | |
| 52 | + |
| 53 | +Every callback-based helper checks the callback return with `is_wp_error()` and skips caching when a `WP_Error` is returned, so a transient API failure is not memorised. |
| 54 | + |
| 55 | +## Filters |
| 56 | + |
| 57 | +| Filter | Arguments | Use | |
| 58 | +| --- | --- | --- | |
| 59 | +| `{prefix}_can_cache` | `bool $enabled, string $type` | Return `false` to disable caching. `$type` is `'object'` or `'transient'` so the two can be toggled independently. | |
| 60 | + |
| 61 | +## Actions |
| 62 | + |
| 63 | +The library does not fire any actions. |
| 64 | + |
| 65 | +## Example usage |
| 66 | + |
| 67 | +### `remember()` — object-cache read-through |
| 68 | + |
| 69 | +```php |
| 70 | +$cache = \DuckDev\Cache\Cache::get_instance( 'my_plugin' ); |
| 71 | + |
| 72 | +$posts = $cache->remember( 'latest_posts', function () { |
| 73 | + return new WP_Query( array( |
| 74 | + 'posts_per_page' => 5, |
| 75 | + 'orderby' => 'post_date', |
| 76 | + 'order' => 'desc', |
| 77 | + ) ); |
| 78 | +}, 'queries', HOUR_IN_SECONDS ); |
| 79 | +``` |
| 80 | + |
| 81 | +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. |
| 82 | + |
| 83 | +### `forget()` — one-shot read |
| 84 | + |
| 85 | +```php |
| 86 | +$error = $cache->forget( 'form_errors', 'flash', false ); |
| 87 | + |
| 88 | +if ( $error ) { |
| 89 | + echo 'An error occurred: ' . esc_html( $error ); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### `persist()` — transient read-through |
| 94 | + |
| 95 | +```php |
| 96 | +$cache->persist( 'latest_tweets_' . $user_id, function () use ( $user_id ) { |
| 97 | + return get_latest_tweets_for_user( $user_id ); |
| 98 | +}, false, 15 * MINUTE_IN_SECONDS ); |
| 99 | +``` |
| 100 | + |
| 101 | +Pass `true` for the third argument to use site-wide (multisite) transients. |
| 102 | + |
| 103 | +::: tip |
| 104 | +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`. |
| 105 | +::: |
| 106 | + |
| 107 | +### `flush_group()` |
| 108 | + |
| 109 | +```php |
| 110 | +$cache->flush_group( 'queries' ); |
| 111 | +``` |
| 112 | + |
| 113 | +Internally increments a per-group version sentinel — old entries become unreadable on next access without touching the rest of the object cache. |
| 114 | + |
| 115 | +### Disabling caching for debugging |
| 116 | + |
| 117 | +```php |
| 118 | +add_filter( 'my_plugin_can_cache', '__return_false' ); |
| 119 | +``` |
0 commit comments