You are here

class AccessResultCache in Permissions by Term 8.2

Hierarchy

Expanded class hierarchy of AccessResultCache

1 string reference to 'AccessResultCache'
permissions_by_term.services.yml in ./permissions_by_term.services.yml
permissions_by_term.services.yml
1 service uses AccessResultCache
permissions_by_term.access_result_cache in ./permissions_by_term.services.yml
Drupal\permissions_by_term\Cache\AccessResultCache

File

src/Cache/AccessResultCache.php, line 10

Namespace

Drupal\permissions_by_term\Cache
View source
class AccessResultCache {

  /**
   * The default cache bin.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;
  public function __construct(CacheBackendInterface $cache) {
    $this->cache = $cache;
  }
  public function setAccessResultsCache(int $accountId, $entityId, AccessResult $accessResult) : void {
    $data = \serialize($accessResult);
    $cid = 'permissions_by_term:access_result_cache:' . $entityId . ':' . $accountId;
    $tags = [
      'permissions_by_term:access_result_cache:' . $entityId . ':' . $accountId,
      'permissions_by_term:access_result_cache:' . $entityId,
      'permissions_by_term:access_result_cache',
    ];
    $tags = Cache::mergeTags($tags, [
      $cid,
    ]);
    $this->cache
      ->set($cid, $data, Cache::PERMANENT, $tags);
    $staticCache =& drupal_static(__FUNCTION__ . $cid, NULL);
    $staticCache = $data;
  }
  public function getAccessResultsCache(int $accountId, $entityId) : AccessResult {
    $cid = 'permissions_by_term:access_result_cache:' . $entityId . ':' . $accountId;
    $staticCache =& drupal_static(__FUNCTION__ . $cid, NULL);
    if ($staticCache) {
      return \unserialize($staticCache);
    }
    $result = $this->cache
      ->get($cid);
    $data = \unserialize($result->data);
    if (!$data instanceof AccessResult) {
      throw new \Exception("Unexpected result from cache. Passed accountId: {$accountId} - passed entityId: {$entityId}");
    }
    return $data;
  }
  public function hasAccessResultsCache(int $accountId, $entityId) : bool {
    $cid = 'permissions_by_term:access_result_cache:' . $entityId . ':' . $accountId;
    $staticCache =& drupal_static(__FUNCTION__ . $cid, NULL);
    if ($staticCache) {
      $data = \unserialize($staticCache);
      if (!$data instanceof AccessResult) {
        return FALSE;
      }
      return TRUE;
    }
    $result = $this->cache
      ->get($cid);
    if (!isset($result->data)) {
      return FALSE;
    }
    $data = \unserialize($result->data);
    if (!$data instanceof AccessResult) {
      return FALSE;
    }
    return TRUE;
  }

}

Members