5 * Contains \Drupal\memcache\DrupalMemcache.
8 namespace Drupal\memcache;
13 * Class DrupalMemcache.
15 class DrupalMemcache extends DrupalMemcacheBase {
20 public function __construct(DrupalMemcacheConfig $settings) {
21 parent::__construct($settings);
23 $this->memcache = new \Memcache();
29 public function addServer($server_path, $persistent = FALSE) {
30 list($host, $port) = explode(':', $server_path);
32 // Support unix sockets in the format 'unix:///path/to/socket'.
33 if ($host == 'unix') {
34 // When using unix sockets with Memcache use the full path for $host.
36 // Port is always 0 for unix sockets.
40 // When using the PECL memcache extension, we must use ->(p)connect
41 // for the first connection.
42 return $this->connect($host, $port, $persistent);
48 public function close() {
49 $this->memcache->close();
53 * Connects to a memcache server.
57 * @param bool $persistent
61 protected function connect($host, $port, $persistent) {
63 return @$this->memcache->pconnect($host, $port);
66 return @$this->memcache->connect($host, $port);
73 public function set($key, $value, $exp = 0, $flag = FALSE) {
74 $full_key = $this->key($key);
75 return $this->memcache->set($full_key, $value, $flag, $exp);
81 public function getMulti(array $keys) {
84 foreach ($keys as $cid) {
85 $full_key = $this->key($cid);
86 $full_keys[$cid] = $full_key;
89 $results = $this->memcache->get($full_keys);
91 // If $results is FALSE, convert it to an empty array.
96 // Convert the full keys back to the cid.
97 $cid_results = array();
99 // Order isn't guaranteed, so ensure the return order matches that
100 // requested. So base the results on the order of the full_keys, as they
101 // reflect the order of the $cids passed in.
102 foreach (array_intersect($full_keys, array_keys($results)) as $cid => $full_key) {
103 $cid_results[$cid] = $results[$full_key];