You are here

class DrupalMemcached in Memcache Storage 8

Class DrupalMemcached

Contains integration for Drupal & PECL Memcached extension.

@package Drupal\memcache_storage

Hierarchy

Expanded class hierarchy of DrupalMemcached

File

src/DrupalMemcached.php, line 14

Namespace

Drupal\memcache_storage
View source
class DrupalMemcached extends DrupalMemcachedBase {

  /**
   * Defines pecl extension to use.
   *
   * @var string
   */
  protected $extension = 'Memcached';

  /**
   * Contains list of pecl memcached settings.
   *
   * @var array
   */
  protected $options;

  /**
   * Contains username and password for memcached SASL authentication.
   *
   * @var array
   */
  protected $saslAuth;

  /**
   * Default pecl memcached options.
   * Can be overriden in settings.
   *
   * @var array
   */
  protected $optionsDefault = [
    \Memcached::OPT_COMPRESSION => FALSE,
    \Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT,
  ];

  /**
   * Pecl memcached object.
   *
   * @var \Memcached.
   */
  protected $memcached;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $settings, $cluster_name) {
    parent::__construct($settings, $cluster_name);

    // For more info about memcached constants see
    // http://www.php.net/manual/en/memcached.constants.php.
    $this->options = !empty($settings['memcached_options']) ? $settings['memcached_options'] : [];
    $this->options += $this->optionsDefault;

    // Add SASL support.
    // See http://php.net/manual/en/memcached.setsaslauthdata.php
    if (!empty($this->settings['sasl_auth']['user']) && !empty($this->settings['sasl_auth']['password'])) {
      $this->memcached
        ->setSaslAuthData($this->settings['sasl_auth']['user'], $this->settings['sasl_auth']['password']);

      // SASL auth works only with binary protocol.
      $this->options[\Memcached::OPT_BINARY_PROTOCOL] = TRUE;
    }

    // Set pecl memcached options.
    // See http://php.net/manual/en/memcached.setoptions.php
    $this->memcached
      ->setOptions($this->options);
  }

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

  /**
   * {@inheritdoc}
   */
  public function addServer($host, $port) {
    $this->memcached
      ->addServer($host, $port);
  }

  /**
   * {@inheritdoc}
   */
  public function setMulti(array $items, $cache_bin = '') {

    // No point in performing any action is we're not connected to memcached.
    if (empty($this->isConnected)) {
      return;
    }

    // To perform a multiple set operation we have to group cache items
    // by the expiration time.
    // See http://php.net/manual/en/memcached.setmulti.php
    $item_groups = [];
    foreach ($items as $item) {

      // Get the formatted cache key.
      $memcached_key = $this
        ->itemKey($item->cid, $cache_bin);

      // Prepare the expiration time for memcached.
      $expiration = $item->expire;
      if ($item->expire == CacheBackendInterface::CACHE_PERMANENT) {
        $expiration = 0;
      }
      $item_groups[$expiration][$memcached_key] = $item;
    }

    // Multiple set of cache items.
    foreach ($item_groups as $expiration => $cache_items) {

      // Perform preparations for the debug logging.
      if (!empty($this->debug)) {
        DrupalMemcachedDebug::prepare();

        // Prepare the matching between memcached keys and drupal cache ids.
        $memcached_keys = [];
        foreach ($cache_items as $cache_key => $item) {
          $memcached_keys[$cache_key] = $item->cid;
        }
      }
      $result = $this->memcached
        ->setMulti($cache_items, $expiration);

      // Logs the debug entry about the memcached operation.
      if (!empty($this->debug)) {
        DrupalMemcachedDebug::process('set', $result, $memcached_keys, $cache_bin, $this->cluster);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getMulti(array $keys, $cache_bin = '') {

    // No point in performing any action is we're not connected to memcached.
    if (empty($this->isConnected)) {
      return [];
    }

    // Format every cache key before the request to memcached pool.
    $memcached_keys = [];
    foreach ($keys as $key) {
      $memcached_key = $this
        ->itemKey($key, $cache_bin);
      $memcached_keys[$memcached_key] = $key;
    }

    // Perform preparations for the debug logging.
    if (!empty($this->debug)) {
      DrupalMemcachedDebug::prepare();
    }

    // Get all cache items from memcached.
    $result = $this->memcached
      ->getMulti(array_keys($memcached_keys));

    // Logs the debug entry about the memcached operation.
    if (!empty($this->debug)) {
      DrupalMemcachedDebug::process('get', $result, $memcached_keys, $cache_bin, $this->cluster);
    }

    // Replace formatted memcached keys by Drupal keys.
    $cache = [];
    foreach ($result as $memcached_key => $value) {
      $normal_key = $memcached_keys[$memcached_key];
      $cache[$normal_key] = $value;
    }
    return $cache;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteMulti(array $keys, $cache_bin = '') {

    // No point in performing any action is we're not connected to memcached.
    if (empty($this->isConnected)) {
      return;
    }

    // Format every cache key before the request to memcached pool.
    $memcached_keys = [];
    foreach ($keys as $key) {
      $memcached_key = $this
        ->itemKey($key, $cache_bin);
      $memcached_keys[$memcached_key] = $key;
    }

    // Perform preparations for the debug logging.
    if (!empty($this->debug)) {
      DrupalMemcachedDebug::prepare();
    }

    // Make a request to delete all cache items.
    $result = $this->memcached
      ->deleteMulti(array_keys($memcached_keys));

    // Logs the debug entry about the memcached operation.
    if (!empty($this->debug)) {
      DrupalMemcachedDebug::process('delete', $result, $memcached_keys, $cache_bin, $this->cluster);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalMemcached::$extension protected property Defines pecl extension to use. Overrides DrupalMemcachedBase::$extension
DrupalMemcached::$memcached protected property Pecl memcached object. Overrides DrupalMemcachedBase::$memcached
DrupalMemcached::$options protected property Contains list of pecl memcached settings.
DrupalMemcached::$optionsDefault protected property Default pecl memcached options. Can be overriden in settings.
DrupalMemcached::$saslAuth protected property Contains username and password for memcached SASL authentication.
DrupalMemcached::addServer public function Adds a new memcached server. Overrides DrupalMemcachedInterface::addServer
DrupalMemcached::deleteMulti public function Bulk delete from the memcached pool. Overrides DrupalMemcachedInterface::deleteMulti
DrupalMemcached::getMulti public function Get the multiple cache items from the memcached pool. Overrides DrupalMemcachedInterface::getMulti
DrupalMemcached::getStats public function Returns info about connected memached servers. Overrides DrupalMemcachedInterface::getStats
DrupalMemcached::setMulti public function Bulk set if cache items to the memcached pool. Overrides DrupalMemcachedInterface::setMulti
DrupalMemcached::__construct public function Builds a new DrupalMemcache(d) object. Overrides DrupalMemcachedBase::__construct
DrupalMemcachedBase::$bin_indexes private property Internal variable. Contains special int indexes for each cache bin. The bin index included into building of memcached key. See itemKey() method for more info.
DrupalMemcachedBase::$cluster protected property The name of the current memcached cluster.
DrupalMemcachedBase::$debug protected property Status of the debug mode.
DrupalMemcachedBase::$hashAlgorithm protected property The algorithm for hashing memcached keys longer than 250 chars. By default sha1 is chosen because it performs quickly with minimal collisions.
DrupalMemcachedBase::$isConnected protected property Indicates the connection to memcached servers.
DrupalMemcachedBase::$keyPrefix protected property Prefix for each memcached key.
DrupalMemcachedBase::$servers protected property List of memcached servers referenced to the current cluster.
DrupalMemcachedBase::$serversDefault protected property Default list of memcached servers and its cluster name.
DrupalMemcachedBase::$settings protected property Array of Settings:get('memcache_storage') settings.
DrupalMemcachedBase::addServers public function Overrides DrupalMemcachedInterface::addServers
DrupalMemcachedBase::delete public function Delete the cache item from memcached. Overrides DrupalMemcachedInterface::delete
DrupalMemcachedBase::flush public function Reset the cache for the entire cache bin. Overrides DrupalMemcachedInterface::flush
DrupalMemcachedBase::get public function Get the cache item from memcached. Overrides DrupalMemcachedInterface::get
DrupalMemcachedBase::getBinIndex final protected function Returns cache bin index. This index is part of memcache key and changes if cache bin should be cleared.
DrupalMemcachedBase::increaseBinIndex final protected function Increase cache bin index. This operation changes all memcache keys in the specified cache bin, so we fake the cache flush operation.
DrupalMemcachedBase::itemKey final public function Return the formatted cache key as it will be stored in memcached.
DrupalMemcachedBase::set public function Set the cache to the memcached. Overrides DrupalMemcachedInterface::set