87 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace dominion\cache;
 | 
						|
 | 
						|
use Yii;
 | 
						|
use Predis\Client;
 | 
						|
use yii\helpers\Inflector;
 | 
						|
 | 
						|
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;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
}
 |