WireCache / $cache
$cache is the API variable for ProcessWire's built-in persistent cache
It stores and retrieves strings, arrays, and PageArray objects, with configurable expiration based on time, page saves, or template saves.
$cache is accessible in templates as $cache, wire()->cache, or cache() (if
functions API enabled); and in modules as $this->wire()->cache.
$cache->get($name, $expire, $func)
Get a cached value by name.
- Arguments:
get(string|array $name, int|string|callable|null $expire = null, callable $func = null) - Returns:
string|array|PageArray|mixed|null— cached value, ornullif not found - When
$funcis provided and the cache is missing or expired, the function is called to generate a new value, which is automatically saved and returned. $expireis used as the save expiration when$funcis provided, not as a filter.- Pass an array of names to retrieve multiple caches at once (returns associative array). Requested names that do not exist are present in the returned array with a blank string value.
- Wildcard names (
"MyModule*") retrieve all matching caches.
// Get a cache (returns null if not found or expired)
$value = $cache->get('my-cache');
// Get with max-age check (returns null if older than 1 hour)
$value = $cache->get('my-cache', 3600);
// Get with generation function (most common pattern)
$value = $cache->get('my-cache', 3600, function() {
return "expensive result";
});
// Get multiple caches at once
$values = $cache->get(['cache-a', 'cache-b']);
// $values['cache-a'] and $values['cache-b']
// Get all caches matching a wildcard
$values = $cache->get('MyModule*');$cache->save($name, $data, $expire)
Save a value to the cache.
- Arguments:
save(string $name, string|array|PageArray $data, int|string|Page|Template $expire = WireCache::expireDaily) - Returns:
bool - Default expiration is
Wire(24 hours).Cache::expireDaily $datamay be a string, a flat array (no nested objects), or a PageArray.- To expire when any page using a given template is saved, pass a
PageorTemplateobject as$expire. Passing aPageuses that page's template — it does not expire on saves of that specific page alone. - To expire when any page matching a selector is saved, pass the selector as a string.
// Save with 1-hour expiration
$cache->save('my-cache', 'Hello world', 3600);
// Save with array data (default daily expiration)
$cache->save('my-data', ['foo' => 'bar', 'num' => 42]);
// Save until any page is saved
$cache->save('nav-html', $navMarkup, WireCache::expireSave);
// Save until any page using $page's template is saved (not just $page itself)
$cache->save('page-nav', $html, $page);
// Save until any page using a specific template is saved
$cache->save('blog-list', $html, $templates->get('blog-post'));
// Expire when a page matching a selector is saved (pass selector as string)
$cache->save('blog-list', $html, 'template=blog-post');
// Never expire
$cache->save('site-config', $data, WireCache::expireNever); Use the For variants to automatically scope caches to a module or object namespace,
which keeps cache names unique and makes bulk deletion easy.
$cache->getFor($ns, $name, $expire, $func)
Get a namespaced cache. Equivalent to get("NamespaceName__$name", ...).
- Arguments:
getFor(string|object $ns, string $name, int|string|callable|null $expire = null, callable $func = null) - Returns:
string|array|PageArray|mixed|null $nsmay be a string or any object (its class name is used).
// In a module, use $this as the namespace
$value = $cache->getFor($this, 'results', 3600, function() {
return $this->buildResults();
});
// String namespace
$value = $cache->getFor('MyModule', 'results', 3600);$cache->saveFor($ns, $name, $data, $expire)
Save a namespaced cache.
- Arguments:
saveFor(string|object $ns, string $name, string|array|PageArray $data, int|Page $expire = WireCache::expireDaily) - Returns:
bool
$cache->saveFor($this, 'results', $data, 3600);
$cache->saveFor('MyModule', 'settings', $settingsArray, WireCache::expireNever); $cache->deleteFor($ns, $name)
Delete a namespaced cache, or all caches in a namespace.
- Arguments:
deleteFor(string|object $ns, string $name = '') - Returns:
bool - Omit
$nameto delete all caches in the namespace.
// Delete a specific namespaced cache
$cache->deleteFor($this, 'results');
// Delete all caches for this namespace
$cache->deleteFor($this);$cache->preloadFor($ns, $expire)
Preload all caches in a namespace into memory in a single query (see Preloading below).
- Arguments:
preloadFor(string|object $ns, int|string|null $expire = null)
$cache->preloadFor($this);$cache->delete($name)
Delete a cache by name, or all caches matching a wildcard.
- Arguments:
delete(string $name) - Returns:
bool
$cache->delete('my-cache');
// Delete all caches starting with "MyModule__"
$cache->delete('MyModule*');$cache->deleteAll()
Delete all caches (except those with expireReserved status, used internally).
- Returns:
int— number of caches deleted
$cache->deleteAll();$cache->expireAll()
Delete all caches with normal date/time expirations. Caches using expireNever,
expireSave, selector expiration, template/page-save expiration, or expireReserved
are not affected.
- Returns:
int— number of caches expired
$cache->expireAll();Preloading methods are legacy/deprecated helpers. They still work, but new code usually does not need them.
$cache->preload($names, $expire)
Preload multiple caches from the database in a single query so subsequent get() calls
for those names return immediately from memory.
- Arguments:
preload(array $names, int|string|null $expire = null)
// Preload several caches before using them
$cache->preload(['header-html', 'nav-html', 'footer-html']);
// Now each get() is served from memory, no DB query
$header = $cache->get('header-html');
$nav = $cache->get('nav-html');
$footer = $cache->get('footer-html');These constants are defined on the Wire class and can be passed as the $expire
argument to get() and save().
| Constant | Value | Description |
|---|---|---|
Wire | 0 | Expire immediately; on save() clears any existing cache with that name |
Wire | 3600 | Expires after 1 hour |
Wire | 86400 | Expires after 1 day (default for save()) |
Wire | 604800 | Expires after 1 week |
Wire | 2419200 | Expires after ~28 days |
Wire | — | Never expires (must be deleted manually) |
Wire | — | Expires whenever any page is saved or deleted |
Wire | — | Internal marker for selector expiration; pass a selector string to save() |
Wire | false | On get(): return cached value regardless of whether it has expired |
- Accessible as
$cacheAPI variable; source:wire/core/Wire.Cache/Wire Cache.php - Data must be a string, a flat array (no nested objects), or a
PageArray. Other objects are not supported unless they implement__toString(). - Cache names should be 190 characters or fewer. Use
cacheName()for long or generated names. Use*as a wildcard suffix inget()anddelete(). - The
getFor()/saveFor()namespace is just a"NamespaceName__"prefix on the name. You can inspect or delete namespaced caches with the wildcard form$cache->delete("MyModule*"). expireSaveandexpireSelectorcaches are expired by themaintenance()hook that runs automatically on every page save/delete — no manual call needed.expireNevercaches persist across all page saves and must be removed withdelete()ordeleteFor(). Use sparingly.- The default cache backend is database (
Wire). An alternative backend can be installed as a separate module implementingCacheDatabase Wire— for example, the WireCacheInterface CacheFilesystem module.
// Get a cache named 'foo' that lasts for 1 hour (aka 3600 seconds)
$value = $cache->get('foo', 3600, function() {
// this is called if cache expired or does not exist,
// so generate a new cache value here and return it
return "This is the cached value";
});Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Wire class also inherits all the methods and properties of: Wire.
Common
Advanced
| Name | Return | Summary | |
|---|---|---|---|
$cache->getInfo() $cache->getInfo() $cache->getInfo(bool $verbose = true, $names = [], $exclude = [], array $cols = []) | array | Get information about all the caches in this Wire | |
$cache->maintenance() $cache->maintenance() $cache->maintenance($obj = null) | bool | Cache maintenance removes expired caches | |
$cache->preload() $cache->preload(array $names) $cache->preload(array $names, $expire = null) | None | Preload the given caches, so that they will be returned without query on the next get() call | |
$cache->preloadFor() $cache->preloadFor($ns) $cache->preloadFor($ns, $expire = null) | None | Preload all caches for the given object or namespace |
Constants
These constants are used for the $expire argument of get() and save() cache methods.
| Name | Return | Summary | |
|---|---|---|---|
| Wire | Wire | Default cache class | |
| Wire | 86400 | Cache should expire once per day | |
| Wire | 3600 | Cache should expire once per hour | |
| Wire | false | Ignore expiration (skips expiration check) 3.0.218+ | |
| Wire | 2419200 | Cache should expire once per month | |
| Wire | 2010-04-08 03:10:10 | Cache should never expire (unless manually cleared). | |
| Wire | 0 | Cache should expire now | |
| Wire | 2010-04-08 03:10:01 | Cache should never expire and should not be deleted during deleteAll() calls (for PW internal system use) Can only be deleted by delete() calls that specify it directly or match it specifically with a wildcard. | |
| Wire | 2010-01-01 01:01:01 | Cache should expire when a given resource (Page or Template) is saved. | |
| Wire | 604800 | Cache should expire once per week |
Additional methods and properties
In addition to the methods and properties above, Wire
API reference based on ProcessWire core version 3.0.261