Completed
Push — master ( ecfeff...c95134 )
by
unknown
01:46
created

PHPCache::decrement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
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 22
    public function get($key, $default = null)
33
    {
34 22
        $key = $this->key($key);
35 20
        if (isset($this->cache[$key])) {
36 11
            list($expires, $value) = $this->cache[$key];
37 11
            if (!$expires || ($expires >= microtime(true))) {
38 10
                return $value;
39
            }
40 1
            unset($this->cache[$key]);
41
        }
42 20
        return $default;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 22
    public function set($key, $value, $ttl = null)
49
    {
50
        /* Cache gets a microtime expiry date. */
51 22
        $ttl = $this->ttl($ttl);
52 22
        $this->cache[$this->key($key)] = [
53 21
            $ttl ? ((int)$ttl + microtime(true)) : false,
54 21
            $value
55
        ];
56 21
        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 2
    public function increment($key, $step = 1)
111
    {
112 2
        $key = $this->key($key);
113 2
        $newValue = $this->get($key, 0) + $this->step($step);
114 2
        $result = $this->set($key, $newValue);
115 2
        return $result !== false ? $newValue : false;
0 ignored issues
show
introduced by
The condition $result !== false is always true.
Loading history...
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121 2
    public function decrement($key, $step = 1)
122
    {
123 2
        $key = $this->key($key);
124 2
        $newValue = $this->get($key, 0) - $this->step($step);
125 2
        $result = $this->set($key, $newValue);
126 2
        return $result !== false ? $newValue : false;
0 ignored issues
show
introduced by
The condition $result !== false is always true.
Loading history...
127
    }
128
}
129