snicco / better-wp-cache
Requires
- php: ^7.4|^8.0
- cache/simple-cache-bridge: ^1.2.0
- cache/tag-interop: ^1.1
- cache/taggable-cache: 1.2.0
- psr/cache: ^1.0.0
- psr/simple-cache: ^1.0.0
- snicco/better-wp-api: ^2.0
Requires (Dev)
- cache/integration-tests: 0.17.0
- codeception/codeception: ^4.1.29
- lucatume/wp-browser: ~3.1.4
- phpunit/phpunit: ^9.5.13
Conflicts
- snicco/better-wp-cache-bundle: <2.0.0-beta.9
- snicco/better-wp-cli: <2.0.0-beta.9
- snicco/better-wp-cli-testing: <2.0.0-beta.9
- snicco/better-wp-hooks: <2.0.0-beta.9
- snicco/better-wp-hooks-bundle: <2.0.0-beta.9
- snicco/better-wp-mail: <2.0.0-beta.9
- snicco/better-wp-mail-bundle: <2.0.0-beta.9
- snicco/better-wp-mail-testing: <2.0.0-beta.9
- snicco/better-wpdb: <2.0.0-beta.9
- snicco/better-wpdb-bundle: <2.0.0-beta.9
- snicco/blade-bridge: <2.0.0-beta.9
- snicco/blade-bundle: <2.0.0-beta.9
- snicco/content-negotiation-middleware: <2.0.0-beta.9
- snicco/debug-bundle: <2.0.0-beta.9
- snicco/default-headers-middleware: <2.0.0-beta.9
- snicco/eloquent: <2.0.0-beta.9
- snicco/encryption-bundle: <2.0.0-beta.9
- snicco/event-dispatcher: <2.0.0-beta.9
- snicco/event-dispatcher-testing: <2.0.0-beta.9
- snicco/guests-only-middleware: <1.0.0
- snicco/http-routing: <2.0.0-beta.9
- snicco/http-routing-bundle: <2.0.0-beta.9
- snicco/http-routing-testing: <2.0.0-beta.9
- snicco/https-only-middleware: <2.0.0-beta.9
- snicco/illuminate-container-bridge: <2.0.0-beta.9
- snicco/kernel: <2.0.0-beta.9
- snicco/kernel-testing: <2.0.0-beta.9
- snicco/method-override-middleware: <2.0.0-beta.9
- snicco/minimal-logger: <2.0.0-beta.9
- snicco/must-match-route-middleware: <2.0.0-beta.9
- snicco/no-robots-middleware: <2.0.0-beta.9
- snicco/open-redirect-protection-middleware: <2.0.0-beta.9
- snicco/payload-middleware: <2.0.0-beta.9
- snicco/pimple-bridge: <2.0.0-beta.9
- snicco/psr7-error-handler: <2.0.0-beta.9
- snicco/redirect-middleware: <2.0.0-beta.9
- snicco/session: <2.0.0-beta.9
- snicco/session-bundle: <2.0.0-beta.9
- snicco/session-psr16-bridge: <2.0.0-beta.9
- snicco/session-testing: <2.0.0-beta.9
- snicco/session-wp-bridge: <2.0.0-beta.9
- snicco/share-cookies-middleware: <2.0.0-beta.9
- snicco/signed-url: <2.0.0-beta.9
- snicco/signed-url-psr15-bridge: <2.0.0-beta.9
- snicco/signed-url-psr16-bridge: <2.0.0-beta.9
- snicco/signed-url-testing: <2.0.0-beta.9
- snicco/signed-url-wp-bridge: <2.0.0-beta.9
- snicco/str-arr: <2.0.0-beta.9
- snicco/templating: <2.0.0-beta.9
- snicco/templating-bundle: <2.0.0-beta.9
- snicco/testable-clock: <2.0.0-beta.9
- snicco/testing-bundle: <2.0.0-beta.9
- snicco/trailing-slash-middleware: <2.0.0-beta.9
- snicco/wp-auth-only-middleware: <2.0.0-beta.9
- snicco/wp-capability-middleware: <2.0.0-beta.9
- snicco/wp-capapility-middleware: <1.0.0
- snicco/wp-guests-only-middleware: <2.0.0-beta.9
- snicco/wp-nonce-middleware: <2.0.0-beta.9
- dev-master
- v2.0.0-beta.9
- v2.0.0-beta.8
- v2.0.0-beta.7
- v2.0.0-beta.6
- v2.0.0-beta.5
- v2.0.0-beta.4
- v2.0.0-beta.3
- v2.0.0-beta.2
- v2.0.0-beta.1
- v1.10.1
- v1.10.0
- v1.9.1
- v1.9.0
- v1.8.1
- v1.8.0
- v1.7.0
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- dev-beta
This package is auto-updated.
Last update: 2024-12-07 15:06:29 UTC
README
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
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:
- 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.
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.