$cache API variable / WireCache class

Provides easy, persistent caching of markup, strings, arrays or PageArray objects.

// 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?        

Common

NameReturnSummary 
$cache->delete(string $name)
boolDelete/clear the cache(s) identified by given name or wildcard 
$cache->deleteAll()
intDelete all caches (where allowed) 
$cache->deleteFor(string $ns)
boolDelete one or more caches in a given namespace 
$cache->expireAll()
intDeletes all caches that have expiration dates (only) 
$cache->get($name)
string array PageArray mixed nullGet data from cache with given name 
$cache->getCacheModule()
WireCacheInterfaceGet WireCache module that is currently being used 
$cache->getFor($ns, string $name)
string array PageArray mixed nullSame as get() but with namespace 
$cache->renderFile(string $filename)
string boolRender a file as a ProcessWire template file and cache the output 
$cache->save(string $name, $data)
boolSave data to cache with given name 
$cache->saveFor($ns, string $name, $data)
boolSame as save() except with namespace 
$cache->setCacheModule(WireCacheInterface $module)
(nothing)Set WireCache module to use for caching 

Advanced

NameReturnSummary 
$cache->getInfo()
arrayGet information about all the caches in this WireCache 
$cache->maintenance()
boolCache maintenance removes expired caches 
$cache->preload(array $names)
(nothing)Preload the given caches, so that they will be returned without query on the next get() call 
$cache->preloadFor($ns)
(nothing)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.236

Latest news

  • ProcessWire Weekly #531
    In the 531st issue of ProcessWire Weekly we’ll check out the latest weekly update from Ryan, introduce new third party modules, and more. Read on!
    Weekly.pw / 13 July 2024
  • Page List Custom Children module
    This simple module gives you the ability to customize the parent/child relationship as it appears in the admin page list, enabling child pages to appear under more than one parent.
    Blog / 28 June 2024
  • Subscribe to weekly ProcessWire news

“We chose ProcessWire because of its excellent architecture, modular extensibility and the internal API. The CMS offers the necessary flexibility and performance for such a complex website like superbude.de. ProcessWire offers options that are only available for larger systems, such as Drupal, and allows a much slimmer development process.” —xport communication GmbH