Completed
Push — 2.1 ( 28b26f...4d9204 )
by Alexander
10:53
created

ArrayCache   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 55
ccs 16
cts 16
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteValue() 0 5 1
A has() 0 5 3
A getValue() 0 7 4
A setValue() 0 5 2
A clear() 0 5 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
/**
11
 * ArrayCache provides caching for the current request only by storing the values in an array.
12
 *
13
 * Application configuration example:
14
 *
15
 * ```php
16
 * return [
17
 *     'components' => [
18
 *         'cache' => [
19
 *             'class' => yii\caching\Cache::class,
20
 *             'handler' => [
21
 *                 'class' => yii\caching\ArrayCache::class,
22
 *             ],
23
 *         ],
24
 *         // ...
25
 *     ],
26
 *     // ...
27
 * ];
28
 * ```
29
 *
30
 * See [[\Psr\SimpleCache\CacheInterface]] for common cache operations that ArrayCache supports.
31
 *
32
 * Unlike the [[Cache]], ArrayCache allows the expire parameter of [[set()]] and [[setMultiple()]]  to
33
 * be a floating point number, so you may specify the time in milliseconds (e.g. 0.1 will be 100 milliseconds).
34
 *
35
 * For enhanced performance of ArrayCache, you can disable serialization of the stored data by setting [[$serializer]] to `false`.
36
 *
37
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
38
 *
39
 * @author Carsten Brandt <[email protected]>
40
 * @since 2.0
41
 */
42
class ArrayCache extends SimpleCache
43
{
44
    /**
45
     * @var array cached values.
46
     */
47
    private $_cache = [];
48
49
50
    /**
51
     * @inheritdoc
52
     */
53 6
    public function has($key)
54
    {
55 6
        $key = $this->normalizeKey($key);
56 6
        return isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true));
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 48
    protected function getValue($key)
63
    {
64 48
        if (isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true))) {
65 40
            return $this->_cache[$key][0];
66
        }
67 36
        return false;
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 48
    protected function setValue($key, $value, $ttl)
74
    {
75 48
        $this->_cache[$key] = [$value, $ttl === 0 ? 0 : microtime(true) + $ttl];
76 48
        return true;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 3
    protected function deleteValue($key)
83
    {
84 3
        unset($this->_cache[$key]);
85 3
        return true;
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 14
    public function clear()
92
    {
93 14
        $this->_cache = [];
94 14
        return true;
95
    }
96
}
97