#110864 Конфигурация yii для работы с кластерным redis
This commit is contained in:
parent
72f50116d6
commit
5d359453e6
|
@ -0,0 +1,85 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace dominion\cache;
|
||||||
|
|
||||||
|
use Yii;
|
||||||
|
use Predis\Client;
|
||||||
|
|
||||||
|
class Connection extends \yii\redis\Connection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var mixed Connection parameters for one or more servers.
|
||||||
|
*/
|
||||||
|
public $parameters;
|
||||||
|
/**
|
||||||
|
* @var mixed Options to configure some behaviours of the client.
|
||||||
|
*/
|
||||||
|
public $options;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Client redis socket connection
|
||||||
|
*/
|
||||||
|
private $_socket = false;
|
||||||
|
|
||||||
|
public function __call($name, $params)
|
||||||
|
{
|
||||||
|
$redisCommand = strtoupper(Inflector::camel2words($name, false));
|
||||||
|
if (in_array($redisCommand, $this->redisCommands)) {
|
||||||
|
return $this->executeCommand($redisCommand, $params);
|
||||||
|
} else {
|
||||||
|
return parent::__call($name, $params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function executeCommand($name, $params = [])
|
||||||
|
{
|
||||||
|
$this->open();
|
||||||
|
Yii::debug("Executing Redis Command: {$name} ". implode(' ', $params), __METHOD__);
|
||||||
|
return $this->_socket->executeCommand(
|
||||||
|
$this->_socket->createCommand($name, $params)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the connection when this component is being serialized.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function __sleep()
|
||||||
|
{
|
||||||
|
$this->close();
|
||||||
|
return array_keys(get_object_vars($this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a value indicating whether the DB connection is established.
|
||||||
|
* @return bool whether the DB connection is established
|
||||||
|
*/
|
||||||
|
public function getIsActive()
|
||||||
|
{
|
||||||
|
return $this->_socket !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function open()
|
||||||
|
{
|
||||||
|
if ($this->_socket !== false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Yii::debug('Opening redis DB connection: ' /*. var_export($this->parameters, true)*/, __METHOD__);
|
||||||
|
$this->_socket = new Client($this->parameters, $this->options);
|
||||||
|
$this->initConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the currently active DB connection.
|
||||||
|
* It does nothing if the connection is already closed.
|
||||||
|
*/
|
||||||
|
public function close()
|
||||||
|
{
|
||||||
|
if ($this->_socket !== false) {
|
||||||
|
Yii::debug('Closing DB connection: ' . var_export($this->parameters, true), __METHOD__);
|
||||||
|
$this->_socket->disconnect();
|
||||||
|
$this->_socket = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
270
RedisCache.php
270
RedisCache.php
|
@ -3,35 +3,34 @@
|
||||||
namespace dominion\cache;
|
namespace dominion\cache;
|
||||||
|
|
||||||
use Yii;
|
use Yii;
|
||||||
use Redis;
|
|
||||||
use yii\console\Exception;
|
|
||||||
|
|
||||||
class RedisCache
|
class RedisCache
|
||||||
{
|
{
|
||||||
protected static $redis;
|
|
||||||
/**
|
/**
|
||||||
* используется для отключения кеширования
|
* используется для отключения кеширования
|
||||||
* например при реиндексе или дебаге
|
* например при реиндексе или дебаге
|
||||||
* @var type
|
* @var type
|
||||||
*/
|
*/
|
||||||
protected static $active = true;
|
protected static $active = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* время жизни кеша в секундах
|
* время жизни кеша в секундах
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected static $livetime = 300;
|
protected static $livetime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* начальная директория для кеша в редисе
|
* начальная директория для кеша в редисе
|
||||||
* @var type
|
* @var type
|
||||||
*/
|
*/
|
||||||
protected static $prefix = '';
|
protected static $prefix;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Время чкрез ктр удалить кешь
|
* Время чкрез ктр удалить кешь
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected static $keysDeleteTime = 30;
|
protected static $keysDeleteTime = 30;
|
||||||
|
|
||||||
protected static $setDeleteKey = false;
|
protected static $setDeleteKey = false;
|
||||||
|
|
||||||
public static function setActive($active)
|
public static function setActive($active)
|
||||||
|
@ -49,93 +48,56 @@ class RedisCache
|
||||||
self::$setDeleteKey = $setDeleteKey;
|
self::$setDeleteKey = $setDeleteKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function connection($reConnect = false)
|
protected static function livetime()
|
||||||
{
|
{
|
||||||
if(!self::$redis || $reConnect)
|
if (!self::$livetime)
|
||||||
{
|
{
|
||||||
if(empty(Yii::$app->params['redis'])) {
|
self::$livetime = isset(Yii::$app->params['redis'], Yii::$app->params['redis']['livetime']) ? (int) Yii::$app->params['redis']['livetime'] : 300;
|
||||||
throw new Exception('Отсутствует конфигурация для redis');
|
|
||||||
}
|
}
|
||||||
if(empty(Yii::$app->params['redis']['host'])) {
|
return self::$livetime;
|
||||||
throw new Exception('Отсутствует конфигурация для redis host');
|
|
||||||
}
|
|
||||||
if(empty(Yii::$app->params['redis']['port'])) {
|
|
||||||
throw new Exception('Отсутствует конфигурация для redis port');
|
|
||||||
}
|
|
||||||
if(empty(Yii::$app->params['redis']['prefix'])) {
|
|
||||||
throw new Exception('Отсутствует конфигурация для redis prefix');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$redis = new Redis;
|
protected static function prefix()
|
||||||
$redis->connect(Yii::$app->params['redis']['host'], Yii::$app->params['redis']['port']);
|
|
||||||
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);
|
|
||||||
$redis->setOption(Redis::OPT_PREFIX, Yii::$app->params['redis']['prefix']);
|
|
||||||
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
|
|
||||||
|
|
||||||
self::$redis = $redis;
|
|
||||||
self::$livetime = isset(Yii::$app->params['redis']['livetime']) ? (int)Yii::$app->params['redis']['livetime'] : self::$livetime;
|
|
||||||
self::$prefix = Yii::$app->params['redis']['prefix'];
|
|
||||||
}
|
|
||||||
return self::$redis;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function hdel($key, $value, $connect)
|
|
||||||
{
|
{
|
||||||
if(self::$setDeleteKey)
|
if (!self::$prefix)
|
||||||
|
{
|
||||||
|
self::$prefix = isset(Yii::$app->params['redis'], Yii::$app->params['redis']['prefix']) ? (int) Yii::$app->params['redis']['prefix'] : '';
|
||||||
|
}
|
||||||
|
return self::$prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function calculateKey($key)
|
||||||
|
{
|
||||||
|
return self::prefix() . $key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function hdel($key, $value)
|
||||||
|
{
|
||||||
|
if (self::$setDeleteKey)
|
||||||
{
|
{
|
||||||
self::addDeleteKey($key, $value);
|
self::addDeleteKey($key, $value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$prefix = self::$prefix;
|
Yii::$app->redis->hdel(self::calculateKey($key), $value);
|
||||||
$raw = "HDEL {$prefix}{$key} {$value}";
|
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
$connect->hdel($key, $value);
|
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function del($key, $realDelete = false)
|
public static function del($key, $realDelete = false)
|
||||||
{
|
{
|
||||||
if(self::$setDeleteKey && !$realDelete)
|
if (self::$setDeleteKey && !$realDelete)
|
||||||
{
|
{
|
||||||
self::addDeleteKey($key);
|
self::addDeleteKey($key);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$prefix = self::$prefix;
|
Yii::$app->redis->del(self::calculateKey($key));
|
||||||
$raw = "DEL {$prefix}{$key}";
|
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
try
|
|
||||||
{
|
|
||||||
self::connection()->del($key);
|
|
||||||
}
|
|
||||||
catch (\Exception $exc)
|
|
||||||
{
|
|
||||||
self::connection(true)->del($key);
|
|
||||||
}
|
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function hset($key, $field, $value)
|
public static function hset($key, $field, $value)
|
||||||
{
|
{
|
||||||
$result = false;
|
return self::getActive() ? Yii::$app->redis->hset(self::calculateKey($key), $field, $value) : false;
|
||||||
if(self::getActive())
|
|
||||||
{
|
|
||||||
$prefix = self::$prefix;
|
|
||||||
$raw = "HSET {$prefix}{$key} {$field}";
|
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$result = self::connection()->hset($key, $field, $value);
|
|
||||||
}
|
|
||||||
catch (\Exception $exc)
|
|
||||||
{
|
|
||||||
$result = self::connection(true)->hset($key, $field, $value);
|
|
||||||
}
|
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function hsetModel($key, $field, $model)
|
public static function hsetModel($key, $field, $model)
|
||||||
|
@ -158,7 +120,7 @@ class RedisCache
|
||||||
{
|
{
|
||||||
$values[] = $model->attributes;
|
$values[] = $model->attributes;
|
||||||
}
|
}
|
||||||
if($page)
|
if ($page)
|
||||||
{
|
{
|
||||||
$values['page'] = [
|
$values['page'] = [
|
||||||
'totalCount' => $page->totalCount,
|
'totalCount' => $page->totalCount,
|
||||||
|
@ -172,23 +134,7 @@ class RedisCache
|
||||||
|
|
||||||
public static function hget($key, $field)
|
public static function hget($key, $field)
|
||||||
{
|
{
|
||||||
$result = false;
|
return self::getActive() ? Yii::$app->redis->hget(self::calculateKey($key), $field) : false;
|
||||||
if(self::getActive())
|
|
||||||
{
|
|
||||||
$prefix = self::$prefix;
|
|
||||||
$raw = "HGET {$prefix}{$key} {$field}";
|
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$result = self::connection()->hget($key, $field);
|
|
||||||
}
|
|
||||||
catch (\Exception $exc)
|
|
||||||
{
|
|
||||||
$result = self::connection(true)->hget($key, $field);
|
|
||||||
}
|
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function hgetModel($key, $field, $className)
|
public static function hgetModel($key, $field, $className)
|
||||||
|
@ -199,19 +145,19 @@ class RedisCache
|
||||||
|
|
||||||
public static function hgetModelArray($key, $field, $className, $page = null)
|
public static function hgetModelArray($key, $field, $className, $page = null)
|
||||||
{
|
{
|
||||||
if($page)
|
if ($page)
|
||||||
{
|
{
|
||||||
$field .= ":page:{$page->page}:perPage:{$page->perPage}:sort:{$page->sort}";
|
$field .= ":page:{$page->page}:perPage:{$page->perPage}:sort:{$page->sort}";
|
||||||
}
|
}
|
||||||
$values = self::hget($key, $field);
|
$values = self::hget($key, $field);
|
||||||
if($values === false)
|
if ($values === false)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$ids = [];
|
$ids = [];
|
||||||
foreach ($values as $keyVal => $value)
|
foreach ($values as $keyVal => $value)
|
||||||
{
|
{
|
||||||
if($keyVal !== 'page' && isset($value['id']))
|
if ($keyVal !== 'page' && isset($value['id']))
|
||||||
{
|
{
|
||||||
$ids[] = $value['id'];
|
$ids[] = $value['id'];
|
||||||
}
|
}
|
||||||
|
@ -219,9 +165,9 @@ class RedisCache
|
||||||
$output = [];
|
$output = [];
|
||||||
foreach ($values as $keyVal => $value)
|
foreach ($values as $keyVal => $value)
|
||||||
{
|
{
|
||||||
if($keyVal === 'page')
|
if ($keyVal === 'page')
|
||||||
{
|
{
|
||||||
if($page)
|
if ($page)
|
||||||
{
|
{
|
||||||
$page->setMeta($value['totalCount'], $value['perPage']);
|
$page->setMeta($value['totalCount'], $value['perPage']);
|
||||||
$page->sort = $value['sort'];
|
$page->sort = $value['sort'];
|
||||||
|
@ -230,7 +176,7 @@ class RedisCache
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$model = new $className($value);
|
$model = new $className($value);
|
||||||
if(isset($model->ids))
|
if (isset($model->ids))
|
||||||
{
|
{
|
||||||
$model->ids = $ids;
|
$model->ids = $ids;
|
||||||
}
|
}
|
||||||
|
@ -242,29 +188,24 @@ class RedisCache
|
||||||
|
|
||||||
public static function delete($key, array $patterns)
|
public static function delete($key, array $patterns)
|
||||||
{
|
{
|
||||||
if(self::getActive())
|
if (self::getActive())
|
||||||
{
|
{
|
||||||
$prefix = self::$prefix;
|
foreach ($patterns as $pattern)
|
||||||
$connect = self::connection();
|
|
||||||
foreach($patterns as $pattern)
|
|
||||||
{
|
{
|
||||||
$cursor = NULL;
|
$cursor = NULL;
|
||||||
foreach ($patterns as $pattern)
|
foreach ($patterns as $pattern)
|
||||||
{
|
{
|
||||||
$arKeys = [];
|
$arKeys = [];
|
||||||
$raw = "HSCAN {$prefix}{$key} {$cursor} MATCH {$pattern}";
|
while ($values = Yii::$app->redis->hscan(self::calculateKey($key), $cursor, $pattern))
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
while($values = $connect->hscan($key, $cursor, $pattern))
|
|
||||||
{
|
{
|
||||||
foreach($values as $vkey => $value)
|
foreach ($values as $vkey => $value)
|
||||||
{
|
{
|
||||||
$arKeys[] = $vkey;
|
$arKeys[] = $vkey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
foreach ($arKeys as $value)
|
||||||
foreach($arKeys as $value)
|
|
||||||
{
|
{
|
||||||
self::hdel($key, $value, $connect);
|
self::hdel($key, $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -278,7 +219,7 @@ class RedisCache
|
||||||
*/
|
*/
|
||||||
public static function deleteAll($key, $realDelete = false)
|
public static function deleteAll($key, $realDelete = false)
|
||||||
{
|
{
|
||||||
if(self::getActive())
|
if (self::getActive())
|
||||||
{
|
{
|
||||||
self::del($key, $realDelete);
|
self::del($key, $realDelete);
|
||||||
}
|
}
|
||||||
|
@ -286,23 +227,13 @@ class RedisCache
|
||||||
|
|
||||||
public static function getKeyTypes()
|
public static function getKeyTypes()
|
||||||
{
|
{
|
||||||
$prefix = self::$prefix;
|
|
||||||
$raw = "KEYS {$prefix}*";
|
$result = Yii::$app->redis->keys(self::calculateKey('*'));
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$result = self::connection()->keys('*');
|
|
||||||
}
|
|
||||||
catch (\Exception $exc)
|
|
||||||
{
|
|
||||||
$result = self::connection(true)->keys('*');
|
|
||||||
}
|
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
|
||||||
$output = ['*'];
|
$output = ['*'];
|
||||||
foreach ($result as $val)
|
foreach ($result as $val)
|
||||||
{
|
{
|
||||||
$arVal = explode(':', $val);
|
$arVal = explode(':', $val);
|
||||||
if(isset($arVal[1]) && !in_array($arVal[1], $output))
|
if (isset($arVal[1]) && !in_array($arVal[1], $output))
|
||||||
{
|
{
|
||||||
$output[] = $arVal[1];
|
$output[] = $arVal[1];
|
||||||
}
|
}
|
||||||
|
@ -318,25 +249,14 @@ class RedisCache
|
||||||
*/
|
*/
|
||||||
public static function deleteType($type)
|
public static function deleteType($type)
|
||||||
{
|
{
|
||||||
if(self::getActive())
|
if (self::getActive())
|
||||||
{
|
{
|
||||||
self::deleteAll($type);
|
self::deleteAll($type);
|
||||||
$type = $type == "*" ? "*" : "{$type}:*";
|
$type = $type == "*" ? "*" : "{$type}:*";
|
||||||
$prefix = self::$prefix;
|
$result = Yii::$app->redis->keys(self::calculateKey($type));
|
||||||
$raw = "KEYS {$prefix}{$type}";
|
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$result = self::connection()->keys($type);
|
|
||||||
}
|
|
||||||
catch (\Exception $exc)
|
|
||||||
{
|
|
||||||
$result = self::connection(true)->keys($type);
|
|
||||||
}
|
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
|
||||||
foreach ($result as $val)
|
foreach ($result as $val)
|
||||||
{
|
{
|
||||||
self::deleteAll(str_replace($prefix, '', $val));
|
self::deleteAll(str_replace(self::prefix(), '', $val));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -345,34 +265,18 @@ class RedisCache
|
||||||
public static function set($key, $value, $options = null)
|
public static function set($key, $value, $options = null)
|
||||||
{
|
{
|
||||||
$result = false;
|
$result = false;
|
||||||
if(self::getActive())
|
if (self::getActive())
|
||||||
{
|
{
|
||||||
if($options === true)
|
if ($options === true)
|
||||||
{
|
{
|
||||||
$options = ['EX' => self::$livetime];
|
$options = ['EX' => self::livetime()];
|
||||||
}
|
}
|
||||||
elseif(is_int($options))
|
elseif (is_int($options))
|
||||||
{
|
{
|
||||||
$options = ['EX' => $options];
|
$options = ['EX' => $options];
|
||||||
}
|
}
|
||||||
$addStr = '';
|
|
||||||
foreach((array)$options as $okey => $oval)
|
|
||||||
{
|
|
||||||
$addStr .= "{$okey} {$oval}";
|
|
||||||
}
|
|
||||||
$prefix = self::$prefix;
|
|
||||||
$json = json_encode($value);
|
$json = json_encode($value);
|
||||||
$raw = "SET {$prefix}{$key} {$json} {$addStr}";
|
Yii::$app->redis->set(self::calculateKey($key), $json, $options);
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$result = self::connection()->set($key, $json, $options);
|
|
||||||
}
|
|
||||||
catch (\Exception $exc)
|
|
||||||
{
|
|
||||||
$result = self::connection(true)->set($key, $json, $options);
|
|
||||||
}
|
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
@ -389,7 +293,7 @@ class RedisCache
|
||||||
{
|
{
|
||||||
$values[] = $model->attributes;
|
$values[] = $model->attributes;
|
||||||
}
|
}
|
||||||
if($page)
|
if ($page)
|
||||||
{
|
{
|
||||||
$values['page'] = [
|
$values['page'] = [
|
||||||
'totalCount' => $page->totalCount,
|
'totalCount' => $page->totalCount,
|
||||||
|
@ -403,20 +307,9 @@ class RedisCache
|
||||||
public static function get(string $key)
|
public static function get(string $key)
|
||||||
{
|
{
|
||||||
$result = false;
|
$result = false;
|
||||||
if(self::getActive())
|
if (self::getActive())
|
||||||
{
|
{
|
||||||
$prefix = self::$prefix;
|
$result = Yii::$app->redis->get(self::calculateKey($key));
|
||||||
$raw = "GET {$prefix}{$key}";
|
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$result = self::connection()->get($key);
|
|
||||||
}
|
|
||||||
catch (\Exception $exc)
|
|
||||||
{
|
|
||||||
$result = self::connection(true)->get($key);
|
|
||||||
}
|
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
|
||||||
}
|
}
|
||||||
return $result ? json_decode($result, true) : $result;
|
return $result ? json_decode($result, true) : $result;
|
||||||
}
|
}
|
||||||
|
@ -430,14 +323,14 @@ class RedisCache
|
||||||
public static function getModelArray($key, $className, $page = null)
|
public static function getModelArray($key, $className, $page = null)
|
||||||
{
|
{
|
||||||
$values = self::get($key);
|
$values = self::get($key);
|
||||||
if($values === false)
|
if ($values === false)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$ids = [];
|
$ids = [];
|
||||||
foreach ($values as $keyVal => $value)
|
foreach ($values as $keyVal => $value)
|
||||||
{
|
{
|
||||||
if($keyVal !== 'page' && isset($value['id']))
|
if ($keyVal !== 'page' && isset($value['id']))
|
||||||
{
|
{
|
||||||
$ids[] = $value['id'];
|
$ids[] = $value['id'];
|
||||||
}
|
}
|
||||||
|
@ -445,9 +338,9 @@ class RedisCache
|
||||||
$output = [];
|
$output = [];
|
||||||
foreach ($values as $keyVal => $value)
|
foreach ($values as $keyVal => $value)
|
||||||
{
|
{
|
||||||
if($keyVal === 'page')
|
if ($keyVal === 'page')
|
||||||
{
|
{
|
||||||
if($page)
|
if ($page)
|
||||||
{
|
{
|
||||||
$page->setMeta($value['totalCount'], $value['perPage']);
|
$page->setMeta($value['totalCount'], $value['perPage']);
|
||||||
$page->sort = $value['sort'];
|
$page->sort = $value['sort'];
|
||||||
|
@ -456,7 +349,7 @@ class RedisCache
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$model = new $className($value);
|
$model = new $className($value);
|
||||||
if(isset($model->ids))
|
if (isset($model->ids))
|
||||||
{
|
{
|
||||||
$model->ids = $ids;
|
$model->ids = $ids;
|
||||||
}
|
}
|
||||||
|
@ -474,55 +367,46 @@ class RedisCache
|
||||||
*/
|
*/
|
||||||
public static function addDeleteKey($key, $field = '-')
|
public static function addDeleteKey($key, $field = '-')
|
||||||
{
|
{
|
||||||
return self::hset('del:'.$key, $field, time() + self::$keysDeleteTime);
|
return self::hset('del:' . $key, $field, time() + self::$keysDeleteTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Удаляет устаревший кеш
|
* Удаляет устаревший кеш
|
||||||
* @return type
|
* @return type
|
||||||
*/
|
*/
|
||||||
public static function oldCacheDelete()
|
public static function oldCacheDelete()
|
||||||
{
|
{
|
||||||
$connect = self::connection();
|
$result = Yii::$app->redis->keys(self::calculateKey('del:*'));
|
||||||
$prefix = self::$prefix;
|
|
||||||
$raw = "KEYS {$prefix}del:*";
|
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
$result = $connect->keys('del:*');
|
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
|
||||||
foreach ($result as $val)
|
foreach ($result as $val)
|
||||||
{
|
{
|
||||||
$cursor = null;
|
$cursor = null;
|
||||||
//$key = str_replace("{$prefix}del:", '', $val);
|
$key = str_replace(self::prefix(), '', $val);
|
||||||
$key = str_replace($prefix, '', $val);
|
|
||||||
$pattern = '*';
|
$pattern = '*';
|
||||||
$arKeys = [];
|
$arKeys = [];
|
||||||
$raw = "HSCAN {$prefix}{$key} {$cursor} MATCH {$pattern}";
|
while ($values = Yii::$app->redis->hscan(self::calculateKey($key), $cursor, $pattern))
|
||||||
Yii::beginProfile($raw, 'yii\db\Command::query');
|
|
||||||
while($values = $connect->hscan($key, $cursor, $pattern))
|
|
||||||
{
|
{
|
||||||
foreach($values as $vkey => $value)
|
foreach ($values as $vkey => $value)
|
||||||
{
|
{
|
||||||
if($value < time())
|
if ($value < time())
|
||||||
{
|
{
|
||||||
$arKeys[] = $vkey;
|
$arKeys[] = $vkey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$keyCache = str_replace("{$prefix}del:", '', $val);
|
$keyCache = str_replace(self::prefix() . "del:", '', $val);
|
||||||
Yii::endProfile($raw, 'yii\db\Command::query');
|
foreach ($arKeys as $value)
|
||||||
foreach($arKeys as $value)
|
|
||||||
{
|
{
|
||||||
self::$setDeleteKey = false;
|
self::$setDeleteKey = false;
|
||||||
if($value == '-')
|
if ($value == '-')
|
||||||
{
|
{
|
||||||
self::del($keyCache, $connect);
|
self::del($keyCache);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
self::hdel($keyCache, $value, $connect);
|
self::hdel($keyCache, $value);
|
||||||
}
|
}
|
||||||
self::hdel($key, $value, $connect);
|
self::hdel($key, $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue