Memory::stats()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php namespace Comodojo\Cache\Drivers;
2
3
use \DateTime;
4
5
/**
6
 * @package     Comodojo Cache
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @license     MIT
9
 *
10
 * LICENSE:
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 */
20
21
class Memory extends AbstractDriver {
22
23
    const DRIVER_NAME = "memory";
24
25
    /**
26
     * Cache repository
27
     *
28
     * @param array
29
     */
30
    private $data = [];
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function __construct(array $configuration = []) {}
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 124
    public function test() {
41
42 124
        return true;
43
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 98
    public function get($key, $namespace) {
50
51 98
        return $this->checkMemory($key, $namespace) ?
52 98
            $this->data[$namespace][$key]['data'] : null;
53
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 90
    public function set($key, $namespace, $value, $ttl = null) {
60
61 90
        $this->checkNamespace($namespace, true);
62
63 90
        $expire = is_null($ttl) || $ttl == 0 ? 0 : new DateTime("now + $ttl secs");
64
65 90
        $this->data[$namespace][$key] = [
66 90
            'data' => $value,
67 90
            'expire' => $expire
68
        ];
69
70 90
        return true;
71
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 12
    public function delete($key, $namespace) {
78
79 12
        if ( !$this->checkMemory($key, $namespace) ) return false;
80
81 12
        unset($this->data[$namespace][$key]);
82
83 12
        return true;
84
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 22
    public function clear($namespace = null) {
91
92 22
        if ( $namespace == null ) {
93
94 18
            $this->data = [];
95
96 18
            return true;
97
98
        } else {
99
100 4
            if ( $this->checkNamespace($namespace) ) {
101 4
                unset($this->data[$namespace]);
102 4
                return true;
103
            }
104
105
            return false;
106
107
        }
108
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 3
    public function getMultiple(array $keys, $namespace) {
115
116 3
        $result = [];
117
118 3
        foreach ( $keys as $key ) {
119 3
            $result[$key] = $this->get($key, $namespace);
120
        }
121
122 3
        return $result;
123
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 2
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
130
131 2
        $result = [];
132
133 2
        foreach ( $key_values as $key => $value ) {
134 2
            $result[$key] = $this->set($key, $namespace, $value, $ttl);
135
        }
136
137 2
        return !in_array(false, $result);
138
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 4
    public function deleteMultiple(array $keys, $namespace) {
145
146 4
        $result = [];
147
148 4
        foreach ( $keys as $key ) {
149 4
            $result[] = $this->delete($key, $namespace);
150
        }
151
152 4
        return !in_array(false, $result);
153
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 46
    public function has($key, $namespace) {
160
161 46
        return $this->checkMemory($key, $namespace);
162
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 4
    public function stats() {
169
170 4
        return ['objects' => count($this->data)];
171
172
    }
173
174 90
    private function checkNamespace($namespace, $create = false) {
175
176 90
        if ( array_key_exists($namespace, $this->data) ) {
177 17
            return true;
178 90
        } else if ( $create ) {
179 90
            $this->data[$namespace] = [];
180 90
            return true;
181
        } else {
182
            return false;
183
        }
184
185
    }
186
187 107
    private function checkMemory($key, $namespace) {
188
189 107
        $time = new DateTime('now');
190
191 107
        if ( !array_key_exists($namespace, $this->data)
192 107
            || !array_key_exists($key, $this->data[$namespace]) ) {
193 68
            return false;
194 84
        } else if ( $this->data[$namespace][$key]['expire'] === 0
195 84
            || $this->data[$namespace][$key]['expire'] > $time ) {
196 82
            return true;
197
        } else {
198 2
            unset($this->data[$namespace][$key]);
199 2
            return false;
200
        }
201
202
    }
203
204
}
205