Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / memcache / src / Driver / MemcachedDriver.php
1 <?php
2
3 namespace Drupal\memcache\Driver;
4
5 /**
6  * Class MemcachedDriver.
7  */
8 class MemcachedDriver extends DriverBase {
9
10   /**
11    * {@inheritdoc}
12    */
13   public function set($key, $value, $exp = 0, $flag = FALSE) {
14     $collect_stats = $this->statsInit();
15
16     $full_key = $this->key($key);
17     $result = $this->memcache->set($full_key, $value, $exp);
18
19     if ($collect_stats) {
20       $this->statsWrite('set', 'cache', [$full_key => (int) $result]);
21     }
22
23     return $result;
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public function add($key, $value, $expire = 0) {
30     $collect_stats = $this->statsInit();
31
32     $full_key = $this->key($key);
33     $result = $this->memcache->add($full_key, $value, $expire);
34
35     if ($collect_stats) {
36       $this->statsWrite('add', 'cache', [$full_key => (int) $result]);
37     }
38
39     return $result;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getMulti(array $keys) {
46     $collect_stats = $this->statsInit();
47     $multi_stats   = [];
48
49     $full_keys = [];
50
51     foreach ($keys as $key => $cid) {
52       $full_key = $this->key($cid);
53       $full_keys[$cid] = $full_key;
54
55       if ($collect_stats) {
56         $multi_stats[$full_key] = FALSE;
57       }
58     }
59
60     if (PHP_MAJOR_VERSION === 7) {
61       $results = $this->memcache->getMulti($full_keys, \Memcached::GET_PRESERVE_ORDER);
62     }
63     else {
64       $cas_tokens = NULL;
65       $results = $this->memcache->getMulti($full_keys, $cas_tokens, \Memcached::GET_PRESERVE_ORDER);
66     }
67
68     // If $results is FALSE, convert it to an empty array.
69     if (!$results) {
70       $results = [];
71     }
72
73     if ($collect_stats) {
74       foreach ($multi_stats as $key => $value) {
75         $multi_stats[$key] = isset($results[$key]) ? TRUE : FALSE;
76       }
77     }
78
79     // Convert the full keys back to the cid.
80     $cid_results = [];
81     $cid_lookup = array_flip($full_keys);
82
83     foreach (array_filter($results) as $key => $value) {
84       $cid_results[$cid_lookup[$key]] = $value;
85     }
86
87     if ($collect_stats) {
88       $this->statsWrite('getMulti', 'cache', $multi_stats);
89     }
90
91     return $cid_results;
92   }
93
94 }