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.
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>.
20 namespace Doctrine\Common\Cache;
22 use Predis\ClientInterface;
25 * Predis cache provider.
27 * @author othillo <othillo@othillo.nl>
29 class PredisCache extends CacheProvider
32 * @var ClientInterface
37 * @param ClientInterface $client
41 public function __construct(ClientInterface $client)
43 $this->client = $client;
49 protected function doFetch($id)
51 $result = $this->client->get($id);
52 if (null === $result) {
56 return unserialize($result);
62 protected function doFetchMultiple(array $keys)
64 $fetchedItems = call_user_func_array([$this->client, 'mget'], $keys);
66 return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems)));
72 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
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));
81 if ((string) $response != 'OK') {
89 // No lifetime, use MSET
90 $response = $this->client->mset(array_map(function ($value) {
91 return serialize($value);
94 return (string) $response == 'OK';
100 protected function doContains($id)
102 return (bool) $this->client->exists($id);
108 protected function doSave($id, $data, $lifeTime = 0)
110 $data = serialize($data);
112 $response = $this->client->setex($id, $lifeTime, $data);
114 $response = $this->client->set($id, $data);
117 return $response === true || $response == 'OK';
123 protected function doDelete($id)
125 return $this->client->del($id) >= 0;
131 protected function doDeleteMultiple(array $keys)
133 return $this->client->del($keys) >= 0;
139 protected function doFlush()
141 $response = $this->client->flushdb();
143 return $response === true || $response == 'OK';
149 protected function doGetStats()
151 $info = $this->client->info();
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