Completed
Push — 2.1-master-merge ( 240673 )
by Alexander
13:45
created

ApcCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 73
ccs 0
cts 37
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A has() 0 4 1
A getValue() 0 4 1
A getValues() 0 5 2
A setValue() 0 4 1
A setValues() 0 5 1
A deleteValue() 0 4 1
A clear() 0 4 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\caching;
9
10
use yii\base\InvalidConfigException;
11
12
/**
13
 * ApcCache provides APCu caching in terms of an application component.
14
 *
15
 * To use this application component, the [APCu PHP extension](http://www.php.net/apcu) must be loaded.
16
 * In order to enable APCu for CLI you should add "apc.enable_cli = 1" to your php.ini.
17
 *
18
 * Application configuration example:
19
 *
20
 * ```php
21
 * return [
22
 *     'components' => [
23
 *         'cache' => [
24
 *             'class' => yii\caching\Cache::class,
25
 *             'handler' => [
26
 *                 'class' => yii\caching\ApcCache::class,
27
 *             ],
28
 *         ],
29
 *         // ...
30
 *     ],
31
 *     // ...
32
 * ];
33
 * ```
34
 *
35
 * See [[\Psr\SimpleCache\CacheInterface]] for common cache operations that ApcCache supports.
36
 *
37
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
38
 *
39
 * @author Qiang Xue <[email protected]>
40
 * @since 2.0
41
 */
42
class ApcCache extends SimpleCache
43
{
44
    /**
45
     * Initializes this application component.
46
     * It checks if extension required is loaded.
47
     * @throws \yii\base\InvalidConfigException
48
     */
49
    public function init()
50
    {
51
        parent::init();
52
        if (!extension_loaded('apcu')) {
53
            throw new InvalidConfigException('ApcCache requires PHP apcu extension to be loaded.');
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function has($key)
61
    {
62
        return apcu_exists($this->normalizeKey($key));
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_exists($this->normalizeKey($key)); of type boolean|string[] adds the type string[] to the return on line 62 which is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::has of type boolean.
Loading history...
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected function getValue($key)
69
    {
70
        return apcu_fetch($key);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function getValues($keys)
77
    {
78
        $values = apcu_fetch($keys);
79
        return is_array($values) ? $values : [];
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function setValue($key, $value, $ttl)
86
    {
87
        return apcu_store($key, $value, $ttl);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_store($key, $value, $ttl); of type boolean|array adds the type array to the return on line 87 which is incompatible with the return type declared by the abstract method yii\caching\SimpleCache::setValue of type boolean.
Loading history...
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function setValues($values, $ttl)
94
    {
95
        $result = apcu_store($values, null, $ttl);
96
        return is_array($result);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    protected function deleteValue($key)
103
    {
104
        return apcu_delete($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_delete($key); of type boolean|string[] adds the type string[] to the return on line 104 which is incompatible with the return type declared by the abstract method yii\caching\SimpleCache::deleteValue of type boolean.
Loading history...
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function clear()
111
    {
112
        return apcu_clear_cache();
113
    }
114
}
115