|
2 | 2 |
|
3 | 3 | namespace dcb9\redis;
|
4 | 4 |
|
5 |
| -use yii\base\Component; |
| 5 | +use Redis; |
| 6 | +use Yii; |
| 7 | +use yii\base\Configurable; |
| 8 | +use RedisException; |
6 | 9 |
|
7 |
| -class Connection extends Component |
| 10 | +/** |
| 11 | + * Class Connection |
| 12 | + * @package dcb9\redis |
| 13 | + */ |
| 14 | +class Connection extends Redis implements Configurable |
8 | 15 | {
|
| 16 | + /** |
| 17 | + * @var string the hostname or ip address to use for connecting to the redis server. Defaults to 'localhost'. |
| 18 | + * If [[unixSocket]] is specified, hostname and port will be ignored. |
| 19 | + */ |
| 20 | + public $hostname = 'localhost'; |
| 21 | + /** |
| 22 | + * @var integer the port to use for connecting to the redis server. Default port is 6379. |
| 23 | + * If [[unixSocket]] is specified, hostname and port will be ignored. |
| 24 | + */ |
| 25 | + public $port = 6379; |
| 26 | + /** |
| 27 | + * @var string the unix socket path (e.g. `/var/run/redis/redis.sock`) to use for connecting to the redis server. |
| 28 | + * This can be used instead of [[hostname]] and [[port]] to connect to the server using a unix socket. |
| 29 | + * If a unix socket path is specified, [[hostname]] and [[port]] will be ignored. |
| 30 | + */ |
| 31 | + public $unixSocket; |
| 32 | + /** |
| 33 | + * @var string the password for establishing DB connection. Defaults to null meaning no AUTH command is send. |
| 34 | + * See http://redis.io/commands/auth |
| 35 | + */ |
| 36 | + public $password; |
| 37 | + /** |
| 38 | + * @var integer the redis database to use. This is an integer value starting from 0. Defaults to 0. |
| 39 | + */ |
| 40 | + public $database = 0; |
| 41 | + /** |
| 42 | + * @var float value in seconds (optional, default is 0.0 meaning unlimited) |
| 43 | + */ |
| 44 | + public $connectionTimeout = 0.0; |
9 | 45 |
|
| 46 | + /** |
| 47 | + * Constructor. |
| 48 | + * The default implementation does two things: |
| 49 | + * |
| 50 | + * - Initializes the object with the given configuration `$config`. |
| 51 | + * - Call [[init()]]. |
| 52 | + * |
| 53 | + * If this method is overridden in a child class, it is recommended that |
| 54 | + * |
| 55 | + * - the last parameter of the constructor is a configuration array, like `$config` here. |
| 56 | + * - call the parent implementation at the end of the constructor. |
| 57 | + * |
| 58 | + * @param array $config name-value pairs that will be used to initialize the object properties |
| 59 | + */ |
| 60 | + public function __construct($config = []) |
| 61 | + { |
| 62 | + if (!empty($config)) { |
| 63 | + Yii::configure($this, $config); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the fully qualified name of this class. |
| 69 | + * @return string the fully qualified name of this class. |
| 70 | + */ |
| 71 | + public static function className() |
| 72 | + { |
| 73 | + return get_called_class(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Establishes a DB connection. |
| 78 | + * It does nothing if a DB connection has already been established. |
| 79 | + * @throws RedisException if connection fails |
| 80 | + */ |
| 81 | + public function open() |
| 82 | + { |
| 83 | + if ($this->unixSocket !== null) { |
| 84 | + $isConnected = $this->connect($this->unixSocket); |
| 85 | + } else { |
| 86 | + $isConnected = $this->connect($this->hostname, $this->port, $this->connectionTimeout); |
| 87 | + } |
| 88 | + |
| 89 | + if ($isConnected === false) { |
| 90 | + throw new RedisException('Connect to redis server error.'); |
| 91 | + } |
| 92 | + |
| 93 | + if ($this->password !== null) { |
| 94 | + $this->auth($this->password); |
| 95 | + } |
| 96 | + |
| 97 | + if ($this->database !== null) { |
| 98 | + $this->select($this->database); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return bool |
| 104 | + */ |
| 105 | + public function ping() |
| 106 | + { |
| 107 | + return parent::ping() === '+PONG'; |
| 108 | + } |
| 109 | + |
| 110 | + public function flushdb() |
| 111 | + { |
| 112 | + return parent::flushDB(); |
| 113 | + } |
10 | 114 | }
|
0 commit comments