You are here

class PersistableCache in RESTful 7.2

Hierarchy

Expanded class hierarchy of PersistableCache

1 file declares its use of PersistableCache
RestfulManager.php in src/RestfulManager.php
Contains \Drupal\restful\RestfulManager.

File

src/Util/PersistableCache.php, line 10
Contains \Drupal\restful\Util\PersistableCache.

Namespace

Drupal\restful\Util
View source
class PersistableCache implements PersistableCacheInterface {

  /**
   * The data array.
   *
   * @var array
   */
  protected $data = array();

  /**
   * Tracks the loaded keys.
   *
   * @var string[]
   */
  protected $loaded = array();

  /**
   * The cache bin where to store the caches.
   *
   * @var string
   */
  protected $cacheBin = 'cache';

  /**
   * PersistableCache constructor.
   *
   * @param string $cache_bin
   *   The cache bin where to store the persisted cache.
   */
  public function __construct($cache_bin = NULL) {
    $this->cacheBin = $cache_bin ?: 'cache';
  }

  /**
   * {@inheritdoc}
   */
  public function contains($key) {
    return isset($this->data[$key]);
  }

  /**
   * {@inheritdoc}
   */
  public function &get($key) {
    if (!$this
      ->contains($key)) {

      // Load from the real cache if it's not loaded yet.
      $this
        ->load($key);
    }
    if (!$this
      ->contains($key)) {
      $this->data[$key] = NULL;
    }
    return $this->data[$key];
  }

  /**
   * {@inheritdoc}
   */
  public function set($key, $value) {
    $this->data[$key] = $value;
  }

  /**
   * {@inheritdoc}
   */
  public function delete($key) {
    unset($this->data[$key]);
  }

  /**
   * {@inheritdoc}
   */
  public function persist() {
    foreach ($this->data as $key => $value) {
      cache_set($key, $value, $this->cacheBin);
    }
  }

  /**
   * Persist the data in the cache backend during shutdown.
   */
  public function __destruct() {
    $this
      ->persist();
  }

  /**
   * Checks if a key was already loaded before.
   *
   * @param string $key
   *   The key to check.
   *
   * @return bool
   *   TRUE if it was loaded before. FALSE otherwise.
   */
  protected function isLoaded($key) {
    return isset($this->loaded[$key]);
  }

  /**
   * Tries to load an item from the real cache.
   *
   * @param string $key
   *   The key of the item.
   */
  protected function load($key) {
    if ($this
      ->isLoaded($key)) {
      return;
    }

    // Mark the key as loaded.
    $this->loaded[$key] = TRUE;
    if ($cache = cache_get($key, $this->cacheBin)) {
      $this->data[$key] = $cache->data;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PersistableCache::$cacheBin protected property The cache bin where to store the caches.
PersistableCache::$data protected property The data array.
PersistableCache::$loaded protected property Tracks the loaded keys.
PersistableCache::contains public function Checks if the cache contains the key. Overrides PersistableCacheInterface::contains
PersistableCache::delete public function Delete a cached item. Overrides PersistableCacheInterface::delete
PersistableCache::get public function Gets the memory reference of the cached item. Overrides PersistableCacheInterface::get
PersistableCache::isLoaded protected function Checks if a key was already loaded before.
PersistableCache::load protected function Tries to load an item from the real cache.
PersistableCache::persist public function Persist the cache to the RESTful cache. Overrides PersistableCacheInterface::persist
PersistableCache::set public function Gets the memory reference of the cached item. Overrides PersistableCacheInterface::set
PersistableCache::__construct public function PersistableCache constructor.
PersistableCache::__destruct public function Persist the data in the cache backend during shutdown.