All Projects → stevegrunwell → Wp Cache Remember

stevegrunwell / Wp Cache Remember

Licence: mit
Helper for the WordPress object cache and transients.

Projects that are alternatives of or similar to Wp Cache Remember

Wp Sitemaps
Proposal to integrate basic XML Sitemaps in WordPress Core
Stars: ✭ 94 (-15.32%)
Mutual labels:  wordpress, wordpress-plugin
S3 Uploads
The WordPress Plugin to Store Uploads on Amazon S3
Stars: ✭ 1,573 (+1317.12%)
Mutual labels:  wordpress, wordpress-plugin
Acf Star Rating Field
A simple star rating field for ACF.
Stars: ✭ 94 (-15.32%)
Mutual labels:  wordpress, wordpress-plugin
Searchwp Live Ajax Search
[WordPress Plugin] Enhance your search forms with live search (utilizes SearchWP if installed)
Stars: ✭ 91 (-18.02%)
Mutual labels:  wordpress, wordpress-plugin
Debug Objects
WordPress Plugin for debugging and learning with and at the application.
Stars: ✭ 98 (-11.71%)
Mutual labels:  wordpress, wordpress-plugin
Dominant Colors Lazy Loading Wordpress Plugin
This plugin allows you to lazy load your images while showing the dominant color of each image as a placeholder – like Pinterest or Google Images.
Stars: ✭ 93 (-16.22%)
Mutual labels:  wordpress, wordpress-plugin
Wordpress Https
WordPress HTTPS is intended to be an all-in-one solution to using SSL on WordPress sites.
Stars: ✭ 96 (-13.51%)
Mutual labels:  wordpress, wordpress-plugin
Jetpack
Security, performance, marketing, and design tools — Jetpack is made by the WordPress experts to make WP sites safer and faster, and help you grow your traffic.
Stars: ✭ 1,283 (+1055.86%)
Mutual labels:  wordpress, wordpress-plugin
Wp model
A simple drop-in abstract class for creating active record style eloquent-esque models of WordPress Posts
Stars: ✭ 98 (-11.71%)
Mutual labels:  wordpress, wordpress-plugin
Wp Crontrol
WP Crontrol lets you view and control what's happening in the WP-Cron system.
Stars: ✭ 97 (-12.61%)
Mutual labels:  wordpress, wordpress-plugin
Phpcompat
WordPress Plugin: PHP Compatibility Checker
Stars: ✭ 106 (-4.5%)
Mutual labels:  wordpress, wordpress-plugin
Wp Postviews
Enables you to display how many times a post/page had been viewed.
Stars: ✭ 107 (-3.6%)
Mutual labels:  wordpress, wordpress-plugin
Wp Less
WordPress plugin which seemlessly compiles, caches and rebuilds your LESS stylesheets.
Stars: ✭ 88 (-20.72%)
Mutual labels:  wordpress, wordpress-plugin
Plugin.wordpress
📦 Official WordPress Plugin of EnlighterJS
Stars: ✭ 93 (-16.22%)
Mutual labels:  wordpress, wordpress-plugin
Wp Functions List
This is a list of all WordPress functions from version 0 to version 4.8.1 along with the data of when they were first introduced and if they are deprecated or not
Stars: ✭ 88 (-20.72%)
Mutual labels:  wordpress, wordpress-plugin
Dynamic Featured Image
Dynamically adds multiple featured image (post thumbnail) functionality to posts, pages and custom post types
Stars: ✭ 96 (-13.51%)
Mutual labels:  wordpress, wordpress-plugin
Remove Comments Absolutely
WordPress Plugin to deactivate comments functions and remove areas absolute form the WordPress install
Stars: ✭ 86 (-22.52%)
Mutual labels:  wordpress, wordpress-plugin
Popup Maker
Popup Maker plugin for WordPress
Stars: ✭ 87 (-21.62%)
Mutual labels:  wordpress, wordpress-plugin
Login Designer
Official repository of the Login Designer WordPress Plugin
Stars: ✭ 97 (-12.61%)
Mutual labels:  wordpress, wordpress-plugin
Wp Help
#WordPressPlugin: Site operators can create detailed, hierarchical documentation for the site's authors, editors, and contributors, viewable in the WordPress admin
Stars: ✭ 99 (-10.81%)
Mutual labels:  wordpress, wordpress-plugin

WP Cache Remember

Build Status Coverage Status GitHub release

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

The best way to install this package is via Composer:

$ composer require stevegrunwell/wp-cache-remember

The package ships with the composer/installers package, 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

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

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

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.

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

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

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

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

remember_transient()

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

forget_transient()

Retrieve and subsequently delete a value from the transient cache.

Parameters

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

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(), but works network-wide when using WordPress Multisite.

forget_site_transient()

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

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].