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;
25 * Redis cache provider.
27 * @link www.doctrine-project.org
29 * @author Osman Ungur <osmanungur@gmail.com>
31 class RedisCache extends CacheProvider
39 * Sets the redis instance to use.
45 public function setRedis(Redis $redis)
47 $redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue());
48 $this->redis = $redis;
52 * Gets the redis instance used by the cache.
56 public function getRedis()
64 protected function doFetch($id)
66 return $this->redis->get($id);
72 protected function doFetchMultiple(array $keys)
74 $fetchedItems = array_combine($keys, $this->redis->mget($keys));
76 // Redis mget returns false for keys that do not exist. So we need to filter those out unless it's the real data.
77 $foundItems = array();
79 foreach ($fetchedItems as $key => $value) {
80 if (false !== $value || $this->redis->exists($key)) {
81 $foundItems[$key] = $value;
91 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
96 // Keys have lifetime, use SETEX for each of them
97 foreach ($keysAndValues as $key => $value) {
98 if (!$this->redis->setex($key, $lifetime, $value)) {
106 // No lifetime, use MSET
107 return (bool) $this->redis->mset($keysAndValues);
113 protected function doContains($id)
115 return $this->redis->exists($id);
121 protected function doSave($id, $data, $lifeTime = 0)
124 return $this->redis->setex($id, $lifeTime, $data);
127 return $this->redis->set($id, $data);
133 protected function doDelete($id)
135 return $this->redis->delete($id) >= 0;
141 protected function doFlush()
143 return $this->redis->flushDB();
149 protected function doGetStats()
151 $info = $this->redis->info();
153 Cache::STATS_HITS => $info['keyspace_hits'],
154 Cache::STATS_MISSES => $info['keyspace_misses'],
155 Cache::STATS_UPTIME => $info['uptime_in_seconds'],
156 Cache::STATS_MEMORY_USAGE => $info['used_memory'],
157 Cache::STATS_MEMORY_AVAILABLE => false
162 * Returns the serializer constant to use. If Redis is compiled with
163 * igbinary support, that is used. Otherwise the default PHP serializer is
166 * @return integer One of the Redis::SERIALIZER_* constants
168 protected function getSerializerValue()
170 if (defined('HHVM_VERSION')) {
171 return Redis::SERIALIZER_PHP;
174 if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) {
175 return Redis::SERIALIZER_IGBINARY;
178 return Redis::SERIALIZER_PHP;