Code Duplication    Length = 186-186 lines in 2 locations

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

@@ 24-209 (lines=186) @@
21
 * THE SOFTWARE.
22
 */
23
24
class Apc extends AbstractDriver {
25
26
    const DRIVER_NAME = "apc";
27
28
    public function __construct(array $configuration = []) {
29
30
        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
        if ( php_sapi_name() === 'cli' ) {
35
            ini_set('apc.use_request_time', 0);
36
        }
37
38
    }
39
40
    public function test() {
41
42
        return (bool) ini_get('apc.enabled');
43
44
    }
45
46
    public function get($key, $namespace) {
47
48
        $scope = $this->getNamespaceKey($namespace);
49
50
        if ( $scope === false ) return null;
51
52
        $shadowName = "$scope-$key";
53
54
        $item = apc_fetch($shadowName, $success);
55
56
        return $success === false ? null : $item;
57
58
    }
59
60
    public function set($key, $namespace, $value, $ttl = null) {
61
62
        if ( $ttl == null ) $ttl = 0;
63
64
        $scope = $this->getNamespaceKey($namespace);
65
66
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
67
68
        if ( $scope === false ) return false;
69
70
        $shadowName = "$scope-$key";
71
72
        return apc_store($shadowName, $value, $ttl);
73
74
    }
75
76
    public function delete($key, $namespace) {
77
78
        $scope = $this->getNamespaceKey($namespace);
79
80
        if ( $scope === false ) return false;
81
82
        $shadowName = "$scope-$key";
83
84
        return apc_delete($shadowName);
85
86
    }
87
88
    public function clear($namespace = null) {
89
90
        if ( $namespace == null ) {
91
92
            return apc_clear_cache("user");
93
94
        } else {
95
96
            $scope = $this->getNamespaceKey($namespace);
97
98
            if ( $scope === false ) return false;
99
100
            return apc_delete($namespace);
101
102
        }
103
104
    }
105
106
    public function getMultiple(array $keys, $namespace) {
107
108
        $keypad = array_combine($keys, array_fill(0, count($keys), null));
109
110
        $scope = $this->getNamespaceKey($namespace);
111
112
        if ( $scope === false ) return $keypad;
113
114
        $keyscope = array_map(function($key) use($scope) {
115
            return "$scope-$key";
116
        }, $keys);
117
118
        $data = apc_fetch($keyscope, $success);
119
120
        $return = [];
121
122
        foreach ( $data as $scoped_key => $value ) {
123
            $key = substr($scoped_key, strlen("$scope-"));
124
            $return[$key] = $value;
125
        }
126
127
        return array_replace($keypad, $return);
128
129
    }
130
131
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
132
133
        if ( $ttl == null ) $ttl = 0;
134
135
        $scope = $this->getNamespaceKey($namespace);
136
137
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
138
139
        if ( $scope === false ) return false;
140
141
        $shadowNames = [];
142
143
        foreach ( $key_values as $key => $value ) {
144
            $shadowNames["$scope-$key"] = $value;
145
        }
146
147
        $data = apc_store($shadowNames, null, $ttl);
148
149
        return empty($data) ? true : false;
150
151
    }
152
153
    public function deleteMultiple(array $keys, $namespace) {
154
155
        $scope = $this->getNamespaceKey($namespace);
156
157
        if ( $scope === false ) return false;
158
159
        $shadowNames = array_map(function($key) use($scope) {
160
            return "$scope-$key";
161
        }, $keys);
162
163
        $delete = apc_delete($shadowNames);
164
165
        return empty($delete) ? true : false;
166
167
    }
168
169
    public function has($key, $namespace) {
170
171
        $scope = $this->getNamespaceKey($namespace);
172
173
        if ( $scope === false ) return false;
174
175
        return apc_exists("$scope-$key");
176
177
    }
178
179
    public function stats() {
180
181
        return apc_cache_info("user", true);
182
183
    }
184
185
    /**
186
     * Set namespace key
187
     *
188
     * @return  mixed
189
     */
190
    private function setNamespaceKey($namespace) {
191
192
        $uId = UniqueId::get();
193
194
        return apc_store($namespace, $uId, 0) === false ? false : $uId;
195
196
    }
197
198
    /**
199
     * Get namespace key
200
     *
201
     * @return  string
202
     */
203
    private function getNamespaceKey($namespace) {
204
205
        return apc_fetch($namespace);
206
207
    }
208
209
}
210

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

