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