annotate vendor/doctrine/cache/lib/Doctrine/Common/Cache/RedisCache.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@0 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@0 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@0 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@0 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@0 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@0 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@0 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@0 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 14 *
Chris@0 15 * This software consists of voluntary contributions made by many individuals
Chris@0 16 * and is licensed under the MIT license. For more information, see
Chris@0 17 * <http://www.doctrine-project.org>.
Chris@0 18 */
Chris@0 19
Chris@0 20 namespace Doctrine\Common\Cache;
Chris@0 21
Chris@0 22 use Redis;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Redis cache provider.
Chris@0 26 *
Chris@0 27 * @link www.doctrine-project.org
Chris@0 28 * @since 2.2
Chris@0 29 * @author Osman Ungur <osmanungur@gmail.com>
Chris@0 30 */
Chris@0 31 class RedisCache extends CacheProvider
Chris@0 32 {
Chris@0 33 /**
Chris@0 34 * @var Redis|null
Chris@0 35 */
Chris@0 36 private $redis;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Sets the redis instance to use.
Chris@0 40 *
Chris@0 41 * @param Redis $redis
Chris@0 42 *
Chris@0 43 * @return void
Chris@0 44 */
Chris@0 45 public function setRedis(Redis $redis)
Chris@0 46 {
Chris@0 47 $redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue());
Chris@0 48 $this->redis = $redis;
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Gets the redis instance used by the cache.
Chris@0 53 *
Chris@0 54 * @return Redis|null
Chris@0 55 */
Chris@0 56 public function getRedis()
Chris@0 57 {
Chris@0 58 return $this->redis;
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * {@inheritdoc}
Chris@0 63 */
Chris@0 64 protected function doFetch($id)
Chris@0 65 {
Chris@0 66 return $this->redis->get($id);
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * {@inheritdoc}
Chris@0 71 */
Chris@0 72 protected function doFetchMultiple(array $keys)
Chris@0 73 {
Chris@0 74 $fetchedItems = array_combine($keys, $this->redis->mget($keys));
Chris@0 75
Chris@0 76 // 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 77 $foundItems = array();
Chris@0 78
Chris@0 79 foreach ($fetchedItems as $key => $value) {
Chris@0 80 if (false !== $value || $this->redis->exists($key)) {
Chris@0 81 $foundItems[$key] = $value;
Chris@0 82 }
Chris@0 83 }
Chris@0 84
Chris@0 85 return $foundItems;
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * {@inheritdoc}
Chris@0 90 */
Chris@0 91 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
Chris@0 92 {
Chris@0 93 if ($lifetime) {
Chris@0 94 $success = true;
Chris@0 95
Chris@0 96 // Keys have lifetime, use SETEX for each of them
Chris@0 97 foreach ($keysAndValues as $key => $value) {
Chris@0 98 if (!$this->redis->setex($key, $lifetime, $value)) {
Chris@0 99 $success = false;
Chris@0 100 }
Chris@0 101 }
Chris@0 102
Chris@0 103 return $success;
Chris@0 104 }
Chris@0 105
Chris@0 106 // No lifetime, use MSET
Chris@0 107 return (bool) $this->redis->mset($keysAndValues);
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * {@inheritdoc}
Chris@0 112 */
Chris@0 113 protected function doContains($id)
Chris@0 114 {
Chris@0 115 return $this->redis->exists($id);
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * {@inheritdoc}
Chris@0 120 */
Chris@0 121 protected function doSave($id, $data, $lifeTime = 0)
Chris@0 122 {
Chris@0 123 if ($lifeTime > 0) {
Chris@0 124 return $this->redis->setex($id, $lifeTime, $data);
Chris@0 125 }
Chris@0 126
Chris@0 127 return $this->redis->set($id, $data);
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * {@inheritdoc}
Chris@0 132 */
Chris@0 133 protected function doDelete($id)
Chris@0 134 {
Chris@0 135 return $this->redis->delete($id) >= 0;
Chris@0 136 }
Chris@0 137
Chris@0 138 /**
Chris@0 139 * {@inheritdoc}
Chris@0 140 */
Chris@0 141 protected function doFlush()
Chris@0 142 {
Chris@0 143 return $this->redis->flushDB();
Chris@0 144 }
Chris@0 145
Chris@0 146 /**
Chris@0 147 * {@inheritdoc}
Chris@0 148 */
Chris@0 149 protected function doGetStats()
Chris@0 150 {
Chris@0 151 $info = $this->redis->info();
Chris@0 152 return array(
Chris@0 153 Cache::STATS_HITS => $info['keyspace_hits'],
Chris@0 154 Cache::STATS_MISSES => $info['keyspace_misses'],
Chris@0 155 Cache::STATS_UPTIME => $info['uptime_in_seconds'],
Chris@0 156 Cache::STATS_MEMORY_USAGE => $info['used_memory'],
Chris@0 157 Cache::STATS_MEMORY_AVAILABLE => false
Chris@0 158 );
Chris@0 159 }
Chris@0 160
Chris@0 161 /**
Chris@0 162 * Returns the serializer constant to use. If Redis is compiled with
Chris@0 163 * igbinary support, that is used. Otherwise the default PHP serializer is
Chris@0 164 * used.
Chris@0 165 *
Chris@0 166 * @return integer One of the Redis::SERIALIZER_* constants
Chris@0 167 */
Chris@0 168 protected function getSerializerValue()
Chris@0 169 {
Chris@0 170 if (defined('HHVM_VERSION')) {
Chris@0 171 return Redis::SERIALIZER_PHP;
Chris@0 172 }
Chris@0 173
Chris@0 174 if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) {
Chris@0 175 return Redis::SERIALIZER_IGBINARY;
Chris@0 176 }
Chris@0 177
Chris@0 178 return Redis::SERIALIZER_PHP;
Chris@0 179 }
Chris@0 180 }