Browse Source

Merge branch 'master' of github.com:yiisoft/yii2

* 'master' of github.com:yiisoft/yii2:
  removed unneeded setting.
  refactored redis cache.
tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
38e5788e5c
  1. 3
      apps/advanced/backend/config/main.php
  2. 3
      apps/advanced/frontend/config/main.php
  3. 3
      apps/basic/config/web.php
  4. 71
      extensions/redis/Cache.php

3
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'],

3
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'],

3
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',
],

71
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');
}
}

Loading…
Cancel
Save