Passed
Pull Request — master (#75)
by kacper
04:01
created

ArrayCache::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MySQLReplication\Cache;
5
6
use MySQLReplication\Config\Config;
7
use Psr\SimpleCache\CacheInterface;
8
9
class ArrayCache implements CacheInterface
10
{
11
    private static $tableMapCache = [];
12
13
    /**
14
     * @inheritDoc
15
     */
16 58
    public function get($key, $default = null)
17
    {
18 58
        return $this->has($key) ? self::$tableMapCache[$key] : $default;
19
    }
20
21
    /**
22
     * @inheritDoc
23
     */
24 63
    public function has($key): bool
25
    {
26 63
        return isset(self::$tableMapCache[$key]);
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 2
    public function clear(): bool
33
    {
34 2
        self::$tableMapCache = [];
35
36 2
        return true;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 3
    public function getMultiple($keys, $default = null)
43
    {
44 3
        $data = [];
45 3
        foreach ($keys as $key) {
46 3
            if ($this->has($key)) {
47 3
                $data[$key] = self::$tableMapCache[$key];
48
            }
49
        }
50
51 3
        return [] !== $data ? $data : $default;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 3
    public function setMultiple($values, $ttl = null): bool
58
    {
59 3
        foreach ($values as $key => $value) {
60 3
            $this->set($key, $value);
61
        }
62
63 3
        return true;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 63
    public function set($key, $value, $ttl = null): bool
70
    {
71
        // automatically clear table cache to save memory
72 63
        if (count(self::$tableMapCache) > Config::getTableCacheSize()) {
73 5
            self::$tableMapCache = array_slice(
74
                self::$tableMapCache,
75 5
                (int)(Config::getTableCacheSize() / 2),
76 5
                null,
77 5
                true
78
            );
79
        }
80
81 63
        self::$tableMapCache[$key] = $value;
82
83 63
        return true;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89 1
    public function deleteMultiple($keys): bool
90
    {
91 1
        foreach ($keys as $key) {
92 1
            $this->delete($key);
93
        }
94
95 1
        return true;
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101 2
    public function delete($key): bool
102
    {
103 2
        unset(self::$tableMapCache[$key]);
104
105 2
        return true;
106
    }
107
}