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 class for the WordPress object cache and transients with group flush feature.

v1.0.0(4y ago)2261GPL-2.0-or-laterPHPPHP &gt;=5.6

Since Aug 8Pushed 4y ago1 watchersCompare

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

READMEChangelog (1)DependenciesVersions (2)Used By (0)

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

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

[](#wp-cache-helper)

WP Cache Helper is a simple WordPress library class to introduce convenient new caching functions.

Built to support group cache flush for WordPress' object cache, which is [not supported by core yet](https://core.trac.wordpress.org/ticket/4476).

- Inspired from [WP Cache Remember](https://github.com/stevegrunwell/wp-cache-remember).

This helper can simplify something like this:

```
function do_something() {
    $cache_key = 'some-cache-key';
    $cached    = wp_cache_get( $cache_key );

    // Return the cached value.
    if ( $cached ) {
        return $cached;
    }

    // Do all the work to calculate the value.
    $value = a_whole_lotta_processing();

    // Cache the value.
    wp_cache_set( $cache_key, $value );

    return $value;
}
```

That pattern works well, but there's a lot of repeated code. This package draws inspiration from [Laravel's `Cache::remember()` method](https://laravel.com/docs/5.6/cache#cache-usage); using `Cache::remember()`, the same code from above becomes:

```
// Use this as a global variable or something.
$cache = new \DuckDev\Cache\Cache();

function do_something() {
    return $cache->remember( 'some-cache-key', function () {
        return a_whole_lotta_processing();
    } );
}
```

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

[](#installation)

The recommended way to install this library in your project is [via Composer](https://getcomposer.org/):

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

Usage
-----

[](#usage)

WP Cache Remember provides the following functions for WordPress:

- [`$cache->remember()`](#cache-remember)
- [`$cache->forget()`](#cache-forget)
- [`$cache->persist()`](#cache-persist)
- [`$cache->cease()`](#cache-cease)
- [`$cache->flush_group()`](#cache-flush_group)
- [`$cache->flush()`](#cache-flush)

Each function checks the response of the callback for a `WP_Error` object, ensuring you're not caching temporary errors for long periods of time. PHP Exceptions will also not be cached.

### $cache-&gt;remember()

[](#cache-remember)

Retrieve a value from the object cache. If it doesn't exist, run the `$callback` to generate and cache the value.

#### Parameters

[](#parameters)

 (string) $key The cache key. (callable) $callback The callback used to generate and cache the value. (string) $group Optional. The cache group. Default is empty. (int) $expire Optional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).#### Example

[](#example)

```
function get_latest_posts() {
    return $cache->remember( 'latest_posts', function () {
        return new WP_Query( array(
            'posts_per_page' => 5,
            'orderby'        => 'post_date',
            'order'          => 'desc',
        ) );
    }, 'my-cache-group', HOUR_IN_SECONDS );
}
```

### $cache-&gt;forget()

[](#cache-forget)

Retrieve and subsequently delete a value from the object cache.

#### Parameters

[](#parameters-1)

 (string) $key The cache key. (string) $group Optional. The cache group. Default is empty. (mixed) $default Optional. The default value to return if the given key doesn't exist in the object cache. Default is null.#### Example

[](#example-1)

```
function show_error_message() {
    $error_message = $cache->forget( 'form_errors', 'my-cache-group', false );

    if ( $error_message ) {
        echo 'An error occurred: ' . $error_message;
    }
}
```

### $cache-&gt;persist()

[](#cache-persist)

Retrieve a value from transients. If it doesn't exist, run the `$callback` to generate and cache the value.

#### Parameters

[](#parameters-2)

 (string) $key The cache key. (callable) $callback The callback used to generate and cache the value. (string) $site Should use site transients. (int) $expire Optional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).#### Example

[](#example-2)

```
function get_tweets() {
    $user_id = get_current_user_id();
    $key     = 'latest_tweets_' . $user_id;

    return $cache->persist( $key, function () use ( $user_id ) {
        return get_latest_tweets_for_user( $user_id );
    }, 15 * MINUTE_IN_SECONDS );
}
```

### $cache-&gt;cease()

[](#cache-cease)

Retrieve and subsequently delete a value from the transient cache.

#### Parameters

[](#parameters-3)

 (string) $key The cache key. (string) $site Should use site transients. (mixed) $default Optional. The default value to return if the given key doesn't exist in transients. Default is null.### $cache-&gt;flush\_group()

[](#cache-flush_group)

Flush a cache group items. Use this and do not flush entire cache.

#### Parameters

[](#parameters-4)

 (string) $group The cache group name.### $cache-&gt;flush()

[](#cache-flush)

Wrapper for `wp_cache_flush` to check if other method is available for flushing if `wp_cache_flush` is disabled.

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

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity46

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

Unknown

Total

1

Last Release

1743d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c454f7cc8bce39652f648e9ff9a24cdba67146dd248b2c36f4ec89df78847130?d=identicon)[Jo](/maintainers/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

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

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

PHPackages © 2026

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