You are here

class DrupalMemcache in Memcache Storage 8

Class DrupalMemcache

  • Contains integration for Drupal & PECL Memcache extension.

@package Drupal\memcache_storage

Hierarchy

Expanded class hierarchy of DrupalMemcache

File

src/DrupalMemcache.php, line 14

Namespace

Drupal\memcache_storage
View source
class DrupalMemcache extends DrupalMemcachedBase {

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

  /**
   * Contains setting about pecl memcache compression.
   * @see http://php.net/manual/en/memcache.setcompressthreshold.php
   *
   * @var array
   */
  protected $compressThreshold = [
    'threshold' => 20000,
    'min_savings' => 0.2,
  ];

  /**
   * Indicates if compression should be enabled.
   * @see http://php.net/manual/en/memcache.set.php
   *
   * @var bool
   */
  protected $compressionEnabled;

  /**
   * Indicates if compression should be enabled.
   * @see http://php.net/manual/en/memcache.addserver.php
   *
   * @var bool
   */
  protected $persistentConnection;

  /**
   * @var \Memcache.
   */
  protected $memcached;

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

    // For more information see
    // http://www.php.net/manual/en/memcache.setcompressthreshold.php.
    if (!empty($settings['compress_threshold'])) {
      $this->compressThreshold = $settings['compress_threshold'];
    }
    if (isset($this->compressThreshold['threshold']) && isset($this->compressThreshold['min_savings'])) {
      $this->memcached
        ->setCompressThreshold($this->compressThreshold['threshold'], $this->compressThreshold['min_savings']);
    }

    // See http://php.net/manual/en/memcache.addserver.php
    $this->persistentConnection = !empty($settings['persistent_connection']);

    // See http://php.net/manual/en/memcache.set.php
    $this->compressionEnabled = !empty($settings['compression_enabled']);
  }

  /**
   * {@inheritdoc}
   */
  public function getStats() {

    // Supress errors, because if one of the memcached servers is not connected
    // it will throw php warning, but we handle this case ourselves.
    return @$this->memcached
      ->getExtendedStats();
  }

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

  /**
   * {@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;
    }

    // PECL memcache doesn't support multiple set, so just loop through
    // every cache item and set it.
    foreach ($items as $item) {

      // Get 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;
      }

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

      // Set the value to the memcached pool.
      $compression = !empty($this->compressionEnabled) ? MEMCACHE_COMPRESSED : 0;
      $result = $this->memcached
        ->set($memcached_key, $item, $compression, $expiration);

      // Logs the debug entry about the memcached operation.
      if (!empty($this->debug)) {
        $memcached_keys = [
          $memcached_key => $item->cid,
        ];
        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.
    $compression = !empty($this->compressionEnabled) ? MEMCACHE_COMPRESSED : 0;
    $result = $this->memcached
      ->get(array_keys($memcached_keys), $compression);

    // 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;
    }

    // PECL memcache doesn't support multiple deletion of elements, so
    // loop through all elements and delete one by one.
    foreach ($keys as $key) {
      $memcached_key = $this
        ->itemKey($key, $cache_bin);

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

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

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalMemcache::$compressionEnabled protected property Indicates if compression should be enabled.
DrupalMemcache::$compressThreshold protected property Contains setting about pecl memcache compression.
DrupalMemcache::$extension protected property Defines pecl extension to use. Overrides DrupalMemcachedBase::$extension
DrupalMemcache::$memcached protected property Overrides DrupalMemcachedBase::$memcached
DrupalMemcache::$persistentConnection protected property Indicates if compression should be enabled.
DrupalMemcache::addServer public function Adds a new memcached server. Overrides DrupalMemcachedInterface::addServer
DrupalMemcache::deleteMulti public function Bulk delete from the memcached pool. Overrides DrupalMemcachedInterface::deleteMulti
DrupalMemcache::getMulti public function Get the multiple cache items from the memcached pool. Overrides DrupalMemcachedInterface::getMulti
DrupalMemcache::getStats public function Returns info about connected memached servers. Overrides DrupalMemcachedInterface::getStats
DrupalMemcache::setMulti public function Bulk set if cache items to the memcached pool. Overrides DrupalMemcachedInterface::setMulti
DrupalMemcache::__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