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

MCCache::decrement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 3
1
<?php
2
/** @noinspection PhpComposerExtensionStubsInspection */
3
4
namespace Vectorface\Cache;
5
6
use Memcache;
7
use Vectorface\Cache\Common\PSR16Util;
8
9
/**
10
 * Implements the cache interface on top of Memcache
11
 *
12
 * This cache is very fast, according to basic benchmarks:
13
 *
14
 * Parameters:
15
 *   Memcache 1.2.2, running locally
16
 *   9-byte key
17
 *   151-byte value
18
 *   10000-iteration test
19
 *
20
 * Result:
21
 *   0.859622001648 seconds
22
 *
23
 * Conclusion:
24
 *   Capable of approximately 11678 requests/second
25
 */
26
class MCCache implements Cache, AtomicCounter
27
{
28
    use PSR16Util;
29
30
    /**
31
     * Memcache instance; Connection to the memcached server.
32
     *
33
     * @var Memcache
34
     */
35
    private $mc;
36
37
    /**
38
     * Create a new memcache-based cache.
39
     *
40
     * @param Memcache $mc The memcache instance, or null to try to build one.
41
     */
42
    public function __construct(Memcache $mc)
43
    {
44
        $this->mc = $mc;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function get($key, $default = null)
51
    {
52
        $value = $this->mc->get($this->key($key));
53
        return ($value === false) ? $default : $value;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function set($key, $value, $ttl = null)
60
    {
61
        return $this->mc->set($this->key($key), $value, null, $this->ttl($ttl));
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function delete($key)
68
    {
69
        return $this->mc->delete($this->key($key));
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function clean()
76
    {
77
        return true;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function flush()
84
    {
85
        return $this->mc->flush();
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function clear()
92
    {
93
        return $this->flush();
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function getMultiple($keys, $default = null)
100
    {
101
        $keys = $this->keys($keys);
102
        $values = $this->mc->get($keys);
103
104
        if ($values === false) {
105
            $values = [];
106
        } elseif (is_string($values)) {
107
            $values = [$values]; // shouldn't technically happen if $keys is an array
108
        }
109
110
        foreach ($keys as $key) {
111
            if (!isset($values[$key]) || $values[$key] === false) {
112
                $values[$key] = $default;
113
            }
114
        }
115
116
        return $values;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function setMultiple($values, $ttl = null)
123
    {
124
        $success = true;
125
        foreach ($this->values($values) as $key => $value) {
126
            $success = $this->set($key, $value, $ttl) && $success;
127
        }
128
        return $success;
129
    }
130
131
    /**
132
     * @inheritDoc
133
     */
134
    public function deleteMultiple($keys)
135
    {
136
        $success = true;
137
        foreach ($this->keys($keys) as $key) {
138
            $success = $this->delete($key) && $success;
139
        }
140
141
        return $success;
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147
    public function has($key)
148
    {
149
        return $this->get($this->key($key), null) !== null;
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    public function increment($key, $step = 1, $ttl = null)
156
    {
157
        $key = $this->key($key);
158
159
        // If the key already exists, this is a no-op, otherwise it ensures the key is created.
160
        // See https://www.php.net/manual/en/memcache.increment.php#90864
161
        $this->mc->add($key, 0, null, $this->ttl($ttl));
162
163
        return $this->mc->increment($key, $this->step($step));
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    public function decrement($key, $step = 1, $ttl = null)
170
    {
171
        $key = $this->key($key);
172
173
        // If the key already exists, this is a no-op, otherwise it ensures the key is created.
174
        // See https://www.php.net/manual/en/memcache.increment.php#90864
175
        $this->mc->add($key, 0, null, $this->ttl($ttl));
176
177
        return $this->mc->decrement($key, $this->step($step));
178
    }
179
}
180