function protected function ProviderBase::cacheGet

8.x-3.x ProviderBase.php protected ProviderBase::cacheGet($type, $key = NULL, $default = NULL, callable $builder = NULL)
7.x-3.x ProviderBase.php protected ProviderBase::cacheGet($key, $default = NULL, $builder = NULL)

Retrieves a value from the CDN Provider cache.

Parameters

string $type: The type of cache item to retrieve.

string $key: Optional. A specific key of the item to retrieve. Note: this can be in the form of dot notation if the value is nested in an array. If not provided, the entire contents of $name will be returned.

mixed $default: Optional. The default value to return if $key is not set.

callable $builder: Optional. If provided, a builder will be invoked when there is no cache currently set. The return value of the build will be used to set the cached value, provided there are no CDN Provider exceptions generated. If there are, but you still need the cache to be set, reset them prior to returning from the builder callback.

Return value

mixed The cached value if it's set or the value supplied to $default if not.

Class

ProviderBase
CDN Provider base class.

Namespace

Drupal\bootstrap\Plugin\Provider

Source src/Plugin/Provider/ProviderBase.php (line 155)

protected function cacheGet($type, $key = NULL, $default = NULL, callable $builder = NULL) {
  $ttl = $this->getCacheTtl($type);
  $never = $ttl === static::TTL_NEVER;
  $forever = $ttl === static::TTL_FOREVER;
  $cache = $forever ? $this->getKeyValue() : $this->getKeyValueExpirable();

  $data = $cache->get($type, []);

  if (!isset($key)) {
    return $data;
  }

  $parts = Unicode::splitDelimiter($key);
  $value = NestedArray::getValue($data, $parts, $key_exists);

  // Build the cache.
  if (!$key_exists && $builder) {
    $value = $builder($default);
    if (!isset($value)) {
      $value = $default;
    }
    NestedArray::setValue($data, $parts, $value);

    // Only set the cache if no CDN Provider exceptions were thrown.
    if (!$this->cdnExceptions && !$never) {
      if ($forever) {
        $cache->set($type, $data);
      }
      else {
        $cache->setWithExpire($type, $data, $ttl);
      }
    }

    return $value;
  }

  return $key_exists ? $value : $default;
}