PHPackages                             duckdev/wp-cache-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. duckdev/wp-cache-helper

ActiveLibrary[Caching](/categories/caching)

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.

2.0.2(2d ago)23111GPL-2.0-or-laterPHPPHP &gt;=7.4

Since Aug 8Pushed 4y ago1 watchersCompare

[ Source](https://github.com/duckdev/wp-cache-helper)[ Packagist](https://packagist.org/packages/duckdev/wp-cache-helper)[ Docs](https://github.com/duckdev/wp-cache-helper)[ RSS](/packages/duckdev-wp-cache-helper/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (6)Versions (6)Used By (0)

[ ![](https://camo.githubusercontent.com/838ee08209c53949b986b1d1c9c01b23693a9c2f6438a28e370f26c630223818/68747470733a2f2f6475636b6465762e636f6d2f77702d636f6e74656e742f75706c6f6164732f323032302f31322f63726f707065642d6475636b6465762d6c6f676f2d6d69642e706e67)](http://duckdev.com)

WP Cache Helper
===============

[](#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](https://github.com/stevegrunwell/wp-cache-remember).

📖 **Full documentation:** [docs.duckdev.com/wp-libraries/wp-cache-helper/overview](https://docs.duckdev.com/wp-libraries/wp-cache-helper/overview)

Requirements
------------

[](#requirements)

- PHP 7.4 or higher
- WordPress 6.1+
- Composer

Installation
------------

[](#installation)

```
composer require duckdev/wp-cache-helper
```

The library autoloads under the `DuckDev\Cache\` namespace via PSR-4.

Architecture
------------

[](#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
-----

[](#usage)

### Initialisation

[](#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

[](#provided-helpers)

MethodBacked byPurpose[`remember()`](#cache-remember)Object cacheRead, or compute + cache on miss.[`forget()`](#cache-forget)Object cacheRead then delete; return a default on miss.[`persist()`](#cache-persist)TransientsRead, or compute + cache on miss.[`cease()`](#cache-cease)TransientsRead then delete; return a default on miss.[`flush_group()`](#cache-flush_group)Object cacheInvalidate every entry in a group.[`flush()`](#cache-flush)Object cacheFlush 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

[](#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()`

[](#cacheremember-)

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()`

[](#cacheforget-)

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()`

[](#cachepersist-)

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()`

[](#cachecease-)

Transient counterpart to `forget()`.

### `Cache::flush_group()`

[](#cacheflush_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()`

[](#cacheflush-)

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
------------------

[](#upgrading-from-1x)

- 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 hardcoded `duckdev_cache` shared across every consumer.
- The `can_cache` filter is now `{prefix}_can_cache` (e.g. `my_plugin_can_cache`) rather than the shared `duckdev_cache_can_cache`.
- `remember()` and `forget()` now correctly treat a cached `0` / `''` / `[]` / `false` as a hit instead of re-running the callback.

The public method surface (`remember`, `forget`, `persist`, `cease`, `flush_group`, `flush`) is otherwise unchanged.

Development
-----------

[](#development)

```
composer install
composer test     # PHPUnit
composer phpcs    # WordPress Coding Standards
```

### Credits

[](#credits)

- Maintained by [Joel James](https://github.com/joel-james/)

### License

[](#license)

[GPLv2+](http://www.gnu.org/licenses/gpl-2.0.html)

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance55

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~595 days

Total

4

Last Release

2d ago

Major Versions

v1.0.0 → 2.0.02026-06-09

PHP version history (2 changes)v1.0.0PHP &gt;=5.6

2.0.0PHP &gt;=7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/17586?v=4)[Schmidt](/maintainers/Jo)[@jo](https://github.com/jo)

---

Top Contributors

[![Joel-James](https://avatars.githubusercontent.com/u/7510463?v=4)](https://github.com/Joel-James "Joel-James (4 commits)")

---

Tags

cacheobject-cachetransientswordpresswordpresscachetransientsobject-cache

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/duckdev-wp-cache-helper/health.svg)

```
[![Health](https://phpackages.com/badges/duckdev-wp-cache-helper/health.svg)](https://phpackages.com/packages/duckdev-wp-cache-helper)
```

###  Alternatives

[rtcamp/nginx-helper

Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also provides cloudflare edge cache purging with Cache-Tags.

23617.1k1](/packages/rtcamp-nginx-helper)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
