Code Monkey home page Code Monkey logo

wordpress-object-cache-helper's Introduction

Author GitHub License Analytics Get Flywheel Twitter

WordPress Object Cache Wrapper Class

A simple MU plugin for WordPress that acts as a wrapper for WP Object Cache functions, with support for flushing cache by group.

It was created as an MU plugin so that it is loaded and available for use in the theme as well as standard custom plugins, useful to cache heavy operations that fall outside of WordPress's built-in caching (such as direct database queries, file and remote operations, etc).

Installation

Simply copy the object-cache-helper.php file to your wp-content/mu-plugins directory (create one if it does not exist).

Usage

Class Instantiation

Without arguments:

$cache = new \MU_Plugins\WP_Cache_Object();

With arguments:

$cache = new \MU_Plugins\WP_Cache_Object([
   'expire' => HOUR_IN_SECONDS * 8,
   'group' => 'my_cache_group'
]);

Getting/Setting Cache Value

In this example, we will retrieve the public IP address of the server from SeeIP using an anonymous function as the callback and cache it for one day:

function get_public_ip_address() {
  $result = wp_remote_get( 'https://ip4.seeip.org' );
  return isset( $result['body'] ) ? $result['body'] : null;
}

$cache = new \MU_Plugins\WP_Cache_Object( [ 'expire' => DAY_IN_SECONDS ] );
$ip_address = $cache->get_object( 'my_server_public_ip_address', 'get_public_ip_address' );

echo 'Public IP Address: ' . $ip_address;

Note that in this example, we have one line of code in the callback function, but you can add as much logic as you like.

Anonymous Function as Callback

In this example, we will do the same as above with an anonymous function as the callback:

$cache = new \MU_Plugins\WP_Cache_Object( [ 'expire' => DAY_IN_SECONDS ] );

$ip_address = $cache->get_object( 'my_server_public_ip_address', function() {
  $result = wp_remote_get( 'https://ip4.seeip.org' );
  return isset( $result['body'] ) ? $result['body'] : null;
});

echo 'Public IP Address: ' . $ip_address;

Passing Variables to Callback Function

This example shows how to pass variables to an anonymous callback. You would not cache this in practice and purely serves as an example for passing variables. In this example, we pass $name and $age to the callback.

$cache = new \MU_Plugins\WP_Cache_Object( [ 'expire' => HOUR_IN_SECONDS * 12 ] );

$name = 'Daniel';
$age = 29;

$greeting = $cache->get_object( 'about_me_string', function() use ( &$name, &$age ) {
  return sprintf( 'Hello %s. You are %d years old.', $name, $age );
});

echo $greeting; // Hello Daniel. You are 29 years old.

Flushing the Cache

You can flush the entire cache using wp_cache_flush(), or you can flush a group that was created using this class with the following:

$cache->flush_group( 'my_cache_group' );

Deleting a Specific Key From a Cache Group

$cache->delete_group_key( 'my_cache_key_name' ); // Removes key from default group

$cache->delete_group_key( 'my_cache_key_name', 'custom_cache_group' ); // Removes key from specific group

Arguments

The following arguments are supported when instantiating the class:

Option Description Type Default
group The group to store the cache key in string {theme_dir}_cache_group
expire Number of seconds to cache the value int 3600 (1 hour)
single Store as single key rather than group array bool false
network_global Set to true to store cache value for entire network, rather than per sub-site bool false
force Always return uncached value, useful for debugging bool false

wordpress-object-cache-helper's People

Contributors

dmhendricks avatar

Watchers

 avatar  avatar

Forkers

wprobot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.