3 namespace Doctrine\Common\Cache;
5 use Predis\ClientInterface;
8 * Predis cache provider.
10 * @author othillo <othillo@othillo.nl>
12 class PredisCache extends CacheProvider
15 * @var ClientInterface
20 * @param ClientInterface $client
24 public function __construct(ClientInterface $client)
26 $this->client = $client;
32 protected function doFetch($id)
34 $result = $this->client->get($id);
35 if (null === $result) {
39 return unserialize($result);
45 protected function doFetchMultiple(array $keys)
47 $fetchedItems = call_user_func_array(array($this->client, 'mget'), $keys);
49 return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems)));
55 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
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));
64 if ((string) $response != 'OK') {
72 // No lifetime, use MSET
73 $response = $this->client->mset(array_map(function ($value) {
74 return serialize($value);
77 return (string) $response == 'OK';
83 protected function doContains($id)
85 return (bool) $this->client->exists($id);
91 protected function doSave($id, $data, $lifeTime = 0)
93 $data = serialize($data);
95 $response = $this->client->setex($id, $lifeTime, $data);
97 $response = $this->client->set($id, $data);
100 return $response === true || $response == 'OK';
106 protected function doDelete($id)
108 return $this->client->del($id) >= 0;
114 protected function doFlush()
116 $response = $this->client->flushdb();
118 return $response === true || $response == 'OK';
124 protected function doGetStats()
126 $info = $this->client->info();
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