Skip to content

Helper class for the WordPress object cache and transients with group flush feature

License

Notifications You must be signed in to change notification settings

duckdev/wp-cache-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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

The recommended way to install this library in your project is via Composer:

$ composer require duckdev/wp-cache-helper

Usage

WP Cache Remember provides the following functions for WordPress:

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

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

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

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

Retrieve and subsequently delete a value from the object cache.

Parameters

(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

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

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

$cache->persist()

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

Parameters

(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

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

Retrieve and subsequently delete a value from the transient cache.

Parameters

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

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

Parameters

(string) $group
The cache group name.

$cache->flush()

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

Credits

License

GPLv2+