Simple interface to Redis in-memory storage similar to WireCache
ProcessWire interface to Redis memory cache
Compatible with ProcessWire 3.x
Alpha, WiP, use with care
The phpredis extension must be installed.
After installation, you need to enable the module in the module settings by ticking the "Active" checkbox. The server and port are preset to 127.0.0.1 and 6379.
The configuration options support connecting via unix domain sockets and using TLS. You can optionally enable authentication.
CacheRedis registers itself as a PW API variable with the name "redis", so you can access in all the usual ways:
$var = $redis->get('mykey'); $var = wire('redis')->get('otherkey'); $var = $this->redis->get('thirdkey'); // only within Wire derived classes
Retrieve a value from the cache by the given name. If $func is given and no cached entry was found, call $func and use its return value instead, saving that in the cache with the given expiry time.
Alternatively, you can pass an array of names to the method. In that case, an associative array of names and values is returned, with only those keys present that were found in the cache. If you do that, you must not pass a $func parameter.
Save the value in the cache with the given name as the key and the specified expire time. If $expire is not set, CacheRedis::expireDaily is assumed (24h).
Deletes the entry with the given key name from the cache.
Delete all entries from the cache.
This method behaves similar to
$files->render() and actually delegates the file rendering to that method (when creating the cache). The important difference is that this method caches the output according to the given expiry value, rather than re-rendering the file on every call.
If there are any changes to the source file
$filename the cache will be automatically re-created, regardless of what is specified for the
// render primary nav from site/templates/partials/primary-nav.php // and cache for 3600 seconds (1 hour) echo $redis->renderFile('partials/primary-nav.php', 3600);
Parameters for renderFile:
- Filename to render (typically PHP file)
- Can be full path/file, or dir/file relative to current work directory (which is typically /site/templates/).
- If providing a file relative to current dir, it should not start with "/".
- File must be somewhere within site/templates/, site/modules/ or wire/modules/, or provide your own
- Please note that $filename receives API variables already (you don’t have to provide them).
- Specify one of the
- Specify the time in seconds until expiry, up to 30 days.
- Specify the future date you want it to expire (any value above 2592000 will be treated as a unix timestamp).
CacheRedis::expireNeverto prevent expiration.
CacheRedis::expireSaveto expire when any page or template is saved.
- Specify a selector string. In that case, whenever a page is saved that matches the selector, the cache for the file is cleared.
- You can specify multiple selectors as an array. This can make sense with multiple selectors and/or if you want to specify selector expiry and a maximum lifetime.
- Omit for default value, which is
- Specify one of the
$optionsAccepts all options for the
WireFileTools::render()method, plus these additional ones:
name(string): Optionally specify a unique name for this cache, otherwise $filename will be used as the unique name. (default='')
vars(array): Optional associative array of extra variables to send to template file. (default=)
allowedPaths(array): Array of paths that are allowed (default is anywhere within templates, core modules and site modules)
throwExceptions(bool): Throw exceptions when fatal error occurs? (default=true)
string|bool Rendered template file or boolean false on fatal error (and throwExceptions disabled)
Licensed under Mozilla Public License v2. See file LICENSE in the repository for details.
Ryan Cramer, creator of ProcessWire. Quite a bit of his well thought out code was taken more or less literally from his WireCache class to keep things compatible and keep from reinventing the wheel.