Download the PHP package duckdev/wp-cache-helper without Composer
On this page you can find all versions of the php package duckdev/wp-cache-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download duckdev/wp-cache-helper
More information about duckdev/wp-cache-helper
Files in duckdev/wp-cache-helper
Package wp-cache-helper
Short Description Helper class for the WordPress object cache and transients with group flush feature.
License GPL-2.0-or-later
Informations about the package wp-cache-helper
WP Cache Helper
WP Cache Helper is a simple WordPress library class to introduce convenient new caching functions.
Built to support group cache flush for WordPress' object cache, which is not supported by core yet.
- Inspired from WP Cache Remember.
This helper can simplify something like this:
That pattern works well, but there's a lot of repeated code. This package draws inspiration from Laravel's Cache::remember()
method; using Cache::remember()
, the same code from above becomes:
Installation
The recommended way to install this library in your project is via Composer:
Usage
WP Cache Remember provides the following functions for WordPress:
$cache->remember()
$cache->forget()
$cache->persist()
$cache->cease()
$cache->flush_group()
$cache->flush()
Each function checks the response of the callback for a WP_Error
object, ensuring you're not caching temporary errors for long periods of time. PHP Exceptions will also not be cached.
$cache->remember()
Retrieve a value from the object cache. If it doesn't exist, run the $callback
to generate and cache the value.
Parameters
- (string) $key
- The cache key.
- (callable) $callback
- The callback used to generate and cache the value.
- (string) $group
- Optional. The cache group. Default is empty.
- (int) $expire
- Optional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).
Example
$cache->forget()
Retrieve and subsequently delete a value from the object cache.
Parameters
- (string) $key
- The cache key.
- (string) $group
- Optional. The cache group. Default is empty.
- (mixed) $default
- Optional. The default value to return if the given key doesn't exist in the object cache. Default is null.
Example
$cache->persist()
Retrieve a value from transients. If it doesn't exist, run the $callback
to generate and cache the value.
Parameters
- (string) $key
- The cache key.
- (callable) $callback
- The callback used to generate and cache the value.
- (string) $site
- Should use site transients.
- (int) $expire
- Optional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).
Example
$cache->cease()
Retrieve and subsequently delete a value from the transient cache.
Parameters
- (string) $key
- The cache key.
- (string) $site
- Should use site transients.
- (mixed) $default
- Optional. The default value to return if the given key doesn't exist in transients. Default is null.
$cache->flush_group()
Flush a cache group items. Use this and do not flush entire cache.
Parameters
- (string) $group
- The cache group name.
$cache->flush()
Wrapper for wp_cache_flush
to check if other method is available for flushing if wp_cache_flush
is disabled.
Credits
- Maintained by Joel James