|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Spiral\Cache\Stores\Memcache; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Two sisters. |
|
13
|
|
|
*/ |
|
14
|
|
|
class MemcachedDriver extends AbstractDriver |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Memcached |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $driver = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(array $servers) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->servers = $servers; |
|
27
|
|
|
$this->driver = new \Memcached(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
View Code Duplication |
public function connect() |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
foreach ($this->servers as $server) { |
|
36
|
|
|
|
|
37
|
|
|
//Merging default options |
|
38
|
|
|
$server = $server + $this->defaultServer; |
|
39
|
|
|
|
|
40
|
|
|
$this->driver->addServer( |
|
41
|
|
|
$server['host'], |
|
42
|
|
|
$server['port'], |
|
43
|
|
|
$server['weight'] |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function has(string $name): bool |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->driver->get($name) === false) { |
|
54
|
|
|
return $this->driver->getResultCode() != \Memcached::RES_NOTFOUND; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function get(string $name) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->driver->get($name); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \MemcachedException |
|
72
|
|
|
*/ |
|
73
|
|
|
public function set(string $name, $data, $ttl = null) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->driver->set($name, $data, $this->lifetime($ttl)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function delete(string $name) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->driver->delete($name); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
|
View Code Duplication |
public function inc(string $name, int $delta = 1): int |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
if (!$this->has($name)) { |
|
92
|
|
|
$this->set($name, $delta); |
|
93
|
|
|
|
|
94
|
|
|
return $delta; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->driver->increment($name, $delta); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function dec(string $name, int $delta = 1): int |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->driver->decrement($name, $delta); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function clear() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->driver->flush(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.