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