|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Cache\Redis |
|
5
|
|
|
* |
|
6
|
|
|
* Core\Cache Redis Driver. |
|
7
|
|
|
* |
|
8
|
|
|
* @package core |
|
9
|
|
|
* @author [email protected] |
|
10
|
|
|
* @author [email protected] |
|
11
|
|
|
* @version 1.0.1 |
|
12
|
|
|
* @copyright Caffeina srl - 2014-2016 - http://caffeina.com |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
namespace Cache; |
|
17
|
|
|
|
|
18
|
|
|
class Redis implements Adapter { |
|
19
|
|
|
|
|
20
|
|
|
protected $redis = null; |
|
21
|
|
|
protected $options = [ |
|
22
|
|
|
'scheme' => 'tcp', |
|
23
|
|
|
'host' => '127.0.0.1', |
|
24
|
|
|
'port' => 6379, |
|
25
|
|
|
'timeout' => 1, |
|
26
|
|
|
'reconnect' => 100, |
|
27
|
|
|
'prefix' => '', |
|
28
|
|
|
'serialize' => true, |
|
29
|
|
|
'database' => 0, |
|
30
|
|
|
'exceptions' => true, |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
public static function valid(){ |
|
34
|
|
|
return true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function instance(){ |
|
38
|
|
|
return $redis; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function __construct($opt=[]){ |
|
42
|
|
|
/** |
|
43
|
|
|
* Predis Docs: |
|
44
|
|
|
* https://github.com/nrk/predis |
|
45
|
|
|
*/ |
|
46
|
|
|
$this->options = array_merge($this->options, $opt); |
|
47
|
|
|
$this->redis = new \Predis\Client($this->options['scheme'].'://'.$this->options['host'].':'.$this->options['port'].'/', [ |
|
48
|
|
|
'prefix' => 'core:'.$this->options['prefix'], |
|
49
|
|
|
'exceptions' => $this->options['exceptions'], |
|
50
|
|
|
'connection_timeout' => $this->options['timeout'], |
|
51
|
|
|
'database' => $this->options['database'], |
|
52
|
|
|
]); |
|
53
|
|
|
$this->redis->select($this->options['database']); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function get($key){ |
|
57
|
|
|
return unserialize($this->redis->get($key)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function set($key,$value,$expire=0){ |
|
61
|
|
|
$expire = (int)$expire; |
|
62
|
|
|
return $expire > 0 ? $this->redis->setex($key,$expire,serialize($value)) : $this->redis->set($key,serialize($value)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function delete($key){ |
|
66
|
|
|
$this->redis->del($key); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function exists($key){ |
|
70
|
|
|
return $this->redis->exists($key); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function flush(){ |
|
74
|
|
|
return $this->redis->flushdb(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function inc($key,$value=1){ |
|
78
|
|
|
return $this->redis->incrby($key,$value); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function dec($key,$value=1){ |
|
82
|
|
|
return $this->redis->decrby($key,$value); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.