Lines 56.25% 27 / 48
Methods 35.71% 5 / 14
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 25.00% 1 / 4 0.00% 0 / 1 3.69
 get 0.00% 0 / 4 0.00% 0 / 1 6
 set 0.00% 0 / 6 0.00% 0 / 1 20
 delete 0.00% 0 / 2 0.00% 0 / 1 2
 clear 0.00% 0 / 1 0.00% 0 / 1 2
 has 0.00% 0 / 2 0.00% 0 / 1 2
 [Lucent\Cache\Cache] setDefaultTtl 100.00% 1 / 1 100.00% 1 / 1 1
 [Lucent\Cache\Cache] getDefaultTtl 100.00% 1 / 1 100.00% 1 / 1 1
 [Lucent\Cache\Cache] resolveTtl 100.00% 1 / 1 100.00% 1 / 1 1
 [Lucent\Cache\Cache] validateKey 100.00% 2 / 2 100.00% 1 / 1 3
 [Lucent\Cache\Cache] expiryFromTtl 85.71% 6 / 7 0.00% 0 / 1 4.05
 [Lucent\Cache\Cache] getMultiple 100.00% 5 / 5 100.00% 1 / 1 2
 [Lucent\Cache\Cache] setMultiple 83.33% 5 / 6 0.00% 0 / 1 3.04
 [Lucent\Cache\Cache] deleteMultiple 83.33% 5 / 6 0.00% 0 / 1 3.04
20class ApcuDriver extends Cache
21{
22    /**
23     * @throws \RuntimeException If the `apcu` extension is not loaded
24     */
25    public function __construct()
26    {
27        if (!extension_loaded('apcu')) {
28            throw new \RuntimeException(
29                'The APCu cache driver requires the "apcu" extension. Install it or select a different CACHE_DRIVER.'
30            );
31        }
32    }
33
34    /**
35     * @inheritDoc
36     */
37    public function get(string $key, mixed $default = null): mixed
38    {
39        $this->validateKey($key);
40
41        $success = false;
42        $value = \apcu_fetch($key, $success);
43
44        return $success ? $value : $default;
45    }
46
47    /**
48     * @inheritDoc
49     */
50    public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
51    {
52        $this->validateKey($key);
53
54        $expires = $this->expiryFromTtl($this->resolveTtl($ttl));
55
56        if ($expires !== null && $expires <= time()) {
57            return $this->delete($key);
58        }
59
60        // APCu's TTL is a lifetime in seconds from now, not an absolute
61        // expiry timestamp. Convert the absolute expiry back to a duration.
62        $lifetime = $expires === null ? 0 : max(0, $expires - time());
63
64        return \apcu_store($key, $value, $lifetime);
65    }
66
67    /**
68     * @inheritDoc
69     */
70    public function delete(string $key): bool
71    {
72        $this->validateKey($key);
73
74        return \apcu_delete($key);
75    }
76
77    /**
78     * @inheritDoc
79     */
80    public function clear(): bool
81    {
82        return \apcu_clear_cache();
83    }
84
85    /**
86     * @inheritDoc
87     */
88    public function has(string $key): bool
89    {
90        $this->validateKey($key);
91
92        return \apcu_exists($key);
93    }
94}

Inherited from Lucent\Cache\Cache

45    public function setDefaultTtl(?int $ttl): void
46    {
47        $this->defaultTtl = $ttl;
48    }
55    public function getDefaultTtl(): ?int
56    {
57        return $this->defaultTtl;
58    }
69    protected function resolveTtl(null|int|DateInterval $ttl): null|int|DateInterval
70    {
71        return $ttl ?? $this->defaultTtl;
72    }
89    protected function validateKey(mixed $key): void
90    {
91        if (!is_string($key) || preg_match('/^[A-Za-z0-9_.]{1,512}$/', $key) !== 1) {
92            throw new InvalidArgumentException($key);
93        }
94    }
102    protected function expiryFromTtl(null|int|DateInterval $ttl): ?int
103    {
104        if ($ttl === null) {
105            return null;
106        }
107
108        if ($ttl instanceof DateInterval) {
109            $ttl = (new \DateTimeImmutable())->add($ttl)->getTimestamp() - time();
110        }
111
112        if ($ttl <= 0) {
113            // Already expired: return a timestamp in the past so the item is
114            // treated as stale and removed rather than stored.
115            return time() - 1;
116        }
117
118        return time() + $ttl;
119    }
124    public function getMultiple(iterable $keys, mixed $default = null): iterable
125    {
126        $result = [];
127
128        foreach ($keys as $key) {
129            $this->validateKey($key);
130            $result[$key] = $this->get($key, $default);
131        }
132
133        return $result;
134    }
139    public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
140    {
141        $success = true;
142
143        foreach ($values as $key => $value) {
144            $this->validateKey($key);
145            if (!$this->set($key, $value, $ttl)) {
146                $success = false;
147            }
148        }
149
150        return $success;
151    }
156    public function deleteMultiple(iterable $keys): bool
157    {
158        $success = true;
159
160        foreach ($keys as $key) {
161            $this->validateKey($key);
162            if (!$this->delete($key)) {
163                $success = false;
164            }
165        }
166
167        return $success;
168    }