BetterWPCache is a tiny library that allows you to use the persistent WordPress object cache as a PSR-16 cache or a PSR-6 cache.
Additionally, BetterWPCache supports cache tagging.
This library is 100% integration tested against the official php-cache/integration-tests using the Redis Object Cache plugin by Till Krüss.
We developed this library because many WordPress related components of the Snicco project require some form of caching.
To offer the greatest flexibility for users, they all only depend on the PSR cache interfaces.
But using these interfaces' meant that there was no way of using these components inside WordPress
with the already connected WP_Object_Cache
.
BetterWPCache solves this need.
This library has three main use cases:
- You are developing a distributed WordPress library and don't want to depend on the
WP_Object_Cache
. - You are using any PHP package that depends on the PSR cache interface, and you want to use your WordPress caching plugin for it.
- You want cache-tagging, which is something that the
WP_Object_Cache
does not support.
Ideally, WordPress core would replace the custom WP_Object_Cache
with the PSR cache interface instead, so that
the need for this library vanishes. In the meantime BetterWPCache will do the job.
composer require snicco/better-wp-cache
Short answer: Yes!
Long answer: As long as your caching plugin correctly implements the wp_cache_xxx
functions. This library only uses
the
official WordPress cache functions.
This is how you create a PSR-6 cache from your favorite WordPress cache plugin.
use Snicco\Component\BetterWPCache\CacheFactory;
$cache_group = 'my_plugin';
$psr_6_cache = CacheFactory::psr6($cache_group);
This is how you create a PSR-16 cache from your favorite WordPress cache plugin.
use Snicco\Component\BetterWPCache\CacheFactory;
$cache_group = 'my_plugin';
$psr_16_cache = CacheFactory::psr16($cache_group);
For cache tagging a PSR-6 cache is needed.
use Snicco\Component\BetterWPCache\CacheFactory;
$cache_group = 'my_plugin';
$psr_6_cache = CacheFactory::psr6($cache_group);
$taggable_cache = CacheFactory::taggable($psr_6_cache);
$taggable_cache
is an instance of TaggableCacheItemPoolInterface which
is in the process of becoming a PSR standard.
Taken from the official documentation:
use Snicco\Component\BetterWPCache\CacheFactory;
$cache = CacheFactory::taggable(CacheFactory::psr6('my_plugin'));
$item = $cache->getItem('tobias');
$item->set('value')->setTags(['tag0', 'tag1'])
$cache->save($item);
$item = $cache->getItem('aaron');
$item->set('value')->setTags(['tag0']);
$cache->save($item);
// Remove everything tagged with 'tag1'
$cache->invalidateTags(['tag1']);
$cache->getItem('tobias')->isHit(); // false
$cache->getItem('aaron')->isHit(); // true
$item = $cache->getItem('aaron');
echo $item->getPreviousTags(); // array('tag0')
// No tags will be saved again. This is the same as saving
// an item with no tags.
$cache->save($item);
This repository is a read-only split of the development repo of the Snicco project.
This is how you can contribute.
Please report issues in the Snicco monorepo.
If you discover a security vulnerability within BetterWPCache, please follow our disclosure procedure.