Completed
Push — master ( e91e9a...b4b292 )
by Alexander
01:49
created

ApcuCache::deleteValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
namespace Yiisoft\Cache;
3
4
/**
5
 * ApcuCache provides APCu caching in terms of an application component.
6
 *
7
 * To use this application component, the [APCu PHP extension](http://www.php.net/apcu) must be loaded.
8
 * In order to enable APCu for CLI you should add "apc.enable_cli = 1" to your php.ini.
9
 *
10
 * Application configuration example:
11
 *
12
 * ```php
13
 * return [
14
 *     'components' => [
15
 *         'cache' => [
16
 *             '__class' => Yiisoft\Cache\Cache::class,
17
 *             'handler' => [
18
 *                 '__class' => Yiisoft\Cache\ApcCache::class,
19
 *             ],
20
 *         ],
21
 *         // ...
22
 *     ],
23
 *     // ...
24
 * ];
25
 * ```
26
 *
27
 * See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ApcCache supports.
28
 */
29
final class ApcuCache extends SimpleCache
30
{
31
    private const TTL_INFINITY = 0;
32
33
    protected function hasValue(string $key): bool
34
    {
35
        return \apcu_exists($key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_exists($key) could return the type string[] which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
36
    }
37
38
    protected function getValue(string $key, $default = null)
39
    {
40
        $value = \apcu_fetch($key, $success);
41
        return $success ? $value : $default;
42
    }
43
44
    protected function getValues(iterable $keys, $default = null): iterable
45
    {
46
        return \apcu_fetch($keys, $success) ?: [];
0 ignored issues
show
Bug introduced by
$keys of type iterable is incompatible with the type string|string[] expected by parameter $key of apcu_fetch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        return \apcu_fetch(/** @scrutinizer ignore-type */ $keys, $success) ?: [];
Loading history...
47
    }
48
49
    protected function setValue(string $key, $value, ?int $ttl): bool
50
    {
51
        return \apcu_store($key, $value, $ttl ?? self::TTL_INFINITY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_store($key, ... ?? self::TTL_INFINITY) could return the type array which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
52
    }
53
54
    protected function setValues(iterable $values, ?int $ttl): bool
55
    {
56
        return \apcu_store($values, null, $ttl ?? self::TTL_INFINITY) === [];
0 ignored issues
show
Bug introduced by
$values of type iterable is incompatible with the type array|string expected by parameter $key of apcu_store(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        return \apcu_store(/** @scrutinizer ignore-type */ $values, null, $ttl ?? self::TTL_INFINITY) === [];
Loading history...
57
    }
58
59
    protected function deleteValue(string $key): bool
60
    {
61
        return \apcu_delete($key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_delete($key) could return the type string[] which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
62
    }
63
64
    public function clear(): bool
65
    {
66
        return \apcu_clear_cache();
67
    }
68
69
    public function deleteValues(iterable $keys): bool
70
    {
71
        return \apcu_delete($keys) === [];
72
    }
73
}
74