snicco/better-wp-cache

v2.0.0-beta.9 2024-09-07 14:27 UTC

README

codecov Psalm Type-Coverage Psalm level PhpMetrics - Static Analysis PHP-Versions

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.

Table of contents

  1. Motivation
  2. Installation
  3. Usage
    1. Does this work with any caching plugin?
    2. PSR-6
    3. PSR-16
    4. Cache Tagging
  4. Contributing
  5. Issues and PR's
  6. Security

Motivation

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:

  1. You are developing a distributed WordPress library and don't want to depend on the WP_Object_Cache.
  2. You are using any PHP package that depends on the PSR cache interface, and you want to use your WordPress caching plugin for it.
  3. 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.

Installation

composer require snicco/better-wp-cache

Usage

Does this work with any caching plugin?

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.

Creating a PSR-6 cache

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);

Creating a PSR-16 cache

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);

Cache tagging

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);

Contributing

This repository is a read-only split of the development repo of the Snicco project.

This is how you can contribute.

Reporting issues and sending pull requests

Please report issues in the Snicco monorepo.

Security

If you discover a security vulnerability within BetterWPCache, please follow our disclosure procedure.