1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate\Cache; |
6
|
|
|
|
7
|
|
|
use Suricate; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Memcache extension for Suricate |
11
|
|
|
* |
12
|
|
|
* @package Suricate |
13
|
|
|
* @author Mathieu LESNIAK <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @property string $host Memcache host (default: localhost) |
16
|
|
|
* @property int $port Memcache port (default: 11211) |
17
|
|
|
* @property int $defaultExpiry Key default expiry |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
class Memcached extends Suricate\Cache |
21
|
|
|
{ |
22
|
|
|
protected $parametersList = ['host', 'port', 'defaultExpiry']; |
23
|
|
|
|
24
|
|
|
private $handler; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
parent::__construct(); |
29
|
|
|
|
30
|
|
|
$this->handler = false; |
31
|
|
|
$this->host = 'localhost'; |
32
|
|
|
$this->port = 11211; |
33
|
|
|
$this->defaultExpiry = 3600; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getHost() |
37
|
|
|
{ |
38
|
|
|
return $this->host; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setHost($host) |
42
|
|
|
{ |
43
|
|
|
$this->host = $host; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getPort() |
49
|
|
|
{ |
50
|
|
|
return $this->port; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setPort($port) |
54
|
|
|
{ |
55
|
|
|
$this->port = $port; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getDefaultExpiry() |
61
|
|
|
{ |
62
|
|
|
return $this->defaultExpiry; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setDefaultExpiry($expiry) |
66
|
|
|
{ |
67
|
|
|
$this->defaultExpiry = $expiry; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function connect() |
73
|
|
|
{ |
74
|
|
|
if ($this->handler === false) { |
75
|
|
|
if (class_exists('Memcached')) { |
76
|
|
|
try { |
77
|
|
|
$this->handler = new \Memcached(); |
78
|
|
|
$this->handler->addServer($this->host, $this->port); |
79
|
|
|
} catch (\Exception $e) { |
80
|
|
|
throw new \Exception('Can\'t connect to memcache server'); |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
|
|
throw new \BadMethodCallException( |
84
|
|
|
'Can\'t find Memcached extension' |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Put a value into memcache |
94
|
|
|
* @param string $variable Variable name |
95
|
|
|
* @param mixed $value Value |
96
|
|
|
* @param int $expiry Cache expiry |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function set(string $variable, $value, $expiry = null) |
101
|
|
|
{ |
102
|
|
|
$this->connect(); |
103
|
|
|
|
104
|
|
|
if ($expiry === null) { |
105
|
|
|
$expiry = $this->defaultExpiry; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->handler->set($variable, $value, $expiry); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function get(string $variable) |
112
|
|
|
{ |
113
|
|
|
$this->connect(); |
114
|
|
|
return $this->handler->get($variable); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Delete a variable from memcache |
119
|
|
|
* |
120
|
|
|
* @param string $variable |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function delete(string $variable) |
125
|
|
|
{ |
126
|
|
|
$this->connect(); |
127
|
|
|
return $this->handler->delete($variable); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|