Code Duplication    Length = 186-186 lines in 2 locations

src/Comodojo/Cache/Drivers/Apc.php 1 location

@@ 22-207 (lines=186) @@
19
 * THE SOFTWARE.
20
 */
21
22
class Apc extends AbstractDriver {
23
24
    const DRIVER_NAME = "apc";
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function __construct(array $configuration = []) {
30
31
        if ( extension_loaded('apc') === false ) throw new Exception("ext-apc 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 = apc_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;
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 apc_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 apc_delete($shadowName);
98
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function clear($namespace = null) {
105
106
        if ( $namespace == null ) {
107
108
            return apc_clear_cache("user");
109
110
        } else {
111
112
            $scope = $this->getNamespaceKey($namespace);
113
114
            if ( $scope === false ) return false;
115
116
            return apc_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 = apc_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;
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 = apc_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 = apc_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 apc_exists("$scope-$key");
204
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function stats() {

src/Comodojo/Cache/Drivers/Apcu.php 1 location

@@ 22-207 (lines=186) @@
19
 * THE SOFTWARE.
20
 */
21
22
class Apcu extends AbstractDriver {
23
24
    const DRIVER_NAME = "apcu";
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function __construct(array $configuration = []) {
30
31
        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;
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 ) {
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;
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() {