diff --git a/extensions/redis/Cache.php b/extensions/redis/Cache.php index f1a21ed..a7a535c 100644 --- a/extensions/redis/Cache.php +++ b/extensions/redis/Cache.php @@ -7,6 +7,9 @@ namespace yii\redis; +use Yii; +use yii\base\InvalidConfigException; + /** * Redis Cache implements a cache application component based on [redis](http://redis.io/) key-value store. * @@ -46,13 +49,30 @@ namespace yii\redis; class Cache extends \yii\caching\Cache { /** - * @var string the id of the application component to use as the redis connection. - * It should be configured as a [[yii\redis\Connection]]. Defaults to `redis`. + * @var Connection|string the Redis [[Connection]] object or the application component ID of the Redis [[Connection]]. + * After the Cache object is created, if you want to change this property, you should only assign it + * with a Redis [[Connection]] object. */ - public $connectionId = 'redis'; + public $redis = 'redis'; /** + * Initializes the DbCache component. + * This method will initialize the [[db]] property to make sure it refers to a valid DB connection. + * @throws InvalidConfigException if [[db]] is invalid. + */ + public function init() + { + parent::init(); + if (is_string($this->redis)) { + $this->redis = Yii::$app->getComponent($this->redis); + } + 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."); + } + } + + /** * 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 @@ -64,9 +84,7 @@ class Cache extends \yii\caching\Cache */ public function exists($key) { - /** @var Connection $connection */ - $connection = \Yii::$app->getComponent($this->connectionId); - return (bool) $connection->executeCommand('EXISTS', [$this->buildKey($key)]); + return (bool) $this->redis->executeCommand('EXISTS', [$this->buildKey($key)]); } /** @@ -74,9 +92,7 @@ class Cache extends \yii\caching\Cache */ protected function getValue($key) { - /** @var Connection $connection */ - $connection = \Yii::$app->getComponent($this->connectionId); - return $connection->executeCommand('GET', [$key]); + return $this->redis->executeCommand('GET', [$key]); } /** @@ -84,9 +100,7 @@ class Cache extends \yii\caching\Cache */ protected function getValues($keys) { - /** @var Connection $connection */ - $connection = \Yii::$app->getComponent($this->connectionId); - $response = $connection->executeCommand('MGET', $keys); + $response = $this->redis->executeCommand('MGET', $keys); $result = []; $i = 0; foreach ($keys as $key) { @@ -100,13 +114,11 @@ class Cache extends \yii\caching\Cache */ protected function setValue($key, $value, $expire) { - /** @var Connection $connection */ - $connection = \Yii::$app->getComponent($this->connectionId); if ($expire == 0) { - return (bool) $connection->executeCommand('SET', [$key, $value]); + return (bool) $this->redis->executeCommand('SET', [$key, $value]); } else { $expire = (int) ($expire * 1000); - return (bool) $connection->executeCommand('SET', [$key, $value, 'PX', $expire]); + return (bool) $this->redis->executeCommand('SET', [$key, $value, 'PX', $expire]); } } @@ -115,9 +127,6 @@ class Cache extends \yii\caching\Cache */ protected function setValues($data, $expire) { - /** @var Connection $connection */ - $connection = \Yii::$app->getComponent($this->connectionId); - $args = []; foreach($data as $key => $value) { $args[] = $key; @@ -126,17 +135,17 @@ class Cache extends \yii\caching\Cache $failedKeys = []; if ($expire == 0) { - $connection->executeCommand('MSET', $args); + $this->redis->executeCommand('MSET', $args); } else { $expire = (int) ($expire * 1000); - $connection->executeCommand('MULTI'); - $connection->executeCommand('MSET', $args); + $this->redis->executeCommand('MULTI'); + $this->redis->executeCommand('MSET', $args); $index = []; foreach ($data as $key => $value) { - $connection->executeCommand('PEXPIRE', [$key, $expire]); + $this->redis->executeCommand('PEXPIRE', [$key, $expire]); $index[] = $key; } - $result = $connection->executeCommand('EXEC'); + $result = $this->redis->executeCommand('EXEC'); array_shift($result); foreach($result as $i => $r) { if ($r != 1) { @@ -152,13 +161,11 @@ class Cache extends \yii\caching\Cache */ protected function addValue($key, $value, $expire) { - /** @var Connection $connection */ - $connection = \Yii::$app->getComponent($this->connectionId); if ($expire == 0) { - return (bool) $connection->executeCommand('SET', [$key, $value, 'NX']); + return (bool) $this->redis->executeCommand('SET', [$key, $value, 'NX']); } else { $expire = (int) ($expire * 1000); - return (bool) $connection->executeCommand('SET', [$key, $value, 'PX', $expire, 'NX']); + return (bool) $this->redis->executeCommand('SET', [$key, $value, 'PX', $expire, 'NX']); } } @@ -167,9 +174,7 @@ class Cache extends \yii\caching\Cache */ protected function deleteValue($key) { - /** @var Connection $connection */ - $connection = \Yii::$app->getComponent($this->connectionId); - return (bool) $connection->executeCommand('DEL', [$key]); + return (bool) $this->redis->executeCommand('DEL', [$key]); } /** @@ -177,8 +182,6 @@ class Cache extends \yii\caching\Cache */ protected function flushValues() { - /** @var Connection $connection */ - $connection = \Yii::$app->getComponent($this->connectionId); - return $connection->executeCommand('FLUSHDB'); + return $this->redis->executeCommand('FLUSHDB'); } }