$cache->get() method

Get data from cache with given name

Optionally specify expiration time and/or a cache generation function to use when cache needs to be created.

Cached value can be a string, an array of non-object values, or a PageArray.

Example

// get single cache value 
$str = $cache->get('foo');

// get 3 cached values, returns associative array with foo, bar, baz indexes
$array = $cache->get([ 'foo', 'bar', 'baz' ]);

// get all cache values with names starting with “hello”
$array = $cache->get('hello*');

// get cache only if it’s less than or equal to 1 hour old (3600 seconds)
$str = $cache->get('foo', 3600);

// same as above, but also generates the cache value with function when expired
$str = $cache->get('foo', 3600, function() {
  return "This is the cached value";
});

Usage

// basic usage
$string = $cache->get($name);

// usage with all arguments
$string = $cache->get($name, $expire = null, callable $func = null);

Arguments

NameType(s)Description
namestring, array

Provide a single cache name, an array of cache names, or an asterisk cache name.

  • If given a single cache name (string) just the contents of that cache will be returned.
  • If given an array of names, multiple caches will be returned, indexed by cache name.
  • If given a cache name with an asterisk in it, it will return an array of all matching caches.
expire (optional)int, string, null

Optionally specify max age (in seconds) OR oldest date string.

  • If cache exists and is older, then blank returned. You may omit this to divert to whatever expiration was specified at save() time. Note: The $expire and $func arguments may optionally be reversed.
  • If using a $func, the behavior of $expire becomes the same as that of save().
func (optional)callable

Optionally provide a function/closure that generates the cache value and it will be used when needed. This option requires that only one cache is being retrieved (not an array of caches). Note: The $expire and $func arguments may optionally be reversed.

Return value

string array PageArray mixed null

Returns null if cache doesn’t exist and no generation function provided.

Exceptions

Method can throw exceptions on error:

  • WireException - if given invalid arguments


$cache methods and properties

API reference based on ProcessWire core version 3.0.137

Twitter updates

  • ProcessWire 3.0.144 and 3.0.145 add improved field template context override settings and include a new Inputfields API, along with numerous other issue fixes, optimizations and improvements to the core. More
    8 November 2019
  • ProcessWire 3.0.144 core updates: Upgraded overrides section in the field editor, a new Inputfields JS API, and more— More
    1 November 2019
  • ProcessWire 3.0.143 core updates — 26 commits and 18 resolved issue reports, plus some other useful improvements and additions. More here: More
    25 October 2019

Latest news

  • ProcessWire Weekly #288
    In the 288th issue of ProcessWire Weekly we'll cover the latest core updates, introduce WireMailSendGrid, host a new weekly poll, and showcase a new site of the week. Read on!
    Weekly.pw / 16 November 2019
  • ProcessWire 3.0.145 core updates
    ProcessWire 3.0.144 and 3.0.145 add improved field template context override settings and include a new Inputfields API, along with numerous other issue fixes, optimizations and improvements to the core.
    Blog / 8 November 2019
  • Subscribe to weekly ProcessWire news

“…building with ProcessWire was a breeze, I really love all the flexibility the system provides. I can’t imagine using any other CMS in the future.” —Thomas Aull