|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Adapter for Memcached |
|
4
|
|
|
* |
|
5
|
|
|
* @file MemcachedAdapter.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 5.6+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2016 Alexander Yancharuk |
|
11
|
|
|
* @date 8/21/13 18:42 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\Cache\Adapters; |
|
17
|
|
|
|
|
18
|
|
|
use Exception; |
|
19
|
|
|
use Memcached; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class MemcachedAdapter |
|
23
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
24
|
|
|
*/ |
|
25
|
|
View Code Duplication |
class MemcachedAdapter extends CacheAdapterAbstract implements CacheAdapterInterface |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
/** @var null|array */ |
|
28
|
|
|
protected static $calls; |
|
29
|
|
|
/** @var CacheAdapterInterface */ |
|
30
|
|
|
protected static $instance; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create Memcached class instance and connect to memcached pool |
|
34
|
|
|
*/ |
|
35
|
1 |
|
protected function __construct() |
|
36
|
|
|
{ |
|
37
|
1 |
|
$this->setDriver(new Memcached); |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get data |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $key Key |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function get($key) |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->getDriver()->get($key); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Save data |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $key Key |
|
55
|
|
|
* @param mixed $value Data |
|
56
|
|
|
* @param int $ttl Time to live |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function set($key, $value, $ttl) |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->getDriver()->set($key, $value, $ttl); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Check if data stored in cache |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $key Key |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function has($key) |
|
71
|
|
|
{ |
|
72
|
1 |
|
return (bool) $this->getDriver()->get($key); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Delete data |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $key Key |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function del($key) |
|
82
|
|
|
{ |
|
83
|
1 |
|
return $this->getDriver()->delete($key); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Method for deletion keys by template |
|
88
|
|
|
* |
|
89
|
|
|
* ATTENTION: if key contains spaces, for example 'THIS IS KEY::ID:50d98ld', |
|
90
|
|
|
* then in cache it will be saved as 'THIS_IS_KEY::ID:50d98ld'. So, template |
|
91
|
|
|
* for that key deletion must be look like - 'THIS_IS_KEY'. |
|
92
|
|
|
* Deletion can be made by substring, containing in keys. For example |
|
93
|
|
|
* '_KEY::ID'. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $tpl Substring containing in needed keys |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function delByTemplate($tpl) |
|
99
|
|
|
{ |
|
100
|
|
|
try { |
|
101
|
1 |
|
$cache = new MemcacheRaw(); |
|
102
|
1 |
|
$cache->delByTemplate($tpl)->disconnect(); |
|
103
|
1 |
|
return true; |
|
104
|
1 |
|
} catch (Exception $e) { |
|
105
|
1 |
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Cache cleanup |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function clear() |
|
115
|
|
|
{ |
|
116
|
1 |
|
return $this->getDriver()->flush(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Increment key value |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $key Key |
|
123
|
|
|
* @param int $offset Offset |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool|int |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public function increment($key, $offset) |
|
128
|
|
|
{ |
|
129
|
1 |
|
return $this->getDriver()->increment($key, $offset); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Decrement key value |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $key Key |
|
136
|
|
|
* @param int $offset Offset |
|
137
|
|
|
* |
|
138
|
|
|
* @return bool|int |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function decrement($key, $offset) |
|
141
|
|
|
{ |
|
142
|
1 |
|
return $this->getDriver()->decrement($key, $offset); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
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.