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