Passed
Push — develop ( 304111...cd86fb )
by Jonathan
07:42 queued 06:18
created

PHPCache::decrement()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 1
nop 3
crap 6
1
<?php
2
3
namespace Vectorface\Cache;
4
5
use Vectorface\Cache\Common\PSR16Util;
6
use Vectorface\Cache\Common\MultipleTrait;
7
8
/**
9
 * A cache implementation using an internal PHP associative array.
10
 *
11
 * This cache is very fast, but volatile: cache is only maintained while the PHP interpreter is running.
12
 * Usually, this means one HTTP request.
13
 *
14
 * Capable of a huge number of requests/second
15
 */
16
class PHPCache implements Cache, AtomicCounter
17
{
18
    use MultipleTrait, PSR16Util;
19
20
    /**
21
     * The "cache" which stores entries for the lifetime of the request.
22
     *
23
     * Each entry is an [expiry, value] pair, where expiry is a timestamp.
24
     *
25
     * @var mixed[]
26
     */
27
    protected $cache = [];
28
29
    /**
30
     * @inheritDoc
31
     */
32 21
    public function get($key, $default = null)
33
    {
34 21
        $key = $this->key($key);
35 19
        if (isset($this->cache[$key])) {
36 10
            list($expires, $value) = $this->cache[$key];
37 10
            if (!$expires || ($expires >= microtime(true))) {
38 9
                return $value;
39
            }
40 1
            unset($this->cache[$key]);
41
        }
42 19
        return $default;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 21
    public function set($key, $value, $ttl = null)
49
    {
50
        /* Cache gets a microtime expiry date. */
51 21
        $ttl = $this->ttl($ttl);
52 21
        $this->cache[$this->key($key)] = [
53 20
            $ttl ? ((int)$ttl + microtime(true)) : false,
54 20
            $value
55
        ];
56 20
        return true;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62 6
    public function delete($key)
63
    {
64 6
        unset($this->cache[$this->key($key)]);
65 6
        return true;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71 3
    public function clean()
72
    {
73 3
        foreach ($this->cache as $key => $value) {
74 2
            list($expires) = $value;
75 2
            if ($expires && ($expires < microtime(true))) {
76 1
                unset($this->cache[$key]);
77
            }
78
        }
79 3
        return true;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85 7
    public function flush()
86
    {
87 7
        $this->cache = [];
88 7
        return true;
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94 3
    public function clear()
95
    {
96 3
        return $this->flush();
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102 2
    public function has($key)
103
    {
104 2
        return $this->get($this->key($key)) !== null;
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function increment($key, $step = 1, $ttl = null)
111
    {
112
        $key = $this->key($key);
113
        $exists = $this->has($key);
114
        $newValue = $this->get($key, 0) + $this->step($step);
115
        $this->set($key, $newValue, (!$exists ? $ttl : null));
116
        return $newValue;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function decrement($key, $step = 1, $ttl = null)
123
    {
124
        $key = $this->key($key);
125
        $exists = $this->has($key);
126
        $newValue = $this->get($key, 0) - $this->step($step);
127
        $this->set($key, $newValue, (!$exists ? $ttl : null));
128
        return $newValue;
129
    }
130
}
131