README.md

WP PSR Cache

Object cache implementation for WordPress that acts as an adapter for PSR-6 and PSR-16 caching libraries.

What do PSR-6 and PSR-16 mean?

PSR-6 and PSR-16 are standards established by the PHP-FIG organization. These standards are commonly used in PHP projects of any kind (WordPress is unfortunately an exception), and since this library acts as an adapter, you can use any compatible caching library of your choice with WordPress now. Popular examples include the Symfony Cache Component or Stash.

Features

Any PSR-6 or PSR-16 cache implementation can be used

Persistent and non-persistent cache implementations can be individually specified

Support for reading/writing/deleting multiple cache keys at once

Only checks persistent cache if value not already present in non-persistent cache

Full multisite support, including site and network switching

Allows registration of further cache implementations for fine-grained control per cache group

How to Install

Require this library as a dependency when managing your project with Composer (for example by using Bedrock). You also have to install an actual PSR-6 or PSR-16 cache implementation.

After the installation, you need to move the includes/object-cache.php file into your wp-content directory. If you prefer, you can also automate that process by adding the following to your project's composer.json:

Then, replace the inline comment in the object-cache.php file with the actual instantiations of the classes you want to use. You need to provide two implementations, one for the persistent cache and another for the non-persistent cache.

To prevent conflicts with multiple WordPress installations accessing the same cache service, it is recommended to define a unique WP_CACHE_KEY_PREFIX constant in your wp-config.php file.

Example

The following example uses the symfony/cache library, so you have to require it in your composer.json. It then uses that library's Memcached implementation as persistent cache and its array storage as non-persistent cache.

If you prefer to have more granular control and use more than just one persistent and one non-persistent cache, you can register additional cache adapters using the LeavesAndLove\WpPsrCache\CacheSelector\CacheSelector interface. The implementation used by the object cache can easily be retrieved via wp_object_cache()->getSelector().