Cache.php 5.57 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\redis;

Qiang Xue committed
10 11 12
use Yii;
use yii\base\InvalidConfigException;

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/**
 * Redis Cache implements a cache application component based on [redis](http://redis.io/) key-value store.
 *
 * Redis Cache requires redis version 2.6.12 or higher to work properly.
 *
 * It needs to be configured with a redis [[Connection]] that is also configured as an application component.
 * By default it will use the `redis` application component.
 *
 * See [[Cache]] manual for common cache operations that redis Cache supports.
 *
 * Unlike the [[Cache]], redis Cache allows the expire parameter of [[set]], [[add]], [[mset]] and [[madd]] to
 * be a floating point number, so you may specify the time in milliseconds (e.g. 0.1 will be 100 milliseconds).
 *
 * To use redis Cache as the cache application component, configure the application as follows,
 *
 * ~~~
 * [
 *     'components' => [
 *         'cache' => [
 *             'class' => 'yii\redis\Cache',
33 34 35 36 37 38 39 40 41 42
 *             'redis' => [
 *                 'hostname' => 'localhost',
 *                 'port' => 6379,
 *                 'database' => 0,
 *             ]
 *         ],
 *     ],
 * ]
 * ~~~
 *
Carsten Brandt committed
43
 * Or if you have configured the redis [[Connection]] as an application component, the following is sufficient:
44 45 46 47 48 49 50
 *
 * ~~~
 * [
 *     'components' => [
 *         'cache' => [
 *             'class' => 'yii\redis\Cache',
 *             // 'redis' => 'redis' // id of the connection application component
51 52 53 54 55 56 57 58 59 60 61
 *         ],
 *     ],
 * ]
 * ~~~
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
class Cache extends \yii\caching\Cache
{
	/**
62 63 64
	 * @var Connection|string|array the Redis [[Connection]] object or the application component ID of the Redis [[Connection]].
	 * This can also be an array that is used to create a redis [[Connection]] instance in case you do not want do configure
	 * redis connection as an application component.
Qiang Xue committed
65 66
	 * After the Cache object is created, if you want to change this property, you should only assign it
	 * with a Redis [[Connection]] object.
67
	 */
Qiang Xue committed
68
	public $redis = 'redis';
69 70


Qiang Xue committed
71
	/**
72 73 74
	 * Initializes the redis Cache component.
	 * This method will initialize the [[redis]] property to make sure it refers to a valid redis connection.
	 * @throws InvalidConfigException if [[redis]] is invalid.
Qiang Xue committed
75 76 77 78 79 80
	 */
	public function init()
	{
		parent::init();
		if (is_string($this->redis)) {
			$this->redis = Yii::$app->getComponent($this->redis);
81 82 83 84 85
		} else if (is_array($this->redis)) {
			if (!isset($this->redis['class'])) {
				$this->redis['class'] = Connection::className();
			}
			$this->redis = Yii::createObject($this->redis);
Qiang Xue committed
86 87 88 89 90 91
		}
		if (!$this->redis instanceof Connection) {
			throw new InvalidConfigException("Cache::redis must be either a Redis connection instance or the application component ID of a Redis connection.");
		}
	}

92 93 94 95 96 97 98 99 100 101 102 103
	/**
	 * Checks whether a specified key exists in the cache.
	 * This can be faster than getting the value from the cache if the data is big.
	 * Note that this method does not check whether the dependency associated
	 * with the cached data, if there is any, has changed. So a call to [[get]]
	 * may return false while exists returns true.
	 * @param mixed $key a key identifying the cached value. This can be a simple string or
	 * a complex data structure consisting of factors representing the key.
	 * @return boolean true if a value exists in cache, false if the value is not in the cache or expired.
	 */
	public function exists($key)
	{
Qiang Xue committed
104
		return (bool) $this->redis->executeCommand('EXISTS', [$this->buildKey($key)]);
105 106 107
	}

	/**
Qiang Xue committed
108
	 * @inheritdoc
109 110 111
	 */
	protected function getValue($key)
	{
Qiang Xue committed
112
		return $this->redis->executeCommand('GET', [$key]);
113 114 115
	}

	/**
Qiang Xue committed
116
	 * @inheritdoc
117 118 119
	 */
	protected function getValues($keys)
	{
Qiang Xue committed
120
		$response = $this->redis->executeCommand('MGET', $keys);
121 122 123 124 125 126 127 128 129
		$result = [];
		$i = 0;
		foreach ($keys as $key) {
			$result[$key] = $response[$i++];
		}
		return $result;
	}

	/**
Qiang Xue committed
130
	 * @inheritdoc
131 132 133 134
	 */
	protected function setValue($key, $value, $expire)
	{
		if ($expire == 0) {
Qiang Xue committed
135
			return (bool) $this->redis->executeCommand('SET', [$key, $value]);
136 137
		} else {
			$expire = (int) ($expire * 1000);
Qiang Xue committed
138
			return (bool) $this->redis->executeCommand('SET', [$key, $value, 'PX', $expire]);
139 140 141 142
		}
	}

	/**
Qiang Xue committed
143
	 * @inheritdoc
144 145 146 147 148 149 150 151 152 153 154
	 */
	protected function setValues($data, $expire)
	{
		$args = [];
		foreach($data as $key => $value) {
			$args[] = $key;
			$args[] = $value;
		}

		$failedKeys = [];
		if ($expire == 0) {
Qiang Xue committed
155
			$this->redis->executeCommand('MSET', $args);
156 157
		} else {
			$expire = (int) ($expire * 1000);
Qiang Xue committed
158 159
			$this->redis->executeCommand('MULTI');
			$this->redis->executeCommand('MSET', $args);
160 161
			$index = [];
			foreach ($data as $key => $value) {
Qiang Xue committed
162
				$this->redis->executeCommand('PEXPIRE', [$key, $expire]);
163 164
				$index[] = $key;
			}
Qiang Xue committed
165
			$result = $this->redis->executeCommand('EXEC');
166 167 168 169 170 171 172 173 174 175 176
			array_shift($result);
			foreach($result as $i => $r) {
				if ($r != 1) {
					$failedKeys[] = $index[$i];
				}
			}
		}
		return $failedKeys;
	}

	/**
Qiang Xue committed
177
	 * @inheritdoc
178 179 180 181
	 */
	protected function addValue($key, $value, $expire)
	{
		if ($expire == 0) {
Qiang Xue committed
182
			return (bool) $this->redis->executeCommand('SET', [$key, $value, 'NX']);
183 184
		} else {
			$expire = (int) ($expire * 1000);
Qiang Xue committed
185
			return (bool) $this->redis->executeCommand('SET', [$key, $value, 'PX', $expire, 'NX']);
186 187 188 189
		}
	}

	/**
Qiang Xue committed
190
	 * @inheritdoc
191 192 193
	 */
	protected function deleteValue($key)
	{
Qiang Xue committed
194
		return (bool) $this->redis->executeCommand('DEL', [$key]);
195 196 197
	}

	/**
Qiang Xue committed
198
	 * @inheritdoc
199 200 201
	 */
	protected function flushValues()
	{
Qiang Xue committed
202
		return $this->redis->executeCommand('FLUSHDB');
203 204
	}
}