View source
<?php
namespace Drupal\memcache_storage;
use Drupal\Core\Cache\CacheBackendInterface;
class DrupalMemcache extends DrupalMemcachedBase {
protected $extension = 'Memcache';
protected $compressThreshold = [
'threshold' => 20000,
'min_savings' => 0.2,
];
protected $compressionEnabled;
protected $persistentConnection;
protected $memcached;
public function __construct(array $settings, $cluster_name) {
parent::__construct($settings, $cluster_name);
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']);
}
$this->persistentConnection = !empty($settings['persistent_connection']);
$this->compressionEnabled = !empty($settings['compression_enabled']);
}
public function getStats() {
return @$this->memcached
->getExtendedStats();
}
public function addServer($host, $port) {
$this->memcached
->addserver($host, $port, $this->persistentConnection);
}
public function setMulti(array $items, $cache_bin = '') {
if (empty($this->isConnected)) {
return;
}
foreach ($items as $item) {
$memcached_key = $this
->itemKey($item->cid, $cache_bin);
$expiration = $item->expire;
if ($item->expire == CacheBackendInterface::CACHE_PERMANENT) {
$expiration = 0;
}
if (!empty($this->debug)) {
DrupalMemcachedDebug::prepare();
}
$compression = !empty($this->compressionEnabled) ? MEMCACHE_COMPRESSED : 0;
$result = $this->memcached
->set($memcached_key, $item, $compression, $expiration);
if (!empty($this->debug)) {
$memcached_keys = [
$memcached_key => $item->cid,
];
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();
}
$compression = !empty($this->compressionEnabled) ? MEMCACHE_COMPRESSED : 0;
$result = $this->memcached
->get(array_keys($memcached_keys), $compression);
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;
}
foreach ($keys as $key) {
$memcached_key = $this
->itemKey($key, $cache_bin);
if (!empty($this->debug)) {
DrupalMemcachedDebug::prepare();
}
$result = $this->memcached
->delete($memcached_key);
if (!empty($this->debug)) {
$memcached_keys = [
$memcached_key => $key,
];
DrupalMemcachedDebug::process('delete', $result, $memcached_keys, $cache_bin, $this->cluster);
}
}
}
}