-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRedisDriver.php
94 lines (77 loc) · 1.95 KB
/
RedisDriver.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
<?php
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\MultipleInterface;
use Psr\SimpleCache\IncrementableInterface;
/**
* Redis cache driver.
*
* @author Paul Dragoonis <[email protected]>
* @package PPI
* @subpackage Cache
*/
class RedisDriver implements CacheInterface
{
protected $redis;
public function __construct(\Redis $redis)
{
$this->redis = $redis;
}
public function get($key)
{
return $this->redis->get($key);
}
public function set($key, $value, $ttl = null)
{
return $this->redis->set($key, $value, $ttl);
}
public function delete($key)
{
$this->redis->delete($key);
}
public function clear()
{
$this->redis->flushAll();
}
public function getMultiple($keys)
{
$cacheValues = array_combine($keys, $this->redis->mGet($keys));
foreach ($cacheValues as $key => $value) {
if($value === false && !$this->redis->exists($key)) {
continue;
}
$ret[$key] = $value;
}
return $ret;
}
public function setMultiple($data, $ttl = null)
{
// No native TTL support for MSET so we use a transaction
$transaction = $this->redis->multi();
foreach ($data as $key => $val) {
$transaction->set($key, $val, $ttl);
}
$res = $transaction->exec();
foreach($res as $key => $value) {
if($value === false) {
return false;
}
}
return true;
}
public function deleteMultiple($keys)
{
$transaction = $this->redis->multi();
foreach($keys as $key) {
$transaction->del($key);
}
$transaction->exec();
}
public function increment($key, $step = 1)
{
$this->redis->incrBy($key, $step);
}
public function decrement($key, $step = 1)
{
$this->redis->decrBy($key, $step);
}
}