MCCache::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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