Completed
Push — 2.0 ( 07e083...8b0753 )
by Marco
03:10 queued 01:10
created

Apcu::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 12
1
<?php namespace Comodojo\Cache\Drivers;
2
3
use \Comodojo\Foundation\Utils\UniqueId;
4
use \Exception;
5
6
/**
7
 * @package     Comodojo Cache
8
 * @author      Marco Giovinazzi <[email protected]>
9
 * @license     MIT
10
 *
11
 * LICENSE:
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
 * THE SOFTWARE.
20
 */
21
22 View Code Duplication
class Apcu extends AbstractDriver {
0 ignored issues
show
Duplication introduced by
This class 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...
23
24
    const DRIVER_NAME = "apcu";
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 45
    public function __construct(array $configuration = []) {
30
31 45
        if ( extension_loaded('apcu') === false ) throw new Exception("ext-apcu not available");
32
33
        // In cli, apcu SHOULD NOT use the request time for cache retrieve/invalidation.
34
        // This is because in cli the request time is allways the same.
35
        if ( php_sapi_name() === 'cli' ) {
36
            ini_set('apc.use_request_time', 0);
37
        }
38
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function test() {
45
46
        return (bool) ini_get('apc.enabled');
47
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function get($key, $namespace) {
54
55
        $scope = $this->getNamespaceKey($namespace);
56
57
        if ( $scope === false ) return null;
58
59
        $shadowName = "$scope-$key";
60
61
        $item = apcu_fetch($shadowName, $success);
62
63
        return $success === false ? null : $item;
64
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function set($key, $namespace, $value, $ttl = null) {
71
72
        if ( $ttl == null ) $ttl = 0;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $ttl of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
73
74
        $scope = $this->getNamespaceKey($namespace);
75
76
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
77
78
        if ( $scope === false ) return false;
79
80
        $shadowName = "$scope-$key";
81
82
        return apcu_store($shadowName, $value, $ttl);
83
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function delete($key, $namespace) {
90
91
        $scope = $this->getNamespaceKey($namespace);
92
93
        if ( $scope === false ) return false;
94
95
        $shadowName = "$scope-$key";
96
97
        return apcu_delete($shadowName);
98
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function clear($namespace = null) {
105
106
        if ( $namespace == null ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $namespace of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
107
108
            return apcu_clear_cache();
109
110
        } else {
111
112
            $scope = $this->getNamespaceKey($namespace);
113
114
            if ( $scope === false ) return false;
115
116
            return apcu_delete($namespace);
117
118
        }
119
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getMultiple(array $keys, $namespace) {
126
127
        $keypad = array_combine($keys, array_fill(0, count($keys), null));
128
129
        $scope = $this->getNamespaceKey($namespace);
130
131
        if ( $scope === false ) return $keypad;
132
133
        $keyscope = array_map(function($key) use($scope) {
134
            return "$scope-$key";
135
        }, $keys);
136
137
        $data = apcu_fetch($keyscope, $success);
138
139
        $return = [];
140
141
        foreach ( $data as $scoped_key => $value ) {
142
            $key = substr($scoped_key, strlen("$scope-"));
143
            $return[$key] = $value;
144
        }
145
146
        return array_replace($keypad, $return);
147
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
154
155
        if ( $ttl == null ) $ttl = 0;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $ttl of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
156
157
        $scope = $this->getNamespaceKey($namespace);
158
159
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
160
161
        if ( $scope === false ) return false;
162
163
        $shadowNames = [];
164
165
        foreach ( $key_values as $key => $value ) {
166
            $shadowNames["$scope-$key"] = $value;
167
        }
168
169
        $data = apcu_store($shadowNames, null, $ttl);
170
171
        return empty($data) ? true : false;
172
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function deleteMultiple(array $keys, $namespace) {
179
180
        $scope = $this->getNamespaceKey($namespace);
181
182
        if ( $scope === false ) return false;
183
184
        $shadowNames = array_map(function($key) use($scope) {
185
            return "$scope-$key";
186
        }, $keys);
187
188
        $delete = apcu_delete($shadowNames);
189
190
        return empty($delete) ? true : false;
191
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function has($key, $namespace) {
198
199
        $scope = $this->getNamespaceKey($namespace);
200
201
        if ( $scope === false ) return false;
202
203
        return apcu_exists("$scope-$key");
204
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function stats() {
211
212
        return apcu_cache_info(true);
213
214
    }
215
216
    /**
217
     * Set namespace key
218
     *
219
     * @return  mixed
220
     */
221
    private function setNamespaceKey($namespace) {
222
223
        $uId = UniqueId::generate(64);
224
225
        return apcu_store($namespace, $uId, 0) === false ? false : $uId;
226
227
    }
228
229
    /**
230
     * Get namespace key
231
     *
232
     * @return  string
233
     */
234
    private function getNamespaceKey($namespace) {
235
236
        return apcu_fetch($namespace);
237
238
    }
239
240
}
241