MemoryCache   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 6
c 5
b 1
f 0
lcom 1
cbo 0
dl 0
loc 67
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 16 3
A set() 0 6 1
A expires() 0 8 2
1
<?php
2
3
namespace Phloppy\Cache;
4
5
class MemoryCache implements CacheInterface
6
{
7
8
    /**
9
     * @var array
10
     */
11
    protected $records = [];
12
13
14
    /**
15
     * Retrieve the nodes under the given key.
16
     *
17
     * @param string $key
18
     *
19
     * @return string[]
20
     */
21 4
    public function get($key)
22
    {
23 4
        if (!isset($this->records[$key])) {
24 1
            return null;
25
        }
26
27 4
        $record = $this->records[$key];
28
29 4
        if ($record['expire'] < time()) {
30 2
            unset($this->records[$key]);
31
32 2
            return null;
33
        }
34
35 4
        return $record['nodes'];
36
    }
37
38
39
    /**
40
     * Cache the given Nodes.
41
     *
42
     * @param string   $key
43
     * @param string[] $nodes
44
     * @param int      $ttl TTL in seconds.
45
     *
46
     * @return bool
47
     */
48 6
    public function set($key, array $nodes, $ttl)
49
    {
50 6
        $this->records[$key] = ['nodes' => $nodes, 'expire' => time() + $ttl];
51
52 6
        return true;
53
    }
54
55
56
    /**
57
     * Return seconds left until the key expires.
58
     *
59
     * @param string $key
60
     *
61
     * @return int The number of seconds the key is valid. 0 if expired or unknown.
62
     */
63 2
    public function expires($key)
64
    {
65 2
        if (!isset($this->records[$key])) {
66 1
            return 0;
67
        }
68
69 2
        return (int) max(0, $this->records[$key]['expire'] - time());
70
    }
71
}