From 0b13c43466ae7531a08e6830b383891a241897f6 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 24 Nov 2013 20:48:40 -0500 Subject: [PATCH 1/2] refactored redis cache. --- extensions/redis/Cache.php | 71 ++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 34 deletions(-) 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'); } } From c6318050d99044a2714ad1a72c8021f20a725d62 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 24 Nov 2013 21:29:11 -0500 Subject: [PATCH 2/2] removed unneeded setting. --- apps/advanced/backend/config/main.php | 3 --- apps/advanced/frontend/config/main.php | 3 --- apps/basic/config/web.php | 3 --- 3 files changed, 9 deletions(-) diff --git a/apps/advanced/backend/config/main.php b/apps/advanced/backend/config/main.php index f2745e2..c745a1f 100644 --- a/apps/advanced/backend/config/main.php +++ b/apps/advanced/backend/config/main.php @@ -17,9 +17,6 @@ return [ 'modules' => [], 'extensions' => require($rootDir . '/vendor/yiisoft/extensions.php'), 'components' => [ - 'request' => [ - 'enableCsrfValidation' => true, - ], 'db' => $params['components.db'], 'cache' => $params['components.cache'], 'mail' => $params['components.mail'], diff --git a/apps/advanced/frontend/config/main.php b/apps/advanced/frontend/config/main.php index e0fd2de..6ee8ae5 100644 --- a/apps/advanced/frontend/config/main.php +++ b/apps/advanced/frontend/config/main.php @@ -18,9 +18,6 @@ return [ ], 'extensions' => require($rootDir . '/vendor/yiisoft/extensions.php'), 'components' => [ - 'request' => [ - 'enableCsrfValidation' => true, - ], 'db' => $params['components.db'], 'cache' => $params['components.cache'], 'mail' => $params['components.mail'], diff --git a/apps/basic/config/web.php b/apps/basic/config/web.php index cf921b0..6124c4f 100644 --- a/apps/basic/config/web.php +++ b/apps/basic/config/web.php @@ -5,9 +5,6 @@ $config = [ 'basePath' => dirname(__DIR__), 'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'), 'components' => [ - 'request' => [ - 'enableCsrfValidation' => true, - ], 'cache' => [ 'class' => 'yii\caching\FileCache', ],