You are here

class JsProxyCache in JS Callback Handler 7.2

JS custom cache handler.

This is just a proxy for the actually configured cache backend, that ensures that Drupal is fully bootstrapped if an item cannot be retrieved from cache. By loading all hook implementations this avoids the risk of having incomplete or corrupt cache entries stored during a callback execution.

Hierarchy

Expanded class hierarchy of JsProxyCache

1 string reference to 'JsProxyCache'
_js_cache_initialize in ./js.module
Initializes the cache system and our custom handler.

File

src/JsProxyCache.php, line 11

View source
class JsProxyCache implements DrupalCacheInterface {
  const DEFAULT_BIN_KEY = 'cache_default_class';

  /**
   * The cache bin configuration.
   *
   * @var string[]
   */
  protected static $conf;

  /**
   * The excluded cache classes configuration.
   *
   * @var string[]
   */
  protected static $excludedConf;

  /**
   * Flag indicating whether a full bootstrap may be performed.
   *
   * @var bool
   */
  protected static $fullBootstrapAllowed = FALSE;

  /**
   * The actual cache backend.
   *
   * @var DrupalCacheInterface
   */
  protected $backend;

  /**
   * Sets the cache bin configuration.
   *
   * @param string[] $conf
   *   An associative array of cache backend class names keyed by their cache
   *   bin name.
   */
  public static function setConf(array $conf) {
    static::$conf = $conf;
  }

  /**
   * Sets the cache bin configuration.
   *
   * @param string[] $conf
   *   An associative array of cache backend class names keyed by their cache
   *   bin name.
   */
  public static function setExcludedConf(array $conf) {
    static::$excludedConf = array_combine($conf, $conf);
  }

  /**
   * Sets the flag indicating whether a full bootstrap can be performed.
   *
   * @param bool $allowed
   *   The full bootstrap flag.
   */
  public static function setFullBootstrapAllowed($allowed) {
    static::$fullBootstrapAllowed = $allowed;
  }

  /**
   * Constructs a JS cache handler.
   *
   * @param string $bin
   *   The cache bin name.
   */
  public function __construct($bin) {
    if (isset(static::$conf[static::DEFAULT_BIN_KEY])) {
      $bin_key = 'cache_class_' . $bin;
      $class = isset(static::$conf[$bin_key]) ? static::$conf[$bin_key] : static::$conf[static::DEFAULT_BIN_KEY];
      $this->backend = new $class($bin);
    }
    else {
      $message = 'The cache backend configuration for the JS proxy cache handler is invalid: @conf';
      throw new LogicException(format_string($message, array(
        '@conf' => print_r(static::$conf, TRUE),
      )));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function get($cid) {
    $cache = $this->backend
      ->get($cid);
    if (!$cache) {
      $this
        ->doFullBootstrap();
    }
    return $cache;
  }

  /**
   * {@inheritdoc}
   */
  public function getMultiple(&$cids) {
    $cache = $this->backend
      ->getMultiple($cids);
    if ($cids) {
      $this
        ->doFullBootstrap();
    }
    return $cache;
  }

  /**
   * {@inheritdoc}
   */
  public function set($cid, $data, $expire = CACHE_PERMANENT) {
    $this->backend
      ->set($cid, $data, $expire);
  }

  /**
   * {@inheritdoc}
   */
  public function clear($cid = NULL, $wildcard = FALSE) {
    $this->backend
      ->clear($cid, $wildcard);
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    return $this->backend
      ->isEmpty();
  }

  /**
   * Fully bootstraps Drupal.
   */
  protected function doFullBootstrap() {

    // If a full bootstrap is allowed and the backend class is not configured as
    // excluded, as it intentionally does not return any cache hits, run a full
    // bootstrap.
    if (static::$fullBootstrapAllowed && !isset(static::$excludedConf[get_class($this->backend)])) {
      static::setFullBootstrapAllowed(FALSE);
      if (drupal_get_bootstrap_phase() < DRUPAL_BOOTSTRAP_FULL) {
        js_bootstrap(DRUPAL_BOOTSTRAP_FULL);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsProxyCache::$backend protected property The actual cache backend.
JsProxyCache::$conf protected static property The cache bin configuration.
JsProxyCache::$excludedConf protected static property The excluded cache classes configuration.
JsProxyCache::$fullBootstrapAllowed protected static property Flag indicating whether a full bootstrap may be performed.
JsProxyCache::clear public function Expires data from the cache. Overrides DrupalCacheInterface::clear
JsProxyCache::DEFAULT_BIN_KEY constant
JsProxyCache::doFullBootstrap protected function Fully bootstraps Drupal.
JsProxyCache::get public function Returns data from the persistent cache. Overrides DrupalCacheInterface::get
JsProxyCache::getMultiple public function Returns data from the persistent cache when given an array of cache IDs. Overrides DrupalCacheInterface::getMultiple
JsProxyCache::isEmpty public function Checks if a cache bin is empty. Overrides DrupalCacheInterface::isEmpty
JsProxyCache::set public function Stores data in the persistent cache. Overrides DrupalCacheInterface::set
JsProxyCache::setConf public static function Sets the cache bin configuration.
JsProxyCache::setExcludedConf public static function Sets the cache bin configuration.
JsProxyCache::setFullBootstrapAllowed public static function Sets the flag indicating whether a full bootstrap can be performed.
JsProxyCache::__construct public function Constructs a JS cache handler.