Commit 0b13c434 by Qiang Xue

refactored redis cache.

parent 8ac2b736
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
namespace yii\redis; 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. * Redis Cache implements a cache application component based on [redis](http://redis.io/) key-value store.
* *
...@@ -46,13 +49,30 @@ namespace yii\redis; ...@@ -46,13 +49,30 @@ namespace yii\redis;
class Cache extends \yii\caching\Cache class Cache extends \yii\caching\Cache
{ {
/** /**
* @var string the id of the application component to use as the redis connection. * @var Connection|string the Redis [[Connection]] object or the application component ID of the Redis [[Connection]].
* It should be configured as a [[yii\redis\Connection]]. Defaults to `redis`. * 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. * 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. * 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 * Note that this method does not check whether the dependency associated
...@@ -64,9 +84,7 @@ class Cache extends \yii\caching\Cache ...@@ -64,9 +84,7 @@ class Cache extends \yii\caching\Cache
*/ */
public function exists($key) public function exists($key)
{ {
/** @var Connection $connection */ return (bool) $this->redis->executeCommand('EXISTS', [$this->buildKey($key)]);
$connection = \Yii::$app->getComponent($this->connectionId);
return (bool) $connection->executeCommand('EXISTS', [$this->buildKey($key)]);
} }
/** /**
...@@ -74,9 +92,7 @@ class Cache extends \yii\caching\Cache ...@@ -74,9 +92,7 @@ class Cache extends \yii\caching\Cache
*/ */
protected function getValue($key) protected function getValue($key)
{ {
/** @var Connection $connection */ return $this->redis->executeCommand('GET', [$key]);
$connection = \Yii::$app->getComponent($this->connectionId);
return $connection->executeCommand('GET', [$key]);
} }
/** /**
...@@ -84,9 +100,7 @@ class Cache extends \yii\caching\Cache ...@@ -84,9 +100,7 @@ class Cache extends \yii\caching\Cache
*/ */
protected function getValues($keys) protected function getValues($keys)
{ {
/** @var Connection $connection */ $response = $this->redis->executeCommand('MGET', $keys);
$connection = \Yii::$app->getComponent($this->connectionId);
$response = $connection->executeCommand('MGET', $keys);
$result = []; $result = [];
$i = 0; $i = 0;
foreach ($keys as $key) { foreach ($keys as $key) {
...@@ -100,13 +114,11 @@ class Cache extends \yii\caching\Cache ...@@ -100,13 +114,11 @@ class Cache extends \yii\caching\Cache
*/ */
protected function setValue($key, $value, $expire) protected function setValue($key, $value, $expire)
{ {
/** @var Connection $connection */
$connection = \Yii::$app->getComponent($this->connectionId);
if ($expire == 0) { if ($expire == 0) {
return (bool) $connection->executeCommand('SET', [$key, $value]); return (bool) $this->redis->executeCommand('SET', [$key, $value]);
} else { } else {
$expire = (int) ($expire * 1000); $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 ...@@ -115,9 +127,6 @@ class Cache extends \yii\caching\Cache
*/ */
protected function setValues($data, $expire) protected function setValues($data, $expire)
{ {
/** @var Connection $connection */
$connection = \Yii::$app->getComponent($this->connectionId);
$args = []; $args = [];
foreach($data as $key => $value) { foreach($data as $key => $value) {
$args[] = $key; $args[] = $key;
...@@ -126,17 +135,17 @@ class Cache extends \yii\caching\Cache ...@@ -126,17 +135,17 @@ class Cache extends \yii\caching\Cache
$failedKeys = []; $failedKeys = [];
if ($expire == 0) { if ($expire == 0) {
$connection->executeCommand('MSET', $args); $this->redis->executeCommand('MSET', $args);
} else { } else {
$expire = (int) ($expire * 1000); $expire = (int) ($expire * 1000);
$connection->executeCommand('MULTI'); $this->redis->executeCommand('MULTI');
$connection->executeCommand('MSET', $args); $this->redis->executeCommand('MSET', $args);
$index = []; $index = [];
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
$connection->executeCommand('PEXPIRE', [$key, $expire]); $this->redis->executeCommand('PEXPIRE', [$key, $expire]);
$index[] = $key; $index[] = $key;
} }
$result = $connection->executeCommand('EXEC'); $result = $this->redis->executeCommand('EXEC');
array_shift($result); array_shift($result);
foreach($result as $i => $r) { foreach($result as $i => $r) {
if ($r != 1) { if ($r != 1) {
...@@ -152,13 +161,11 @@ class Cache extends \yii\caching\Cache ...@@ -152,13 +161,11 @@ class Cache extends \yii\caching\Cache
*/ */
protected function addValue($key, $value, $expire) protected function addValue($key, $value, $expire)
{ {
/** @var Connection $connection */
$connection = \Yii::$app->getComponent($this->connectionId);
if ($expire == 0) { if ($expire == 0) {
return (bool) $connection->executeCommand('SET', [$key, $value, 'NX']); return (bool) $this->redis->executeCommand('SET', [$key, $value, 'NX']);
} else { } else {
$expire = (int) ($expire * 1000); $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 ...@@ -167,9 +174,7 @@ class Cache extends \yii\caching\Cache
*/ */
protected function deleteValue($key) protected function deleteValue($key)
{ {
/** @var Connection $connection */ return (bool) $this->redis->executeCommand('DEL', [$key]);
$connection = \Yii::$app->getComponent($this->connectionId);
return (bool) $connection->executeCommand('DEL', [$key]);
} }
/** /**
...@@ -177,8 +182,6 @@ class Cache extends \yii\caching\Cache ...@@ -177,8 +182,6 @@ class Cache extends \yii\caching\Cache
*/ */
protected function flushValues() protected function flushValues()
{ {
/** @var Connection $connection */ return $this->redis->executeCommand('FLUSHDB');
$connection = \Yii::$app->getComponent($this->connectionId);
return $connection->executeCommand('FLUSHDB');
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment