ArrayCache   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
eloc 29
c 8
b 1
f 0
dl 0
loc 99
ccs 30
cts 30
cp 1
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A doDelete() 0 5 1
A doFlush() 0 5 1
A doSave() 0 5 2
A doGetStats() 0 8 1
A doFetch() 0 11 2
A doContains() 0 15 4
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use function time;
6
7
/**
8
 * Array cache driver.
9
 *
10
 * @link   www.doctrine-project.org
11
 */
12
class ArrayCache extends CacheProvider
13
{
14
    /** @var array[] $data each element being a tuple of [$data, $expiration], where the expiration is int|bool */
15
    private $data = [];
16
17
    /** @var int */
18
    private $hitsCount = 0;
19
20
    /** @var int */
21
    private $missesCount = 0;
22
23
    /** @var int */
24
    private $upTime;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 160
    public function __construct()
30
    {
31 160
        $this->upTime = time();
32 160
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 145
    protected function doFetch($id)
38
    {
39 145
        if (! $this->doContains($id)) {
40 81
            $this->missesCount += 1;
41
42 81
            return false;
43
        }
44
45 134
        $this->hitsCount += 1;
46
47 134
        return $this->data[$id][0];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 156
    protected function doContains($id)
54
    {
55 156
        if (! isset($this->data[$id])) {
56 156
            return false;
57
        }
58
59 144
        $expiration = $this->data[$id][1];
60
61 144
        if ($expiration && $expiration < time()) {
62 2
            $this->doDelete($id);
63
64 2
            return false;
65
        }
66
67 144
        return true;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 152
    protected function doSave($id, $data, $lifeTime = 0)
74
    {
75 152
        $this->data[$id] = [$data, $lifeTime ? time() + $lifeTime : false];
76
77 152
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 93
    protected function doDelete($id)
84
    {
85 93
        unset($this->data[$id]);
86
87 93
        return true;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 2
    protected function doFlush()
94
    {
95 2
        $this->data = [];
96
97 2
        return true;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 2
    protected function doGetStats()
104
    {
105
        return [
106 2
            Cache::STATS_HITS             => $this->hitsCount,
107 2
            Cache::STATS_MISSES           => $this->missesCount,
108 2
            Cache::STATS_UPTIME           => $this->upTime,
109
            Cache::STATS_MEMORY_USAGE     => null,
110
            Cache::STATS_MEMORY_AVAILABLE => null,
111
        ];
112
    }
113
}
114