alexsancho/wp-redis-group-cache

A plugin extending the Redis Object Cache for WordPress with a group cache functionality

Installs: 246

Dependents: 1

Suggesters: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:wordpress-plugin

v1.0.2 2018-03-28 11:12 UTC

README

This plugin enables group caching for sites using the Redis Object Cache for WordPress dropin.

Functionalities

Creating the cache group

Defining a group for WP Object Cache items enables simultaneous cache invalidation for a set of cache items. See the codex for further information on setting the group key. The following functions create the object cache functionality by hooking to the Redis Object Cache dropin:

add_to_group

This function is hooked to the cache setting with the wp_cache_set function. If a group key is set for wp_cache_set, the cache key is pushed into a Redis hash list mapped by the group key.

delete_from_group

This function is hooked to the cache deletion with the wp_cache_delete function. If a group key is set for wp_cache_delete, the specified item key is removed from the group list. This ensures the group only has keys that actually exist in the object cache.

Invalidating a cache group

delete_group

This function deletes all data related to a group key by first fetching all keys and deleting them from Redis and then deleting the Redis hash list of the group.

Usage:

\Asancho\GroupCache::delete_group( $group_key );

Excluding groups from caching

The Redis dropin automatically caches all data stored with WordPress Object Cache into Redis. If you want to exclude some groups from being stored into the group cache, return true from the no_group_cache filter for the corresponding group key.

function no_group_cache( $group, $key ) {
    if ( 'no_caching_key' === $group ) {
        return true;
    } else {
        return false;
    }
}
add_filter( 'redis/cache/no_group_cache', 'no_group_cache', 1, 2 );

Note that this does not disable the initial key-value caching!