#110864 Конфигурация yii для работы с кластерным redis
This commit is contained in:
		
							
								
								
									
										85
									
								
								Connection.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								Connection.php
									
									
									
									
									
										Normal file
									
								
							@@ -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;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										202
									
								
								RedisCache.php
									
									
									
									
									
								
							
							
						
						
									
										202
									
								
								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,37 +48,30 @@ 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);
 | 
					        if (!self::$prefix)
 | 
				
			||||||
            $redis->setOption(Redis::OPT_PREFIX, Yii::$app->params['redis']['prefix']);
 | 
					        {
 | 
				
			||||||
            $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
 | 
					            self::$prefix = isset(Yii::$app->params['redis'], Yii::$app->params['redis']['prefix']) ? (int) Yii::$app->params['redis']['prefix'] : '';
 | 
				
			||||||
 | 
					 | 
				
			||||||
            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;
 | 
					        return self::$prefix;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static function hdel($key, $value, $connect)
 | 
					    protected static function calculateKey($key)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return self::prefix() . $key;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static function hdel($key, $value)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (self::$setDeleteKey)
 | 
					        if (self::$setDeleteKey)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -87,13 +79,10 @@ class RedisCache
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        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)
 | 
				
			||||||
@@ -102,40 +91,13 @@ class RedisCache
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        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)
 | 
				
			||||||
@@ -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)
 | 
				
			||||||
@@ -244,27 +190,22 @@ class RedisCache
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        if (self::getActive())
 | 
					        if (self::getActive())
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            $prefix = self::$prefix;
 | 
					 | 
				
			||||||
            $connect = self::connection();
 | 
					 | 
				
			||||||
            foreach ($patterns as $pattern)
 | 
					            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);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -286,18 +227,8 @@ 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)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -322,21 +253,10 @@ class RedisCache
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            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;
 | 
				
			||||||
@@ -349,30 +269,14 @@ class RedisCache
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            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;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -405,18 +309,7 @@ class RedisCache
 | 
				
			|||||||
        $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;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -476,28 +369,21 @@ class RedisCache
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        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)
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
@@ -507,22 +393,20 @@ class RedisCache
 | 
				
			|||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            $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);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user