Security update to Drupal 8.4.6
[yaffs-website] / vendor / doctrine / cache / lib / Doctrine / Common / Cache / PredisCache.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\Common\Cache;
21
22 use Predis\ClientInterface;
23
24 /**
25  * Predis cache provider.
26  *
27  * @author othillo <othillo@othillo.nl>
28  */
29 class PredisCache extends CacheProvider
30 {
31     /**
32      * @var ClientInterface
33      */
34     private $client;
35
36     /**
37      * @param ClientInterface $client
38      *
39      * @return void
40      */
41     public function __construct(ClientInterface $client)
42     {
43         $this->client = $client;
44     }
45
46     /**
47      * {@inheritdoc}
48      */
49     protected function doFetch($id)
50     {
51         $result = $this->client->get($id);
52         if (null === $result) {
53             return false;
54         }
55
56         return unserialize($result);
57     }
58
59     /**
60      * {@inheritdoc}
61      */
62     protected function doFetchMultiple(array $keys)
63     {
64         $fetchedItems = call_user_func_array([$this->client, 'mget'], $keys);
65
66         return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems)));
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
73     {
74         if ($lifetime) {
75             $success = true;
76
77             // Keys have lifetime, use SETEX for each of them
78             foreach ($keysAndValues as $key => $value) {
79                 $response = $this->client->setex($key, $lifetime, serialize($value));
80
81                 if ((string) $response != 'OK') {
82                     $success = false;
83                 }
84             }
85
86             return $success;
87         }
88
89         // No lifetime, use MSET
90         $response = $this->client->mset(array_map(function ($value) {
91             return serialize($value);
92         }, $keysAndValues));
93
94         return (string) $response == 'OK';
95     }
96
97     /**
98      * {@inheritdoc}
99      */
100     protected function doContains($id)
101     {
102         return (bool) $this->client->exists($id);
103     }
104
105     /**
106      * {@inheritdoc}
107      */
108     protected function doSave($id, $data, $lifeTime = 0)
109     {
110         $data = serialize($data);
111         if ($lifeTime > 0) {
112             $response = $this->client->setex($id, $lifeTime, $data);
113         } else {
114             $response = $this->client->set($id, $data);
115         }
116
117         return $response === true || $response == 'OK';
118     }
119
120     /**
121      * {@inheritdoc}
122      */
123     protected function doDelete($id)
124     {
125         return $this->client->del($id) >= 0;
126     }
127
128     /**
129      * {@inheritdoc}
130      */
131     protected function doDeleteMultiple(array $keys)
132     {
133         return $this->client->del($keys) >= 0;
134     }
135
136     /**
137      * {@inheritdoc}
138      */
139     protected function doFlush()
140     {
141         $response = $this->client->flushdb();
142
143         return $response === true || $response == 'OK';
144     }
145
146     /**
147      * {@inheritdoc}
148      */
149     protected function doGetStats()
150     {
151         $info = $this->client->info();
152
153         return [
154             Cache::STATS_HITS              => $info['Stats']['keyspace_hits'],
155             Cache::STATS_MISSES            => $info['Stats']['keyspace_misses'],
156             Cache::STATS_UPTIME            => $info['Server']['uptime_in_seconds'],
157             Cache::STATS_MEMORY_USAGE      => $info['Memory']['used_memory'],
158             Cache::STATS_MEMORY_AVAILABLE  => false
159         ];
160     }
161 }