Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / memcache / src / Driver / MemcacheDriver.php
1 <?php
2
3 namespace Drupal\memcache\Driver;
4
5 /**
6  * Class MemcacheDriver.
7  */
8 class MemcacheDriver 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, $flag, $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, FALSE, $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     $results = $this->memcache->get($full_keys);
61
62     // If $results is FALSE, convert it to an empty array.
63     if (!$results) {
64       $results = [];
65     }
66
67     if ($collect_stats) {
68       foreach ($multi_stats as $key => $value) {
69         $multi_stats[$key] = isset($results[$key]) ? TRUE : FALSE;
70       }
71     }
72
73     // Convert the full keys back to the cid.
74     $cid_results = [];
75
76     // Order isn't guaranteed, so ensure the return order matches that
77     // requested. So base the results on the order of the full_keys, as they
78     // reflect the order of the $cids passed in.
79     foreach (array_intersect($full_keys, array_keys($results)) as $cid => $full_key) {
80       $cid_results[$cid] = $results[$full_key];
81     }
82
83     if ($collect_stats) {
84       $this->statsWrite('getMulti', 'cache', $multi_stats);
85     }
86
87     return $cid_results;
88   }
89
90 }