|
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 brothers. |
|
13
|
|
|
*/ |
|
14
|
|
|
class MemcacheDriver extends AbstractDriver |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Memcache |
|
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 \Memcache(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
View Code Duplication |
public function connect() |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
foreach ($this->servers as $server) { |
|
36
|
|
|
$server = $server + $this->defaultServer; |
|
37
|
|
|
|
|
38
|
|
|
$this->driver->addServer( |
|
39
|
|
|
$server['host'], |
|
40
|
|
|
$server['port'], |
|
41
|
|
|
$server['persistent'], |
|
42
|
|
|
$server['weight'] |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function has(string $name): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->driver->get($name) !== false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function get(string $name) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->driver->get($name); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \MemcachedException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function set(string $name, $data, $ttl = null) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->driver->set($name, $data, 0, $this->lifetime($ttl, 0)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function delete(string $name) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->driver->delete($name); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
View Code Duplication |
public function inc(string $name, int $delta = 1): int |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
if (!$this->has($name)) { |
|
87
|
|
|
$this->set($name, $delta); |
|
88
|
|
|
|
|
89
|
|
|
return $delta; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->driver->increment($name, $delta); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function dec(string $name, int $delta = 1): int |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->driver->decrement($name, $delta); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function clear() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->driver->flush(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
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.