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).
Simply copy the object-cache-helper.php
file to your wp-content/mu-plugins
directory (create one if it does not exist).
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'
]);
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.
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;
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.
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' );
$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
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 |