View source
<?php
namespace Drupal\memcache_storage;
use Drupal\Core\Cache\CacheBackendInterface;
class DrupalMemcached extends DrupalMemcachedBase {
protected $extension = 'Memcached';
protected $options;
protected $saslAuth;
protected $optionsDefault = [
\Memcached::OPT_COMPRESSION => FALSE,
\Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT,
];
protected $memcached;
public function __construct(array $settings, $cluster_name) {
parent::__construct($settings, $cluster_name);
$this->options = !empty($settings['memcached_options']) ? $settings['memcached_options'] : [];
$this->options += $this->optionsDefault;
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']);
$this->options[\Memcached::OPT_BINARY_PROTOCOL] = TRUE;
}
$this->memcached
->setOptions($this->options);
}
public function getStats() {
return $this->memcached
->getStats();
}
public function addServer($host, $port) {
$this->memcached
->addServer($host, $port);
}
public function setMulti(array $items, $cache_bin = '') {
if (empty($this->isConnected)) {
return;
}
$item_groups = [];
foreach ($items as $item) {
$memcached_key = $this
->itemKey($item->cid, $cache_bin);
$expiration = $item->expire;
if ($item->expire == CacheBackendInterface::CACHE_PERMANENT) {
$expiration = 0;
}
$item_groups[$expiration][$memcached_key] = $item;
}
foreach ($item_groups as $expiration => $cache_items) {
if (!empty($this->debug)) {
DrupalMemcachedDebug::prepare();
$memcached_keys = [];
foreach ($cache_items as $cache_key => $item) {
$memcached_keys[$cache_key] = $item->cid;
}
}
$result = $this->memcached
->setMulti($cache_items, $expiration);
if (!empty($this->debug)) {
DrupalMemcachedDebug::process('set', $result, $memcached_keys, $cache_bin, $this->cluster);
}
}
}
public function getMulti(array $keys, $cache_bin = '') {
if (empty($this->isConnected)) {
return [];
}
$memcached_keys = [];
foreach ($keys as $key) {
$memcached_key = $this
->itemKey($key, $cache_bin);
$memcached_keys[$memcached_key] = $key;
}
if (!empty($this->debug)) {
DrupalMemcachedDebug::prepare();
}
$result = $this->memcached
->getMulti(array_keys($memcached_keys));
if (!empty($this->debug)) {
DrupalMemcachedDebug::process('get', $result, $memcached_keys, $cache_bin, $this->cluster);
}
$cache = [];
foreach ($result as $memcached_key => $value) {
$normal_key = $memcached_keys[$memcached_key];
$cache[$normal_key] = $value;
}
return $cache;
}
public function deleteMulti(array $keys, $cache_bin = '') {
if (empty($this->isConnected)) {
return;
}
$memcached_keys = [];
foreach ($keys as $key) {
$memcached_key = $this
->itemKey($key, $cache_bin);
$memcached_keys[$memcached_key] = $key;
}
if (!empty($this->debug)) {
DrupalMemcachedDebug::prepare();
}
$result = $this->memcached
->deleteMulti(array_keys($memcached_keys));
if (!empty($this->debug)) {
DrupalMemcachedDebug::process('delete', $result, $memcached_keys, $cache_bin, $this->cluster);
}
}
}