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 $track_errors = ini_set('track_errors', 1);
91 $results = @$this->memcache->get($full_keys);
93 if (!empty($php_errormsg)) {
94 register_shutdown_function('memcache_log_warning', LogLevel::WARNING, 'Exception caught in DrupalMemcache::getMulti: !msg', array('!msg' => $php_errormsg));
98 ini_set('track_errors', $track_errors);
100 // If $results is FALSE, convert it to an empty array.
105 // Convert the full keys back to the cid.
106 $cid_results = array();
108 // Order isn't guaranteed, so ensure the return order matches that
109 // requested. So base the results on the order of the full_keys, as they
110 // reflect the order of the $cids passed in.
111 foreach (array_intersect($full_keys, array_keys($results)) as $cid => $full_key) {
112 $cid_results[$cid] = $results[$full_key];