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