bcd27497e980e4abc3dea741aa752701289483d0
[yaffs-website] / vendor / doctrine / cache / lib / Doctrine / Common / Cache / PredisCache.php
1 <?php
2
3 namespace Doctrine\Common\Cache;
4
5 use Predis\ClientInterface;
6
7 /**
8  * Predis cache provider.
9  *
10  * @author othillo <othillo@othillo.nl>
11  */
12 class PredisCache extends CacheProvider
13 {
14     /**
15      * @var ClientInterface
16      */
17     private $client;
18
19     /**
20      * @param ClientInterface $client
21      *
22      * @return void
23      */
24     public function __construct(ClientInterface $client)
25     {
26         $this->client = $client;
27     }
28
29     /**
30      * {@inheritdoc}
31      */
32     protected function doFetch($id)
33     {
34         $result = $this->client->get($id);
35         if (null === $result) {
36             return false;
37         }
38
39         return unserialize($result);
40     }
41
42     /**
43      * {@inheritdoc}
44      */
45     protected function doFetchMultiple(array $keys)
46     {
47         $fetchedItems = call_user_func_array(array($this->client, 'mget'), $keys);
48
49         return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems)));
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
56     {
57         if ($lifetime) {
58             $success = true;
59
60             // Keys have lifetime, use SETEX for each of them
61             foreach ($keysAndValues as $key => $value) {
62                 $response = $this->client->setex($key, $lifetime, serialize($value));
63
64                 if ((string) $response != 'OK') {
65                     $success = false;
66                 }
67             }
68
69             return $success;
70         }
71
72         // No lifetime, use MSET
73         $response = $this->client->mset(array_map(function ($value) {
74             return serialize($value);
75         }, $keysAndValues));
76
77         return (string) $response == 'OK';
78     }
79
80     /**
81      * {@inheritdoc}
82      */
83     protected function doContains($id)
84     {
85         return (bool) $this->client->exists($id);
86     }
87
88     /**
89      * {@inheritdoc}
90      */
91     protected function doSave($id, $data, $lifeTime = 0)
92     {
93         $data = serialize($data);
94         if ($lifeTime > 0) {
95             $response = $this->client->setex($id, $lifeTime, $data);
96         } else {
97             $response = $this->client->set($id, $data);
98         }
99
100         return $response === true || $response == 'OK';
101     }
102
103     /**
104      * {@inheritdoc}
105      */
106     protected function doDelete($id)
107     {
108         return $this->client->del($id) >= 0;
109     }
110
111     /**
112      * {@inheritdoc}
113      */
114     protected function doFlush()
115     {
116         $response = $this->client->flushdb();
117
118         return $response === true || $response == 'OK';
119     }
120
121     /**
122      * {@inheritdoc}
123      */
124     protected function doGetStats()
125     {
126         $info = $this->client->info();
127
128         return array(
129             Cache::STATS_HITS              => $info['Stats']['keyspace_hits'],
130             Cache::STATS_MISSES            => $info['Stats']['keyspace_misses'],
131             Cache::STATS_UPTIME            => $info['Server']['uptime_in_seconds'],
132             Cache::STATS_MEMORY_USAGE      => $info['Memory']['used_memory'],
133             Cache::STATS_MEMORY_AVAILABLE  => false
134         );
135     }
136 }