Completed
Push — master ( b9da65...b16e40 )
by Marco
04:58
created

Apc::stats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 5
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 5
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\Components\UniqueId;
4
use \Exception;
5
6
/**
7
 * Apcu provider
8
 *
9
 * @package     Comodojo Spare Parts
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24 View Code Duplication
class Apc 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...
25
26
    const DRIVER_NAME = "apc";
27
28 86
    public function __construct(array $configuration = []) {
29
30 86
        if ( extension_loaded('apc') === false ) throw new Exception("ext-apc not available");
31
32
        // In cli, apcu SHOULD NOT use the request time for cache retrieve/invalidation.
33
        // This is because in cli the request time is allways the same.
34 86
        if ( php_sapi_name() === 'cli' ) {
35 86
            ini_set('apc.use_request_time', 0);
36 86
        }
37
38 86
    }
39
40 86
    public function test() {
41
42 86
        return (bool) ini_get('apc.enabled');
43
44
    }
45
46 67
    public function get($key, $namespace) {
47
48 67
        $scope = $this->getNamespaceKey($namespace);
49
50 67
        if ( $scope === false ) return null;
51
52 50
        $shadowName = "$scope-$key";
53
54 50
        $item = apc_fetch($shadowName, $success);
55
56 50
        return $success === false ? null : $item;
57
58
    }
59
60 52
    public function set($key, $namespace, $value, $ttl = null) {
61
62 52
        if ( $ttl == null ) $ttl = 0;
63
64 52
        $scope = $this->getNamespaceKey($namespace);
65
66 52
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
67
68 52
        if ( $scope === false ) return false;
69
70 52
        $shadowName = "$scope-$key";
71
72 52
        return apc_store($shadowName, $value, $ttl);
73
74
    }
75
76 8
    public function delete($key, $namespace) {
77
78 8
        $scope = $this->getNamespaceKey($namespace);
79
80 8
        if ( $scope === false ) return false;
81
82 8
        $shadowName = "$scope-$key";
83
84 8
        return apc_delete($shadowName);
85
86
    }
87
88 22
    public function clear($namespace = null) {
89
90 22
        if ( $namespace == null ) {
91
92 18
            return apc_clear_cache("user");
93
94
        } else {
95
96 4
            $scope = $this->getNamespaceKey($namespace);
97
98 4
            if ( $scope === false ) return false;
99
100 4
            return apc_delete($namespace);
101
102
        }
103
104
    }
105
106 3
    public function getMultiple(array $keys, $namespace) {
107
108 3
        $keypad = array_combine($keys, array_fill(0, count($keys), null));
109
110 3
        $scope = $this->getNamespaceKey($namespace);
111
112 3
        if ( $scope === false ) return $keypad;
113
114
        $keyscope = array_map(function($key) use($scope) {
115 3
            return "$scope-$key";
116 3
        }, $keys);
117
118 3
        $data = apc_fetch($keyscope, $success);
119
120 3
        $return = [];
121
122 3
        foreach ( $data as $scoped_key => $value ) {
123 3
            $key = substr($scoped_key, strlen("$scope-"));
124 3
            $return[$key] = $value;
125 3
        }
126
127 3
        return array_replace($keypad, $return);
128
129
    }
130
131 2
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
132
133 2
        if ( $ttl == null ) $ttl = 0;
134
135 2
        $scope = $this->getNamespaceKey($namespace);
136
137 2
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
138
139 2
        if ( $scope === false ) return false;
140
141 2
        $shadowNames = [];
142
143 2
        foreach ( $key_values as $key => $value ) {
144 2
            $shadowNames["$scope-$key"] = $value;
145 2
        }
146
147 2
        $data = apc_store($shadowNames, null, $ttl);
148
149 2
        return empty($data) ? true : false;
150
151
    }
152
153 4
    public function deleteMultiple(array $keys, $namespace) {
154
155 4
        $scope = $this->getNamespaceKey($namespace);
156
157 4
        if ( $scope === false ) return false;
158
159 4
        $shadowNames = array_map(function($key) use($scope) {
160 4
            return "$scope-$key";
161 4
        }, $keys);
162
163 4
        $delete = apc_delete($shadowNames);
164
165 4
        return empty($delete) ? true : false;
166
167
    }
168
169 10
    public function has($key, $namespace) {
170
171 10
        $scope = $this->getNamespaceKey($namespace);
172
173 10
        if ( $scope === false ) return false;
174
175 9
        return apc_exists("$scope-$key");
176
177
    }
178
179 4
    public function stats() {
180
181 4
        return apc_cache_info("user", true);
182
183
    }
184
185
    /**
186
     * Set namespace key
187
     *
188
     * @return  mixed
189
     */
190 9
    private function setNamespaceKey($namespace) {
191
192 9
        $uId = UniqueId::get();
193
194 9
        return apc_store($namespace, $uId, 0) === false ? false : $uId;
195
196
    }
197
198
    /**
199
     * Get namespace key
200
     *
201
     * @return  string
202
     */
203 72
    private function getNamespaceKey($namespace) {
204
205 72
        return apc_fetch($namespace);
206
207
    }
208
209
}
210