felixarntz / wp-psr-cache
Object cache implementation for WordPress that acts as an adapter for PSR-6 and PSR-16 caching libraries.
Installs: 215
Dependents: 0
Suggesters: 0
Security: 0
Stars: 33
Watchers: 4
Forks: 4
Open Issues: 2
Type:wordpress-muplugin
Requires
- php: ^7.0
- composer/installers: ~1.0
- psr/cache: *
- psr/simple-cache: *
Requires (Dev)
- phpunit/phpunit: 6.2.*
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-12-06 10:26:33 UTC
README
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
:
"scripts": {
"post-install-cmd": [
"cp -rp web/app/mu-plugins/wp-psr-cache/includes/object-cache.php web/app/object-cache.php"
]
}
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.
<?php /** * Object cache drop-in * * @package LeavesAndLove\WpPsrCache * @license GNU General Public License, version 2 * @link https://github.com/felixarntz/wp-psr-cache */ use LeavesAndLove\WpPsrCache\ObjectCacheService; use Symfony\Component\Cache\Simple\MemcachedCache; use Symfony\Component\Cache\Simple\ArrayCache; defined( 'ABSPATH' ) || exit; ObjectCacheService::loadApi(); /** * Defines and thus starts the object cache. * * @since 1.0.0 */ function wp_psr_start_cache() { $memcached = new Memcached(); $memcached->addServer( '127.0.0.1', 11211, 20 ); wp_cache_start( new MemcachedCache( $memcached ), new ArrayCache() ); } wp_psr_start_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()
.
Requirements
- PHP >= 7.0
Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!