You are here

class KeyValueCache in Permissions by Term 8.2

Hierarchy

Expanded class hierarchy of KeyValueCache

1 file declares its use of KeyValueCache
AccessStorage.php in src/Service/AccessStorage.php
1 string reference to 'KeyValueCache'
permissions_by_term.services.yml in ./permissions_by_term.services.yml
permissions_by_term.services.yml
1 service uses KeyValueCache
permissions_by_term.key_value_cache in ./permissions_by_term.services.yml
Drupal\permissions_by_term\Cache\KeyValueCache

File

src/Cache/KeyValueCache.php, line 10

Namespace

Drupal\permissions_by_term\Cache
View source
class KeyValueCache {

  /**
   * The default cache bin.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;
  public function __construct(CacheBackendInterface $cache) {
    $this->cache = $cache;
  }
  public function set(array $data) : void {
    $cid = 'permissions_by_term:key_value_cache';
    $tags = [
      'permissions_by_term:key_value_cache',
    ];
    $tags = Cache::mergeTags($tags, [
      $cid,
    ]);
    $this->cache
      ->set($cid, $data, Cache::PERMANENT, $tags);
    $staticCache =& drupal_static(__FUNCTION__ . $cid, NULL);
    $staticCache = $data;
  }
  public function get() : array {
    $cid = 'permissions_by_term:key_value_cache';
    $staticCache =& drupal_static(__FUNCTION__ . $cid, NULL);
    if ($staticCache) {
      return $staticCache;
    }
    $result = $this->cache
      ->get($cid);
    $data = $result->data;
    if (!is_array($data)) {
      throw new \Exception('Result from cache was not an array.');
    }
    return $data;
  }
  public function has() : bool {
    $cid = 'permissions_by_term:key_value_cache';
    $staticCache =& drupal_static(__FUNCTION__ . $cid, NULL);
    if ($staticCache) {
      $data = $staticCache;
      if (!is_array($data)) {
        return FALSE;
      }
      return TRUE;
    }
    $result = $this->cache
      ->get($cid);
    if (!isset($result->data)) {
      return FALSE;
    }
    $data = $result->data;
    if (!is_array($data)) {
      return FALSE;
    }
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KeyValueCache::$cache protected property The default cache bin.
KeyValueCache::get public function
KeyValueCache::has public function
KeyValueCache::set public function
KeyValueCache::__construct public function