@@ 24-209 (lines=186) @@
21
 * THE SOFTWARE.
22
 */
23
24
class Apcu extends AbstractDriver {
25
26
    const DRIVER_NAME = "apcu";
27
28
    public function __construct(array $configuration = []) {
29
30
        if ( extension_loaded('apcu') === false ) throw new Exception("ext-apcu 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
        if ( php_sapi_name() === 'cli' ) {
35
            ini_set('apc.use_request_time', 0);
36
        }
37
38
    }
39
40
    public function test() {
41
42
        return (bool) ini_get('apc.enabled');
43
44
    }
45
46
    public function get($key, $namespace) {
47
48
        $scope = $this->getNamespaceKey($namespace);
49
50
        if ( $scope === false ) return null;
51
52
        $shadowName = "$scope-$key";
53
54
        $item = apcu_fetch($shadowName, $success);
55
56
        return $success === false ? null : $item;
57
58
    }
59
60
    public function set($key, $namespace, $value, $ttl = null) {
61
62
        if ( $ttl == null ) $ttl = 0;
63
64
        $scope = $this->getNamespaceKey($namespace);
65
66
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
67
68
        if ( $scope === false ) return false;
69
70
        $shadowName = "$scope-$key";
71
72
        return apcu_store($shadowName, $value, $ttl);
73
74
    }
75
76
    public function delete($key, $namespace) {
77
78
        $scope = $this->getNamespaceKey($namespace);
79
80
        if ( $scope === false ) return false;
81
82
        $shadowName = "$scope-$key";
83
84
        return apcu_delete($shadowName);
85
86
    }
87
88
    public function clear($namespace = null) {
89
90
        if ( $namespace == null ) {
91
92
            return apcu_clear_cache();
93
94
        } else {
95
96
            $scope = $this->getNamespaceKey($namespace);
97
98
            if ( $scope === false ) return false;
99
100
            return apcu_delete($namespace);
101
102
        }
103
104
    }
105
106
    public function getMultiple(array $keys, $namespace) {
107
108
        $keypad = array_combine($keys, array_fill(0, count($keys), null));
109
110
        $scope = $this->getNamespaceKey($namespace);
111
112
        if ( $scope === false ) return $keypad;
113
114
        $keyscope = array_map(function($key) use($scope) {
115
            return "$scope-$key";
116
        }, $keys);
117
118
        $data = apcu_fetch($keyscope, $success);
119
120
        $return = [];
121
122
        foreach ( $data as $scoped_key => $value ) {
123
            $key = substr($scoped_key, strlen("$scope-"));
124
            $return[$key] = $value;
125
        }
126
127
        return array_replace($keypad, $return);
128
129
    }
130
131
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
132
133
        if ( $ttl == null ) $ttl = 0;
134
135
        $scope = $this->getNamespaceKey($namespace);
136
137
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
138
139
        if ( $scope === false ) return false;
140
141
        $shadowNames = [];
142
143
        foreach ( $key_values as $key => $value ) {
144
            $shadowNames["$scope-$key"] = $value;
145
        }
146
147
        $data = apcu_store($shadowNames, null, $ttl);
148
149
        return empty($data) ? true : false;
150
151
    }
152
153
    public function deleteMultiple(array $keys, $namespace) {
154
155
        $scope = $this->getNamespaceKey($namespace);
156
157
        if ( $scope === false ) return false;
158
159
        $shadowNames = array_map(function($key) use($scope) {
160
            return "$scope-$key";
161
        }, $keys);
162
163
        $delete = apcu_delete($shadowNames);
164
165
        return empty($delete) ? true : false;
166
167
    }
168
169
    public function has($key, $namespace) {
170
171
        $scope = $this->getNamespaceKey($namespace);
172
173
        if ( $scope === false ) return false;
174
175
        return apcu_exists("$scope-$key");
176
177
    }
178
179
    public function stats() {
180
181
        return apcu_cache_info(true);
182
183
    }
184
185
    /**
186
     * Set namespace key
187
     *
188
     * @return  mixed
189
     */
190
    private function setNamespaceKey($namespace) {
191
192
        $uId = UniqueId::get();
193
194
        return apcu_store($namespace, $uId, 0) === false ? false : $uId;
195
196
    }
197
198
    /**
199
     * Get namespace key
200
     *
201
     * @return  string
202
     */
203
    private function getNamespaceKey($namespace) {
204
205
        return apcu_fetch($namespace);
206
207
    }
208
209
}
210