Chris@0: . Chris@0: */ Chris@0: Chris@0: namespace Doctrine\Common\Cache; Chris@0: Chris@0: use Redis; Chris@0: Chris@0: /** Chris@0: * Redis cache provider. Chris@0: * Chris@0: * @link www.doctrine-project.org Chris@0: * @since 2.2 Chris@0: * @author Osman Ungur Chris@0: */ Chris@0: class RedisCache extends CacheProvider Chris@0: { Chris@0: /** Chris@0: * @var Redis|null Chris@0: */ Chris@0: private $redis; Chris@0: Chris@0: /** Chris@0: * Sets the redis instance to use. Chris@0: * Chris@0: * @param Redis $redis Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function setRedis(Redis $redis) Chris@0: { Chris@0: $redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue()); Chris@0: $this->redis = $redis; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the redis instance used by the cache. Chris@0: * Chris@0: * @return Redis|null Chris@0: */ Chris@0: public function getRedis() Chris@0: { Chris@0: return $this->redis; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doFetch($id) Chris@0: { Chris@0: return $this->redis->get($id); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doFetchMultiple(array $keys) Chris@0: { Chris@0: $fetchedItems = array_combine($keys, $this->redis->mget($keys)); Chris@0: Chris@0: // Redis mget returns false for keys that do not exist. So we need to filter those out unless it's the real data. Chris@0: $foundItems = array(); Chris@0: Chris@0: foreach ($fetchedItems as $key => $value) { Chris@0: if (false !== $value || $this->redis->exists($key)) { Chris@0: $foundItems[$key] = $value; Chris@0: } Chris@0: } Chris@0: Chris@0: return $foundItems; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) Chris@0: { Chris@0: if ($lifetime) { Chris@0: $success = true; Chris@0: Chris@0: // Keys have lifetime, use SETEX for each of them Chris@0: foreach ($keysAndValues as $key => $value) { Chris@0: if (!$this->redis->setex($key, $lifetime, $value)) { Chris@0: $success = false; Chris@0: } Chris@0: } Chris@0: Chris@0: return $success; Chris@0: } Chris@0: Chris@0: // No lifetime, use MSET Chris@0: return (bool) $this->redis->mset($keysAndValues); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doContains($id) Chris@0: { Chris@0: return $this->redis->exists($id); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doSave($id, $data, $lifeTime = 0) Chris@0: { Chris@0: if ($lifeTime > 0) { Chris@0: return $this->redis->setex($id, $lifeTime, $data); Chris@0: } Chris@0: Chris@0: return $this->redis->set($id, $data); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doDelete($id) Chris@0: { Chris@0: return $this->redis->delete($id) >= 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doFlush() Chris@0: { Chris@0: return $this->redis->flushDB(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doGetStats() Chris@0: { Chris@0: $info = $this->redis->info(); Chris@0: return array( Chris@0: Cache::STATS_HITS => $info['keyspace_hits'], Chris@0: Cache::STATS_MISSES => $info['keyspace_misses'], Chris@0: Cache::STATS_UPTIME => $info['uptime_in_seconds'], Chris@0: Cache::STATS_MEMORY_USAGE => $info['used_memory'], Chris@0: Cache::STATS_MEMORY_AVAILABLE => false Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the serializer constant to use. If Redis is compiled with Chris@0: * igbinary support, that is used. Otherwise the default PHP serializer is Chris@0: * used. Chris@0: * Chris@0: * @return integer One of the Redis::SERIALIZER_* constants Chris@0: */ Chris@0: protected function getSerializerValue() Chris@0: { Chris@0: if (defined('HHVM_VERSION')) { Chris@0: return Redis::SERIALIZER_PHP; Chris@0: } Chris@0: Chris@0: if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) { Chris@0: return Redis::SERIALIZER_IGBINARY; Chris@0: } Chris@0: Chris@0: return Redis::SERIALIZER_PHP; Chris@0: } Chris@0: }