APCCache::setMultiple()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Vectorface\Cache;
4
5
use DateInterval;
6
use Vectorface\Cache\Common\PSR16Util;
7
8
/**
9
 * This cache is ridiculously fast, according to basic benchmarks:
10
 *
11
 * Parameters:
12
 *   APC 3.0.19
13
 *   9-byte key
14
 *   151-byte value
15
 *   10000-iteration test
16
 *
17
 * Result:
18
 *   0.065223 seconds
19
 *
20
 * Conclusion:
21
 *   Capable of approximately 150000 requests/second
22
 */
23
24
/**
25
 * Implements the Cache interface on top of APC or APCu.
26
 */
27
class APCCache implements Cache, AtomicCounter
28
{
29
    use PSR16Util;
30
31
    /**
32
     * The module name that defines the APC methods.
33
     */
34
    private string $apcModule = 'apcu';
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function get(string $key, mixed $default = null) : mixed
40 14
    {
41
        $value = $this->call('fetch', $this->key($key));
42 14
        return ($value === false) ? $default : $value;
43 13
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function set(string $key, mixed $value, DateInterval|int|null $ttl = null) : bool
49 15
    {
50
        return $this->call('store', $this->key($key), $value, $this->ttl($ttl));
51 15
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function delete($key) : bool
57 3
    {
58
        return $this->call('delete', $this->key($key));
59 3
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function clean() : bool
65 1
    {
66
        return false;
67 1
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function flush() : bool
73 6
    {
74
        return $this->call('clear_cache');
75 6
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function clear() : bool
81 3
    {
82
        return $this->flush();
83 3
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function getMultiple(iterable $keys, mixed $default = null) : iterable
89 3
    {
90
        $keys = $this->keys($keys);
91 3
        return $this->defaults(
92 2
            $keys,
93 2
            $this->call('fetch', $keys),
94 2
            $default
95 2
        );
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function setMultiple(iterable $values, DateInterval|int|null $ttl = null) : bool
102 3
    {
103
        $results = $this->call(
104 3
            'store',
105 3
            $this->values($values),
106 3
            null,
107 2
            $this->ttl($ttl)
108 2
        );
109
        return array_reduce($results, static fn($carry, $item) => $carry && $item, true);
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115
    public function deleteMultiple(iterable $keys) : bool
116 2
    {
117
        $success = true;
118 2
        foreach ($this->keys($keys) as $key) {
119 2
            $success = $this->call('delete', $key) && $success;
120 2
        }
121
122
        return $success;
123 2
    }
124
125
    /**
126
     * @inheritDoc
127
     */
128
    public function has(string $key) : bool
129 1
    {
130
        return $this->call('exists', $this->key($key));
131 1
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136
    public function increment(string $key, int $step = 1, DateInterval|int|null  $ttl = null) : int|false
137 1
    {
138
        return $this->call('inc', $this->key($key), $this->step($step), null, $this->ttl($ttl));
139 1
    }
140
141
    /**
142
     * @inheritDoc
143
     */
144
    public function decrement(string $key, int $step = 1, DateInterval|int|null $ttl = null) : int|false
145 1
    {
146
        return $this->call('dec', $this->key($key), $this->step($step), null, $this->ttl($ttl));
147 1
    }
148
149
    /**
150
     * Pass a call through to APC or APCu
151
     * @param string $call Transformed to a function apc(u)_$call
152
     * @param mixed ...$args Function arguments
153
     * @return mixed The result passed through from apc(u)_$call
154
     */
155
    private function call(string $call, ...$args) : mixed
156 17
    {
157
        $function = "{$this->apcModule}_{$call}";
158 17
159
        return $function(...$args);
160 17
    }
161
}
162