class DrupalMemcached in Memcache Storage 8
Class DrupalMemcached
Contains integration for Drupal & PECL Memcached extension.
@package Drupal\memcache_storage
Hierarchy
- class \Drupal\memcache_storage\DrupalMemcachedBase implements DrupalMemcachedInterface
- class \Drupal\memcache_storage\DrupalMemcached
Expanded class hierarchy of DrupalMemcached
File
- src/
DrupalMemcached.php, line 14
Namespace
Drupal\memcache_storageView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DrupalMemcached:: |
protected | property |
Defines pecl extension to use. Overrides DrupalMemcachedBase:: |
|
DrupalMemcached:: |
protected | property |
Pecl memcached object. Overrides DrupalMemcachedBase:: |
|
DrupalMemcached:: |
protected | property | Contains list of pecl memcached settings. | |
DrupalMemcached:: |
protected | property | Default pecl memcached options. Can be overriden in settings. | |
DrupalMemcached:: |
protected | property | Contains username and password for memcached SASL authentication. | |
DrupalMemcached:: |
public | function |
Adds a new memcached server. Overrides DrupalMemcachedInterface:: |
|
DrupalMemcached:: |
public | function |
Bulk delete from the memcached pool. Overrides DrupalMemcachedInterface:: |
|
DrupalMemcached:: |
public | function |
Get the multiple cache items from the memcached pool. Overrides DrupalMemcachedInterface:: |
|
DrupalMemcached:: |
public | function |
Returns info about connected memached servers. Overrides DrupalMemcachedInterface:: |
|
DrupalMemcached:: |
public | function |
Bulk set if cache items to the memcached pool. Overrides DrupalMemcachedInterface:: |
|
DrupalMemcached:: |
public | function |
Builds a new DrupalMemcache(d) object. Overrides DrupalMemcachedBase:: |
|
DrupalMemcachedBase:: |
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:: |
protected | property | The name of the current memcached cluster. | |
DrupalMemcachedBase:: |
protected | property | Status of the debug mode. | |
DrupalMemcachedBase:: |
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:: |
protected | property | Indicates the connection to memcached servers. | |
DrupalMemcachedBase:: |
protected | property | Prefix for each memcached key. | |
DrupalMemcachedBase:: |
protected | property | List of memcached servers referenced to the current cluster. | |
DrupalMemcachedBase:: |
protected | property | Default list of memcached servers and its cluster name. | |
DrupalMemcachedBase:: |
protected | property | Array of Settings:get('memcache_storage') settings. | |
DrupalMemcachedBase:: |
public | function |
Overrides DrupalMemcachedInterface:: |
|
DrupalMemcachedBase:: |
public | function |
Delete the cache item from memcached. Overrides DrupalMemcachedInterface:: |
|
DrupalMemcachedBase:: |
public | function |
Reset the cache for the entire cache bin. Overrides DrupalMemcachedInterface:: |
|
DrupalMemcachedBase:: |
public | function |
Get the cache item from memcached. Overrides DrupalMemcachedInterface:: |
|
DrupalMemcachedBase:: |
final protected | function | Returns cache bin index. This index is part of memcache key and changes if cache bin should be cleared. | |
DrupalMemcachedBase:: |
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:: |
final public | function | Return the formatted cache key as it will be stored in memcached. | |
DrupalMemcachedBase:: |
public | function |
Set the cache to the memcached. Overrides DrupalMemcachedInterface:: |