cap-group-oy/redis-group-cache

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

This package's canonical repository appears to be gone and the package has been frozen as a result.

Installs: 193

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 4

Type:wordpress-muplugin

4.0.0 2021-06-15 07:53 UTC

README

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

Installation

Install with composer:

$ composer require cap-group-oy/redis-group-cache

OR add it into your composer.json:

{
  "require": {
    "cap-group-oy/redis-group-cache": "^4.0.0"
  }
}

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:

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

Excluding custom or including default groups to caching

The Redis dropin automatically caches all data stored with WordPress Object Cache into Redis. If you want to modify which groups are stored you can use the two filters cap/cache/no_group_cache/blacklist and cap/cache/no_group_cache.

Modifying the blacklist

If you want to include some default groups like acf or transient or exclude custom ones for example from plugins that use it, you can use the cap/cache/no_group_cache/blacklist filter to modify an array of blacklisted groups.

function group_cache_blacklist( array $groups ) : array {
	$groups[] = 'customgroup';
	return $groups;
}
add_filter( 'cap/cache/no_group_cache/blacklist', 'group_cache_blacklist', 1, 1 );

Exluded groups by default

classes/cache.php

More specific selection if a group should be excluded

You can do a more direct check if a group should be exluded by using the cap/cache/no_group_cache filter. Note this is applied after the blacklist check, so if the group is already on the blacklist this will not be checked.

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

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

Example

See this Github gist for a usage example.

Maintainers

@jaakkolehtonen

License

GPLv3