Completed
Push — master ( b16e40...058ee0 )
by Marco
07:56
created

Memcached::ping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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 && $this->ping();
45
46
    }
47
48 86
    public function ping() {
49
50
        // try to read a fake value from cache and check it's return code
51 86
        $instance = $this->getInstance();
52 86
        $instance->get('cache-internals');
53 86
        $code = $instance->getResultCode();
54
55
        // check if code represents a failure
56
        // $code != [MEMCACHED_SUCCESS, MEMCACHED_NOTFOUND]
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57 86
        return in_array($code, [0, 16]);
58
        
59
    }
60
61 34
    public function get($key, $namespace) {
62
63 34
        $scope = $this->getNamespaceKey($namespace);
64
65 34
        if ( $scope === false ) return null;
66
67 26
        $shadowName = "$scope-$key";
68
69 26
        $instance = $this->getInstance();
70
71 26
        $item = $instance->get($shadowName);
72
73 26
        return $instance->getResultCode() == MemcachedInstance::RES_NOTFOUND ? null : $item;
74
75
    }
76
77 52
    public function set($key, $namespace, $value, $ttl = null) {
78
79 52
        if ( $ttl == null ) $ttl = 0;
80
81 52
        $scope = $this->getNamespaceKey($namespace);
82
83 52
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
84
85 52
        if ( $scope === false ) return false;
86
87 52
        $shadowName = "$scope-$key";
88
89 52
        return $this->getInstance()->set($shadowName, $value, $ttl);
90
91
    }
92
93 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...
94
95 8
        $scope = $this->getNamespaceKey($namespace);
96
97 8
        if ( $scope === false ) return false;
98
99 8
        $shadowName = "$scope-$key";
100
101 8
        return $this->getInstance()->delete($shadowName);
102
103
    }
104
105 22
    public function clear($namespace = null) {
106
107 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...
108
109 18
            return $this->getInstance()->flush();
110
111
        } else {
112
113 4
            $scope = $this->getNamespaceKey($namespace);
114
115 4
            if ( $scope === false ) return false;
116
117 4
            return $this->getInstance()->delete($namespace);
118
119
        }
120
121
    }
122
123 2
    public function getMultiple(array $keys, $namespace) {
124
125 2
        if ( empty($keys) ) return [];
126
127 2
        $keypad = array_combine($keys, array_fill(0, count($keys), null));
128
129 2
        $scope = $this->getNamespaceKey($namespace);
130
131 2
        if ( $scope === false ) return $keypad;
132
133
        $keyscope = array_map(function($key) use($scope) {
134 2
            return "$scope-$key";
135 2
        }, $keys);
136
137 2
        if ( version_compare(phpversion(), '7.0.0', '<') ) {
138
            $data = $this->getInstance()->getMulti($keyscope, $null = null, MemcachedInstance::GET_PRESERVE_ORDER);
139
        } else {
140 2
            $data = $this->getInstance()->getMulti($keyscope, MemcachedInstance::GET_PRESERVE_ORDER);
141
        }
142
143 2
        $return = [];
144
145 2
        foreach ( $data as $scoped_key => $value ) {
146 2
            $key = substr($scoped_key, strlen("$scope-"));
147 2
            $return[$key] = $value;
148
        }
149
150 2
        return array_replace($keypad, $return);
151
152
    }
153
154 2
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
155
156 2
        if ( $ttl == null ) $ttl = 0;
157
158 2
        $scope = $this->getNamespaceKey($namespace);
159
160 2
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
161
162 2
        if ( $scope === false ) return false;
163
164 2
        $shadowNames = [];
165
166 2
        foreach ( $key_values as $key => $value ) {
167 2
            $shadowNames["$scope-$key"] = $value;
168
        }
169
170 2
        return $this->getInstance()->setMulti($shadowNames, $ttl);
171
172
    }
173
174 4
    public function deleteMultiple(array $keys, $namespace) {
175
176 4
        $scope = $this->getNamespaceKey($namespace);
177
178 4
        if ( $scope === false ) return false;
179
180 4
        $shadowNames = array_map(function($key) use($scope) {
181 4
            return "$scope-$key";
182 4
        }, $keys);
183
184 4
        $delete = $this->getInstance()->deleteMulti($shadowNames);
185
186 4
        return count(array_diff(array_unique($delete), [true])) === 0;
187
188
    }
189
190 5
    public function has($key, $namespace) {
191
192 5
        return $this->get($key, $namespace) === null ? false : true;
193
194
    }
195
196 4
    public function stats() {
197
198 4
        return $this->getInstance()->getStats();
199
200
    }
201
202
    /**
203
     * Set namespace key
204
     *
205
     * @return  mixed
206
     */
207 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...
208
209 9
        $uId = UniqueId::get();
210
211 9
        return $this->getInstance()->set($namespace, $uId, 0) === false ? false : $uId;
212
213
    }
214
215
    /**
216
     * Get namespace key
217
     *
218
     * @return  string
219
     */
220 63
    private function getNamespaceKey($namespace) {
221
222 63
        return $this->getInstance()->get($namespace);
223
224
    }
225
226
}
227