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.

Getting and saving

$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, or null if not found
  • When $func is provided and the cache is missing or expired, the function is called to generate a new value, which is automatically saved and returned.
  • $expire is used as the save expiration when $func is 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 WireCache::expireDaily (24 hours).
  • $data may 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 Page or Template object as $expire. Passing a Page uses 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);
Namespaced caches

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
  • $ns may 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 $name to 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);
Deleting and expiring

$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

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');
Expiration constants

These constants are defined on the WireCache class and can be passed as the $expire argument to get() and save().

ConstantValueDescription
WireCache::expireNow0Expire immediately; on save() clears any existing cache with that name
WireCache::expireHourly3600Expires after 1 hour
WireCache::expireDaily86400Expires after 1 day (default for save())
WireCache::expireWeekly604800Expires after 1 week
WireCache::expireMonthly2419200Expires after ~28 days
WireCache::expireNeverNever expires (must be deleted manually)
WireCache::expireSaveExpires whenever any page is saved or deleted
WireCache::expireSelectorInternal marker for selector expiration; pass a selector string to save()
WireCache::expireIgnorefalseOn get(): return cached value regardless of whether it has expired
Notes
  • Accessible as $cache API variable; source: wire/core/WireCache/WireCache.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 in get() and delete().
  • 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*").
  • expireSave and expireSelector caches are expired by the maintenance() hook that runs automatically on every page save/delete — no manual call needed.
  • expireNever caches persist across all page saves and must be removed with delete() or deleteFor(). Use sparingly.
  • The default cache backend is database (WireCacheDatabase). An alternative backend can be installed as a separate module implementing WireCacheInterface — for example, the WireCacheFilesystem module.
API reference: methods, constants
// 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 WireCache class also inherits all the methods and properties of: Wire.

Show $var?     Show args?       Only hookable?    

Common

NameReturnSummary 
$cache->delete(string $name)
bool

Delete/clear the cache(s) identified by given name or wildcard

 
$cache->deleteAll()
int

Delete all caches (where allowed)

 
$cache->deleteFor(string $ns)
bool

Delete one or more caches in a given namespace

 
$cache->expireAll()
int

Deletes all caches that have expiration dates (only)

 
$cache->get($name)
string array PageArray mixed null

Get data from cache with given name

 
$cache->getCacheModule()
WireCacheInterface

Get WireCache module that is currently being used

 
$cache->getFor($ns, string $name)
string array PageArray mixed null

Same as get() but with namespace

 
$cache->renderFile(string $filename)
string bool

Render a file as a ProcessWire template file and cache the output

 
$cache->save(string $name, $data)
bool

Save data to cache with given name

 
$cache->saveFor($ns, string $name, $data)
bool

Same as save() except with namespace

 
$cache->setCacheModule(WireCacheInterface $module)
None

Set WireCache module to use for caching

 

Advanced

NameReturnSummary 
$cache->getInfo()
array

Get information about all the caches in this WireCache

 
$cache->maintenance()
bool

Cache maintenance removes expired caches

 
$cache->preload(array $names)
None

Preload the given caches, so that they will be returned without query on the next get() call

 
$cache->preloadFor($ns)
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.

NameReturnSummary 
WireCache::defaultCacheClass constWireCacheDatabaseDefault cache class 
WireCache::expireDaily const86400Cache should expire once per day 
WireCache::expireHourly const3600Cache should expire once per hour 
WireCache::expireIgnore constfalseIgnore expiration (skips expiration check) 3.0.218+  
WireCache::expireMonthly const2419200Cache should expire once per month 
WireCache::expireNever const2010-04-08 03:10:10Cache should never expire (unless manually cleared). 
WireCache::expireNow const0Cache should expire now 
WireCache::expireReserved const2010-04-08 03:10:01Cache 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. 
WireCache::expireSave const2010-01-01 01:01:01Cache should expire when a given resource (Page or Template) is saved. 
WireCache::expireWeekly const604800Cache should expire once per week 

Additional methods and properties

In addition to the methods and properties above, WireCache also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.261