Passed
Push — redis ( 69cfbf...5e7e33 )
by Francis
10:01
created

PHPCache::decrement()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 2
nop 3
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
    public function get($key, $default = null)
33
    {
34
        $key = $this->key($key);
35
        if (isset($this->cache[$key])) {
36
            list($expires, $value) = $this->cache[$key];
37
            if (!$expires || ($expires >= microtime(true))) {
38
                return $value;
39
            }
40
            unset($this->cache[$key]);
41
        }
42
        return $default;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function set($key, $value, $ttl = null)
49
    {
50
        /* Cache gets a microtime expiry date. */
51
        $ttl = $this->ttl($ttl);
52
        $this->cache[$this->key($key)] = [
53
            $ttl ? ((int)$ttl + microtime(true)) : false,
54
            $value
55
        ];
56
        return true;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function delete($key)
63
    {
64
        unset($this->cache[$this->key($key)]);
65
        return true;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function clean()
72
    {
73
        foreach ($this->cache as $key => $value) {
74
            list($expires) = $value;
75
            if ($expires && ($expires < microtime(true))) {
76
                unset($this->cache[$key]);
77
            }
78
        }
79
        return true;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function flush()
86
    {
87
        $this->cache = [];
88
        return true;
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function clear()
95
    {
96
        return $this->flush();
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function has($key)
103
    {
104
        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
        $result = $this->set($key, $newValue, (!$exists ? $ttl : null));
116
        return $result !== false ? $newValue : false;
0 ignored issues
show
introduced by
The condition $result !== false is always true.
Loading history...
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
        $result = $this->set($key, $newValue, (!$exists ? $ttl : null));
128
        return $result !== false ? $newValue : false;
0 ignored issues
show
introduced by
The condition $result !== false is always true.
Loading history...
129
    }
130
}
131