Skip to content

Commit 25589cd

Browse files
committed
Real first commit
1 parent 8284852 commit 25589cd

File tree

6 files changed

+541
-2
lines changed

6 files changed

+541
-2
lines changed

Cache.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,137 @@
22

33
namespace dcb9\redis;
44

5+
use yii\di\Instance;
6+
57
class Cache extends \yii\caching\Cache
68
{
9+
/**
10+
* @var Connection|string|array the Redis [[Connection]] object or the application component ID of the Redis [[Connection]].
11+
* This can also be an array that is used to create a redis [[Connection]] instance in case you do not want do configure
12+
* redis connection as an application component.
13+
* After the Cache object is created, if you want to change this property, you should only assign it
14+
* with a Redis [[Connection]] object.
15+
*/
16+
public $redis = 'redis';
17+
18+
/**
19+
* Initializes the redis Cache component.
20+
* This method will initialize the [[redis]] property to make sure it refers to a valid redis connection.
21+
* @throws \yii\base\InvalidConfigException if [[redis]] is invalid.
22+
*/
23+
public function init()
24+
{
25+
parent::init();
26+
$this->redis = Instance::ensure($this->redis, Connection::className());
27+
$this->redis->open();
28+
}
29+
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function exists($key)
35+
{
36+
$key = $this->buildKey($key);
37+
38+
return (bool)$this->redis->exists($key);
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
protected function getValue($key)
45+
{
46+
return $this->redis->get($key);
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
protected function getValues($keys)
53+
{
54+
$response = $this->redis->mget($keys);
55+
$result = [];
56+
$i = 0;
57+
foreach ($keys as $key) {
58+
$result[$key] = $response[$i++];
59+
}
60+
61+
return $result;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
protected function setValue($key, $value, $expire)
68+
{
69+
if ($expire == 0) {
70+
return (bool)$this->redis->set($key, $value);
71+
} else {
72+
return (bool)$this->redis->setex($key, $expire, $value);
73+
}
74+
}
75+
76+
/**
77+
* @inheritdoc
78+
*/
79+
protected function setValues($data, $expire)
80+
{
81+
$args = [];
82+
foreach ($data as $key => $value) {
83+
$args[] = $key;
84+
$args[] = $value;
85+
}
86+
$failedKeys = [];
87+
if ($expire == 0) {
88+
$this->redis->mset($args);
89+
} else {
90+
$expire = (int)($expire * 1000);
91+
$this->redis->multi();
92+
$this->redis->mset($args);
93+
$index = [];
94+
foreach ($data as $key => $value) {
95+
$this->redis->expire($key, $expire);
96+
$index[] = $key;
97+
}
98+
$result = $this->redis->exec();
99+
array_shift($result);
100+
foreach ($result as $i => $r) {
101+
if ($r != 1) {
102+
$failedKeys[] = $index[$i];
103+
}
104+
}
105+
}
106+
107+
return $failedKeys;
108+
}
109+
110+
111+
/**
112+
* @inheritdoc
113+
*/
114+
protected function addValue($key, $value, $expire)
115+
{
116+
if ($expire == 0) {
117+
return (bool)$this->redis->set($key, $value);
118+
}
119+
120+
return (bool)$this->redis->setex($key, $expire, $value);
121+
}
122+
123+
/**
124+
* @inheritdoc
125+
*/
126+
protected function deleteValue($key)
127+
{
128+
return (bool)$this->redis->del($key);
129+
}
7130

131+
/**
132+
* @inheritdoc
133+
*/
134+
protected function flushValues()
135+
{
136+
return $this->redis->flushdb();
137+
}
8138
}

Connection.php

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,113 @@
22

33
namespace dcb9\redis;
44

5-
use yii\base\Component;
5+
use Redis;
6+
use Yii;
7+
use yii\base\Configurable;
8+
use RedisException;
69

7-
class Connection extends Component
10+
/**
11+
* Class Connection
12+
* @package dcb9\redis
13+
*/
14+
class Connection extends Redis implements Configurable
815
{
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;
945

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+
}
10114
}

0 commit comments

Comments
 (0)