1
|
|
|
<?php |
2
|
|
|
namespace DBAL\Caching; |
3
|
|
|
|
4
|
|
|
use Redis; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @codeCoverageIgnore |
8
|
|
|
*/ |
9
|
|
|
class RedisCache implements CacheInterface |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected $cache; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* constructor |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->cache = new Redis(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* destructor closes the connection |
24
|
|
|
*/ |
25
|
|
|
public function __destruct() |
26
|
|
|
{ |
27
|
|
|
if (is_object($this->cache)) { |
28
|
|
|
$this->cache->close(); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Connect to a server |
34
|
|
|
* @param string $host This should be the host name or IP address you want to connect to |
35
|
|
|
* @param int $port The port number where Memcache can be accessed |
36
|
|
|
* @param boolean $persistent If you want this connection to be persistent set to true else set to false |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function connect($host, $port, $persistent = false) |
40
|
|
|
{ |
41
|
|
|
$this->addServer($host, $port, $persistent); |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add a server to connection pool |
47
|
|
|
* @param string $host This should be the host name or IP address you want to add to the Memcache pool |
48
|
|
|
* @param int $port The port number where Memcache can be accessed |
49
|
|
|
* @param boolean $persistent If you want this connection to be persistent set to true else set to false |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function addServer($host, $port, $persistent = false) |
53
|
|
|
{ |
54
|
|
|
if ($persistent === false) { |
55
|
|
|
$this->cache->connect($host, intval($port)); |
56
|
|
|
} else { |
57
|
|
|
$this->cache->pconnect($host, intval($port)); |
58
|
|
|
} |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Adds a value to be stored on the server |
65
|
|
|
* @param string $key This should be the key for the value you wish to add |
66
|
|
|
* @param mixed $value The value you wish to be stored with the given key |
67
|
|
|
* @param int $time How long should the value be stored for in seconds (0 = never expire) (max set value = 2592000 (30 Days)) |
68
|
|
|
* @return boolean Returns true if successfully added or false on failure |
69
|
|
|
*/ |
70
|
|
|
public function save($key, $value, $time = 0) |
71
|
|
|
{ |
72
|
|
|
return $this->cache->set($key, $value, intval($time)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Replaces a stored value for a given key |
78
|
|
|
* @param string $key This should be the key for the value you wish to replace |
79
|
|
|
* @param mixed $value The new value that you wish to give to that key |
80
|
|
|
* @param int $time How long should the value be stored for in seconds (0 = never expire) (max set value = 2592000 (30 Days)) |
81
|
|
|
* @return boolean Returns true if successfully replaced or false on failure |
82
|
|
|
*/ |
83
|
|
|
public function replace($key, $value, $time = 0) |
84
|
|
|
{ |
85
|
|
|
return $this->save($key, $value, $time); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the values store for the given key |
90
|
|
|
* @param string $key This should be the unique query key to get the value |
91
|
|
|
* @return mixed The store value will be returned |
92
|
|
|
*/ |
93
|
|
|
public function fetch($key) |
94
|
|
|
{ |
95
|
|
|
return $this->cache->get($key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Deletes a single value from the server based on the given key |
100
|
|
|
* @param string $key This should be the key that you wish to delete the value for |
101
|
|
|
* @return boolean Returns true on success or false on failure |
102
|
|
|
*/ |
103
|
|
|
public function delete($key) |
104
|
|
|
{ |
105
|
|
|
return (bool)$this->cache->delete($key); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Deletes all values from the server |
110
|
|
|
* @return boolean Returns true on success or false on failure |
111
|
|
|
*/ |
112
|
|
|
public function deleteAll() |
113
|
|
|
{ |
114
|
|
|
return (bool)$this->cache->flushAll(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|