5 * Contains \Drupal\memcache\DrupalMemcached.
8 namespace Drupal\memcache;
11 * Class DrupalMemcached.
13 class DrupalMemcached extends DrupalMemcacheBase {
18 public function __construct(DrupalMemcacheConfig $settings) {
19 parent::__construct($settings);
21 $this->memcache = new \Memcached();
23 $default_opts = array(
24 \Memcached::OPT_COMPRESSION => FALSE,
25 \Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT,
27 foreach ($default_opts as $key => $value) {
28 $this->memcache->setOption($key, $value);
30 // See README.txt for setting custom Memcache options when using the
31 // memcached PECL extension.
32 foreach ($this->settings->get('options', []) as $key => $value) {
33 $this->memcache->setOption($key, $value);
36 // SASL configuration to authenticate with Memcached.
37 // Note: this only affects the Memcached PECL extension.
38 if ($sasl_config = $this->settings->get('sasl', [])) {
39 $this->memcache->setSaslAuthData($sasl_config['username'], $sasl_config['password']);
46 public function addServer($server_path, $persistent = FALSE) {
47 list($host, $port) = explode(':', $server_path);
49 if ($host == 'unix') {
50 // Memcached expects just the path to the socket without the protocol
51 $host = substr($server_path, 7);
52 // Port is always 0 for unix sockets.
56 return $this->memcache->addServer($host, $port, $persistent);
62 public function close() {
63 $this->memcache->quit();
69 public function set($key, $value, $exp = 0, $flag = FALSE) {
70 $full_key = $this->key($key);
71 return $this->memcache->set($full_key, $value, $exp);
77 public function getMulti(array $keys) {
80 foreach ($keys as $cid) {
81 $full_key = $this->key($cid);
82 $full_keys[$cid] = $full_key;
85 if (PHP_MAJOR_VERSION === 7) {
86 $results = $this->memcache->getMulti($full_keys, \Memcached::GET_PRESERVE_ORDER);
89 $results = $this->memcache->getMulti($full_keys, $cas_tokens, \Memcached::GET_PRESERVE_ORDER);
92 // If $results is FALSE, convert it to an empty array.
97 // Convert the full keys back to the cid.
98 $cid_results = array();
99 $cid_lookup = array_flip($full_keys);
101 foreach (array_filter($results) as $key => $value) {
102 $cid_results[$cid_lookup[$key]] = $value;