Completed
Push — master ( fbb370...38e670 )
by Alexander
05:52
created

ApcCache   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 10
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 28
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 4 2
A hasValue() 0 3 1
A deleteValue() 0 3 1
A setValue() 0 3 1
A setValues() 0 3 1
A getValues() 0 3 2
A clear() 0 3 1
1
<?php
2
namespace Yiisoft\Cache;
3
4
/**
5
 * ApcCache 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 ApcCache extends SimpleCache
30
{
31
    private const TTL_INFINITY = 0;
32
33
    public function hasValue(string $key): bool
34
    {
35
        return (bool)\apcu_exists($key);
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(array $keys, $default = null): array
45
    {
46
        return \apcu_fetch($keys, $succses) ?: [];
47
    }
48
49
    protected function setValue(string $key, $value, ?int $ttl): bool
50
    {
51
        return (bool)\apcu_store($key, $value, $ttl ?? self::TTL_INFINITY);
52
    }
53
54
    protected function setValues(array $values, ?int $ttl): bool
55
    {
56
        return \apcu_store($values, null, $ttl ?? self::TTL_INFINITY) === [];
57
    }
58
59
    protected function deleteValue(string $key): bool
60
    {
61
        return (bool)\apcu_delete($key);
62
    }
63
64
    public function clear(): bool
65
    {
66
        return \apcu_clear_cache();
67
    }
68
}
69