You are here

abstract class DrupalMemcacheBase in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 modules/memcache/src/DrupalMemcacheBase.php \Drupal\memcache\DrupalMemcacheBase

Class DrupalMemcacheBase.

Hierarchy

Expanded class hierarchy of DrupalMemcacheBase

File

modules/memcache/src/DrupalMemcacheBase.php, line 16
Contains \Drupal\memcache\DrupalMemcacheBase.

Namespace

Drupal\memcache
View source
abstract class DrupalMemcacheBase implements DrupalMemcacheInterface {

  /**
   * The cache bin name.
   *
   * @var string
   */
  protected $bin;

  /**
   * The settings object.
   *
   * @var \Drupal\Core\Site\Settings
   */
  protected $settings;

  /**
   * The memcache object.
   *
   * @var mixed
   *   E.g. \Memcache|\Memcached
   */
  protected $memcache;

  /**
   * The hash algorithm to pass to hash(). Defaults to 'sha1'
   *
   * @var string
   */
  protected $hashAlgorithm;

  /**
   * Constructs a DrupalMemcacheBase object.
   *
   * @param string $bin
   *   The cache bin.
   * @param \Drupal\Core\Site\Settings
   *   The settings object.
   */
  public function __construct($bin, Settings $settings) {
    $this->bin = $bin;
    $this->settings = $settings;
    $this->hashAlgorithm = $this->settings
      ->get('memcache_key_hash_algorithm', 'sha1');
  }

  /**
   * {@inheritdoc}
   */
  public function get($key) {
    $full_key = $this
      ->key($key);
    $track_errors = ini_set('track_errors', '1');
    $php_errormsg = '';
    $result = @$this->memcache
      ->get($full_key);
    if (!empty($php_errormsg)) {
      register_shutdown_function('memcache_log_warning', LogLevel::WARNING, 'Exception caught in DrupalMemcacheBase::get: !msg', array(
        '!msg' => $php_errormsg,
      ));
      $php_errormsg = '';
    }
    ini_set('track_errors', $track_errors);
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function key($key) {
    $full_key = urlencode($this->bin . '-' . $key);

    // Memcache only supports key lengths up to 250 bytes.  If we have generated
    // a longer key, we shrink it to an acceptable length with a configurable
    // hashing algorithm. Sha1 was selected as the default as it performs
    // quickly with minimal collisions.
    if (strlen($full_key) > 250) {
      $full_key = urlencode(hash($this->hashAlgorithm, $this->bin . '-' . $key));
    }
    return $full_key;
  }

  /**
   * {@inheritdoc}
   */
  public function delete($key) {
    $full_key = $this
      ->key($key);
    return $this->memcache
      ->delete($full_key, 0);
  }

  /**
   * {@inheritdoc}
   */
  public function flush() {
    $this->memcache
      ->flush();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalMemcacheBase::$bin protected property The cache bin name.
DrupalMemcacheBase::$hashAlgorithm protected property The hash algorithm to pass to hash(). Defaults to 'sha1'
DrupalMemcacheBase::$memcache protected property The memcache object.
DrupalMemcacheBase::$settings protected property The settings object.
DrupalMemcacheBase::delete public function Deletes an item from Memcache. Overrides DrupalMemcacheInterface::delete
DrupalMemcacheBase::flush public function Immediately invalidates all existing items. Overrides DrupalMemcacheInterface::flush
DrupalMemcacheBase::get public function Retrieves a value from Memcache. Overrides DrupalMemcacheInterface::get
DrupalMemcacheBase::key public function Prepares the memcache key. Overrides DrupalMemcacheInterface::key
DrupalMemcacheBase::__construct public function Constructs a DrupalMemcacheBase object. 2
DrupalMemcacheInterface::addServer public function Adds a memcache server. 2
DrupalMemcacheInterface::getMulti public function Retrieves multiple values from Memcache. 2
DrupalMemcacheInterface::set public function Adds an item into memcache. 2