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);
40 public function addServer($server_path, $persistent = FALSE) {
41 list($host, $port) = explode(':', $server_path);
43 if ($host == 'unix') {
44 // Memcached expects just the path to the socket without the protocol
45 $host = substr($host, 7);
46 // Port is always 0 for unix sockets.
50 return $this->memcache->addServer($host, $port, $persistent);
56 public function close() {
57 $this->memcache->quit();
63 public function set($key, $value, $exp = 0, $flag = FALSE) {
64 $full_key = $this->key($key);
65 return $this->memcache->set($full_key, $value, $exp);
71 public function getMulti(array $keys) {
74 foreach ($keys as $cid) {
75 $full_key = $this->key($cid);
76 $full_keys[$cid] = $full_key;
80 $results = $this->memcache->getMulti($full_keys, $cas_tokens, \Memcached::GET_PRESERVE_ORDER);
82 // If $results is FALSE, convert it to an empty array.
87 // Convert the full keys back to the cid.
88 $cid_results = array();
89 $cid_lookup = array_flip($full_keys);
91 foreach (array_filter($results) as $key => $value) {
92 $cid_results[$cid_lookup[$key]] = $value;