You are here

class MemcachedDriver in Memcache API and Integration 8.2

Class MemcachedDriver.

Hierarchy

Expanded class hierarchy of MemcachedDriver

File

src/Driver/MemcachedDriver.php, line 8

Namespace

Drupal\memcache\Driver
View source
class MemcachedDriver extends DriverBase {

  /**
   * {@inheritdoc}
   */
  public function set($key, $value, $exp = 0, $flag = FALSE) {
    $collect_stats = $this
      ->statsInit();
    $full_key = $this
      ->key($key);
    $result = $this->memcache
      ->set($full_key, $value, $exp);

    // Something bad happened. Let's log the problem.
    if (!$result && $this->settings
      ->get('debug')) {
      $result_code = $this->memcache
        ->getResultCode();
      $result_message = $this->memcache
        ->getResultMessage();
      $this
        ->getLogger('memcache')
        ->error('MemcachedDriver::set() error key=@key error=[@error_code]@error_msg', [
        '@key' => $full_key,
        '@error_code' => $result_code,
        '@error_msg' => $result_message,
      ]);
    }
    if ($collect_stats) {
      $this
        ->statsWrite('set', 'cache', [
        $full_key => (int) $result,
      ]);
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function add($key, $value, $expire = 0) {
    $collect_stats = $this
      ->statsInit();
    $full_key = $this
      ->key($key);
    $result = $this->memcache
      ->add($full_key, $value, $expire);
    if ($collect_stats) {
      $this
        ->statsWrite('add', 'cache', [
        $full_key => (int) $result,
      ]);
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getMulti(array $keys) {
    $collect_stats = $this
      ->statsInit();
    $multi_stats = [];
    $full_keys = [];
    foreach ($keys as $key => $cid) {
      $full_key = $this
        ->key($cid);
      $full_keys[$cid] = $full_key;
      if ($collect_stats) {
        $multi_stats[$full_key] = FALSE;
      }
    }
    if (PHP_MAJOR_VERSION >= 7) {
      $results = $this->memcache
        ->getMulti($full_keys, \Memcached::GET_PRESERVE_ORDER);
    }
    else {
      $cas_tokens = NULL;
      $results = $this->memcache
        ->getMulti($full_keys, $cas_tokens, \Memcached::GET_PRESERVE_ORDER);
    }

    // If $results is FALSE, convert it to an empty array.
    if (!$results) {
      $results = [];
    }
    if ($collect_stats) {
      foreach ($multi_stats as $key => $value) {
        $multi_stats[$key] = isset($results[$key]) ? TRUE : FALSE;
      }
    }

    // Convert the full keys back to the cid.
    $cid_results = [];
    $cid_lookup = array_flip($full_keys);
    foreach (array_filter($results) as $key => $value) {
      $cid_results[$cid_lookup[$key]] = $value;
    }
    if ($collect_stats) {
      $this
        ->statsWrite('getMulti', 'cache', $multi_stats);
    }
    return $cid_results;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DriverBase::$hashAlgorithm protected property The hash algorithm to pass to hash(). Defaults to 'sha1'.
DriverBase::$memcache protected property The memcache object.
DriverBase::$prefix protected property The prefix memcache key for all keys.
DriverBase::$settings protected property The memcache config object.
DriverBase::$stats protected static property Stats for the entire request.
DriverBase::delete public function Deletes an item from Memcache. Overrides DrupalMemcacheInterface::delete
DriverBase::flush public function Immediately invalidates all existing items. Overrides DrupalMemcacheInterface::flush
DriverBase::get public function Retrieves a value from Memcache. Overrides DrupalMemcacheInterface::get
DriverBase::getBins public function Helper function to get the bins.
DriverBase::getMemcache public function Helper function to get memcache.
DriverBase::getServers public function Helper function to get the servers.
DriverBase::key public function Prepares the memcache key. Overrides DrupalMemcacheInterface::key
DriverBase::requestStats public function Helper function to get request stats.
DriverBase::stats public function Retrieves statistics recorded during memcache operations.
DriverBase::statsInit protected function Helper function to initialize the stats for a memcache operation.
DriverBase::statsTypes public function Returns an array of available statistics types.
DriverBase::statsWrite protected function Memcache statistics to be displayed at end of page generation.
DriverBase::__construct public function Constructs a DriverBase object.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MemcachedDriver::add public function Add an item to Memcache if it doesn't exist already. Overrides DrupalMemcacheInterface::add
MemcachedDriver::getMulti public function Retrieves multiple values from Memcache. Overrides DrupalMemcacheInterface::getMulti
MemcachedDriver::set public function Adds an item into memcache. Overrides DrupalMemcacheInterface::set