1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* For the full copyright and license information, please view the LICENSE |
4
|
|
|
* file that was distributed with this source code. |
5
|
|
|
* |
6
|
|
|
* @author Igor Borodikhin <[email protected]> |
7
|
|
|
* @author Nikita Vershinin <[email protected]> |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
namespace Endeveit\Cache\Drivers; |
11
|
|
|
|
12
|
|
|
use Endeveit\Cache\Exception; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Driver almost the same as Memcache, but uses \Memcached instead of \Memcache. |
16
|
|
|
*/ |
17
|
|
|
class Memcached extends Memcache |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
* |
22
|
|
|
* Additional options: |
23
|
|
|
* "client" => the instance of \Memcached object |
24
|
|
|
* |
25
|
|
|
* @codeCoverageIgnore |
26
|
|
|
* @param array $options |
27
|
|
|
* @throws \Endeveit\Cache\Exception |
28
|
|
|
*/ |
29
|
|
|
public function __construct(array $options = array()) |
30
|
|
|
{ |
31
|
|
|
if (!array_key_exists('client', $options)) { |
32
|
|
|
throw new Exception('You must provide option "client" with \Memcached object'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
parent::__construct($options); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
* |
41
|
|
|
* @param string $id |
42
|
|
|
* @return mixed|false |
43
|
|
|
*/ |
44
|
|
|
protected function doLoad($id) |
45
|
|
|
{ |
46
|
|
|
return $this->getProcessedLoadedValue($this->getOption('client')->get($id)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
* |
52
|
|
|
* @param array $identifiers |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
protected function doLoadMany(array $identifiers) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$result = array(); |
58
|
|
|
$now = time(); |
59
|
|
|
|
60
|
|
|
foreach ($this->getOption('client')->getMulti($identifiers) as $id => $source) { |
61
|
|
|
$source = $this->getProcessedLoadedValue($source); |
62
|
|
|
|
63
|
|
|
if (false !== $source) { |
64
|
|
|
$i = $this->getIdentifierWithoutPrefix($id); |
65
|
|
|
|
66
|
|
|
if (array_key_exists('expiresAt', $source) && ($source['expiresAt'] < $now)) { |
67
|
|
|
$result[$i] = false; |
68
|
|
|
} else { |
69
|
|
|
$result[$i] = $source['data']; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->fillNotFoundKeys($result, $identifiers); |
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
* |
82
|
|
|
* @param string $id |
83
|
|
|
* @return mixed|false |
84
|
|
|
*/ |
85
|
|
|
protected function doLoadRaw($id) |
86
|
|
|
{ |
87
|
|
|
return $this->getOption('client')->get($id); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
* |
93
|
|
|
* @param mixed $data |
94
|
|
|
* @param string $id |
95
|
|
|
* @param array $tags |
96
|
|
|
* @return boolean |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
protected function doSave($data, $id, array $tags = array()) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$this->validateIdentifier($id); |
101
|
|
|
|
102
|
|
|
if (!empty($tags)) { |
103
|
|
|
$this->saveTagsForId($id, $tags); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->getOption('client')->set($id, $data); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
* |
112
|
|
|
* @param mixed $data |
113
|
|
|
* @param string $id |
114
|
|
|
* @param integer|boolean $lifetime |
115
|
|
|
* @return boolean |
116
|
|
|
*/ |
117
|
|
|
protected function doSaveScalar($data, $id, $lifetime = false) |
118
|
|
|
{ |
119
|
|
|
return $this->getOption('client')->set($id, $data, $lifetime); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.