PHPackages                             stevegrunwell/wp-cache-remember - 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. stevegrunwell/wp-cache-remember

ActiveWordpress-muplugin[Caching](/categories/caching)

stevegrunwell/wp-cache-remember
===============================

Helper for the WordPress object cache and transients

v1.1.2(3y ago)14891.2k↓33.3%10MITPHPPHP &gt;=5.2

Since Feb 17Pushed 3y ago6 watchersCompare

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

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

WP Cache Remember
=================

[](#wp-cache-remember)

[![Build Status](https://camo.githubusercontent.com/f161e4e0b81f42daba2816a55ff4290340157738b5a9a8f267b67718b5ffc4a3/68747470733a2f2f7472617669732d63692e6f72672f73746576656772756e77656c6c2f77702d63616368652d72656d656d6265722e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/stevegrunwell/wp-cache-remember)[![Coverage Status](https://camo.githubusercontent.com/54f106f8b187c645bf302f02273e3d4c15dcb98c2d933f0dee4c5487885c1b16/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73746576656772756e77656c6c2f77702d63616368652d72656d656d6265722f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/stevegrunwell/wp-cache-remember?branch=develop)[![GitHub release](https://camo.githubusercontent.com/32a51c4d7b04f385bb97e65bc82e0b87d39a142836f8dc117c3fd57f38e30b1b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73746576656772756e77656c6c2f77702d63616368652d72656d656d6265722e737667)](https://github.com/stevegrunwell/wp-cache-remember/releases)

WP Cache Remember is a simple WordPress include to introduce convenient new caching functions.

Well-built WordPress plugins know when to take advantage of the object cache and/or transients, but they often end up with code that looks 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 `wp_cache_remember()`, the same code from above becomes:

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

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

[](#installation)

The best way to install this package is [via Composer](https://getcomposer.org/):

```
$ composer require stevegrunwell/wp-cache-remember
```

The package ships with the [`composer/installers` package](https://github.com/composer/installers), enabling you to control where you'd like the package to be installed. For example, if you're using WP Cache Remember in a WordPress plugin, you might store the file in an `includes/` directory. To accomplish this, add the following to your plugin's `composer.json` file:

```
{
    "extra": {
        "installer-paths": {
            "includes/{$name}/": ["stevegrunwell/wp-cache-remember"]
        }
    }
}
```

Then, from within your plugin, simply include or require the file:

```
require_once __DIR__ . '/includes/wp-cache-remember/wp-cache-remember.php';
```

### Using as a plugin

[](#using-as-a-plugin)

If you'd prefer, the package also includes the necessary file headers to be used as a WordPress plugin.

After downloading or cloning the package, move `wp-cache-remember.php` into either your `wp-content/mu-plugins/` (preferred) or `wp-content/plugins/` directory. If you chose the regular plugins directory, you'll need to activate the plugin manually via the Plugins › Installed Plugins page within WP Admin.

### Bundling within a plugin or theme

[](#bundling-within-a-plugin-or-theme)

WP Cache Remember has been built in a way that it can be easily bundled within a WordPress plugin or theme, even commercially.

Each function declaration is wrapped in appropriate `function_exists()` checks, ensuring that multiple copies of the library can co-exist in the same WordPress environment.

Usage
-----

[](#usage)

WP Cache Remember provides the following functions for WordPress:

- [`wp_cache_remember()`](#wp_cache_remember)
- [`wp_cache_forget()`](#wp_cache_forget)
- [`remember_transient()`](#remember_transient)
- [`forget_transient()`](#forget_transient)
- [`remember_site_transient()`](#remember_site_transient)
- [`forget_site_transient()`](#forget_site_transient)

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.

### wp\_cache\_remember()

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

### wp\_cache\_forget()

[](#wp_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 = wp_cache_forget( 'form_errors', 'my-cache-group', false );

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

### remember\_transient()

[](#remember_transient)

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. (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 remember_transient( $key, function () use ( $user_id ) {
        return get_latest_tweets_for_user( $user_id );
    }, 15 * MINUTE_IN_SECONDS );
}
```

### forget\_transient()

[](#forget_transient)

Retrieve and subsequently delete a value from the transient cache.

#### Parameters

[](#parameters-3)

 (string) $key The cache key. (mixed) $default Optional. The default value to return if the given key doesn't exist in transients. Default is null.### remember\_site\_transient()

[](#remember_site_transient)

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

This function shares arguments and behavior with [`remember_transient()`](#remember_transient), but works network-wide when using WordPress Multisite.

### forget\_site\_transient()

[](#forget_site_transient)

Retrieve and subsequently delete a value from the site transient cache.

This function shares arguments and behavior with [`forget_transient()`](#forget_transient), but works network-wide when using WordPress Multisite.

License
-------

[](#license)

Copyright 2018 Steve Grunwell

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 89.5% 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 ~534 days

Total

4

Last Release

1410d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/05f4d610f0de13ac1c23825f691fd05f2dd37ae9e8f0483e2dc4f1ca1e2bfb32?d=identicon)[stevegrunwell](/maintainers/stevegrunwell)

---

Top Contributors

[![stevegrunwell](https://avatars.githubusercontent.com/u/233836?v=4)](https://github.com/stevegrunwell "stevegrunwell (51 commits)")[![macbookandrew](https://avatars.githubusercontent.com/u/784333?v=4)](https://github.com/macbookandrew "macbookandrew (3 commits)")[![jrfnl](https://avatars.githubusercontent.com/u/663378?v=4)](https://github.com/jrfnl "jrfnl (2 commits)")[![aaemnnosttv](https://avatars.githubusercontent.com/u/1621608?v=4)](https://github.com/aaemnnosttv "aaemnnosttv (1 commits)")

---

Tags

composer-packagewordpresswordpress-object-cachewordpress-pluginwordpress-transientswordpresswordpress pluginwordpress-object-cachewordpress-transients

### Embed Badge

![Health badge](/badges/stevegrunwell-wp-cache-remember/health.svg)

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

###  Alternatives

[wp-media/wp-rocket

Performance optimization plugin for WordPress

7431.3M3](/packages/wp-media-wp-rocket)[rhubarbgroup/redis-cache

A persistent object cache backend for WordPress powered by Redis. Supports Predis, PhpRedis, Relay, replication, sentinels, clustering and WP-CLI.

51795.3k1](/packages/rhubarbgroup-redis-cache)[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.

23517.0k1](/packages/rtcamp-nginx-helper)[rarst/fragment-cache

WordPress plugin for partial and async caching of heavy front-end elements.

14115.0k2](/packages/rarst-fragment-cache)

PHPackages © 2026

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