Completed
Push — 2.0 ( a51c1d...cacff6 )
by Marco
04:49
created

Memcached::test()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Comodojo\Cache\Drivers;
2
3
use \Comodojo\Cache\Traits\InstanceTrait;
4
use \Comodojo\Cache\Components\UniqueId;
5
use \Memcached as MemcachedInstance;
6
use \Exception;
7
8
/**
9
 * memcached provider
10
 *
11
 * @package     Comodojo Spare Parts
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     MIT
14
 *
15
 * LICENSE:
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
class Memcached extends AbstractDriver {
27
28
    use InstanceTrait;
29
30
    const DRIVER_NAME = "memcached";
31
32 86
    public function __construct(array $configuration = []) {
33
34 86
        if ( class_exists('Memcached') === false ) throw new Exception("ext-memcached not available");
35
36 86
        $this->setInstance(new MemcachedInstance($configuration['persistent_id']));
37 86
        $this->getInstance()
38 86
            ->addServer($configuration['server'], $configuration['port'], $configuration['weight']);
39
40 86
    }
41
42 86
    public function test() {
43
44 86
        return sizeof($this->getInstance()->getServerList()) > 0;
45
46
    }
47
48 35
    public function get($key, $namespace) {
49
50 35
        $scope = $this->getNamespaceKey($namespace);
51
52 35
        if ( $scope === false ) return null;
53
54 27
        $shadowName = "$scope-$key";
55
56 27
        $instance = $this->getInstance();
57
58 27
        $item = $instance->get($shadowName);
59
60 27
        return $instance->getResultCode() == MemcachedInstance::RES_NOTFOUND ? null : $item;
61
62
    }
63
64 52
    public function set($key, $namespace, $value, $ttl = null) {
65
66 52
        if ( $ttl == null ) $ttl = 0;
67
68 52
        $scope = $this->getNamespaceKey($namespace);
69
70 52
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
71
72 52
        if ( $scope === false ) return false;
73
74 52
        $shadowName = "$scope-$key";
75
76 52
        return $this->getInstance()->set($shadowName, $value, $ttl);
77
78
    }
79
80 8 View Code Duplication
    public function delete($key, $namespace) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
81
82 8
        $scope = $this->getNamespaceKey($namespace);
83
84 8
        if ( $scope === false ) return false;
85
86 8
        $shadowName = "$scope-$key";
87
88 8
        return $this->getInstance()->delete($shadowName);
89
90
    }
91
92 22
    public function clear($namespace = null) {
93
94 22 View Code Duplication
        if ( $namespace == null ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
95
96 18
            return $this->getInstance()->flush();
97
98
        } else {
99
100 4
            $scope = $this->getNamespaceKey($namespace);
101
102 4
            if ( $scope === false ) return false;
103
104 4
            return $this->getInstance()->delete($namespace);
105
106
        }
107
108
    }
109
110 2
    public function getMultiple(array $keys, $namespace) {
111
112 2
        if ( empty($keys) ) return [];
113
114 2
        $keypad = array_combine($keys, array_fill(0, count($keys), null));
115
116 2
        $scope = $this->getNamespaceKey($namespace);
117
118 2
        if ( $scope === false ) return $keypad;
119
120
        $keyscope = array_map(function($key) use($scope) {
121 2
            return "$scope-$key";
122 2
        }, $keys);
123
124 2
        if (version_compare(phpversion(), '7.0.0', '<')) {
125
            $data = $this->getInstance()->getMulti($keyscope, $null = null, MemcachedInstance::GET_PRESERVE_ORDER);
126
        } else {
127 2
            $data = $this->getInstance()->getMulti($keyscope, MemcachedInstance::GET_PRESERVE_ORDER);
128
        }
129
130 2
        $return = [];
131
132 2
        foreach ($data as $scoped_key => $value) {
133 2
            $key = substr($scoped_key, strlen("$scope-"));
134 2
            $return[$key] = $value;
135
        }
136
137 2
        return array_replace($keypad, $return);
138
139
    }
140
141 2
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
142
143 2
        if ( $ttl == null ) $ttl = 0;
144
145 2
        $scope = $this->getNamespaceKey($namespace);
146
147 2
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
148
149 2
        if ( $scope === false ) return false;
150
151 2
        $shadowNames = [];
152
153 2
        foreach ($key_values as $key => $value) {
154 2
            $shadowNames["$scope-$key"] = $value;
155
        }
156
157 2
        return $this->getInstance()->setMulti($shadowNames, $ttl);
158
159
    }
160
161 4
    public function deleteMultiple(array $keys, $namespace) {
162
163 4
        $scope = $this->getNamespaceKey($namespace);
164
165 4
        if ( $scope === false ) return false;
166
167 4
        $shadowNames = array_map(function($key) use($scope) {
168 4
            return "$scope-$key";
169 4
        }, $keys);
170
171 4
        $delete = $this->getInstance()->deleteMulti($shadowNames);
172
173 4
        return count(array_diff(array_unique($delete), [true])) === 0;
174
175
    }
176
177 5
    public function has($key, $namespace) {
178
179 5
        return $this->get($key, $namespace) === null ? false : true;
180
181
    }
182
183 4
    public function stats() {
184
185 4
        return $this->getInstance()->getStats();
186
187
    }
188
189
    /**
190
     * Set namespace key
191
     *
192
     * @return  mixed
193
     */
194 9 View Code Duplication
    private function setNamespaceKey($namespace) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
195
196 9
        $uId = UniqueId::get();
197
198 9
        return $this->getInstance()->set($namespace, $uId, 0) === false ? false : $uId;
199
200
    }
201
202
    /**
203
     * Get namespace key
204
     *
205
     * @return  string
206
     */
207 63
    private function getNamespaceKey($namespace) {
208
209 63
        return $this->getInstance()->get($namespace);
210
211
    }
212
213
}
214