Test Failed
Push — master ( ebcfce...031f57 )
by Moln
01:19 queued 12s
created

ArrayCache::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
declare(strict_types=1);
3
4
namespace MySQLReplication\Cache;
5
6
use Composer\InstalledVersions;
7
use Psr\SimpleCache\CacheInterface;
8
9
if (class_exists(InstalledVersions::class) && version_compare(InstalledVersions::getVersion("psr/simple-cache"), '3.0.0', ">=")) {
10
    class ArrayCache implements CacheInterface
11
    {
12
        use ArrayCacheTrait;
13
14
        /**
15
         * @inheritDoc
16
         */
17
        public function get(string $key, $default = null): mixed
18
        {
19
            return $this->has($key) ? $this->tableMapCache[$key] : $default;
20
        }
21
22
        /**
23
         * @inheritDoc
24
         */
25
        public function getMultiple(iterable $keys, $default = null): iterable
26
        {
27
            $data = [];
28
            foreach ($keys as $key) {
29
                if ($this->has($key)) {
30
                    $data[$key] = $this->tableMapCache[$key];
31
                }
32
            }
33
34
            return [] !== $data ? $data : $default;
35
        }
36
    }
37
} else {
38
    class ArrayCache implements CacheInterface
39
    {
40
        use ArrayCacheTrait;
41
42
        /**
43
         * @inheritDoc
44
         */
45
        public function get($key, $default = null)
46
        {
47
            return $this->has($key) ? $this->tableMapCache[$key] : $default;
48
        }
49
50
        /**
51
         * @inheritDoc
52
         */
53
        public function getMultiple($keys, $default = null)
54
        {
55
            $data = [];
56
            foreach ($keys as $key) {
57
                if ($this->has($key)) {
58
                    $data[$key] = $this->tableMapCache[$key];
59
                }
60
            }
61
62
            return [] !== $data ? $data : $default;
63
        }
64
    }
65
}
66
67
trait ArrayCacheTrait {
68
    private $tableMapCache = [];
69
70
    /**
71
     * @var int
72
     */
73
    private $tableCacheSize;
74
75
    public function __construct(int $tableCacheSize = 128)
76
    {
77
        $this->tableCacheSize = $tableCacheSize;
78
    }
79
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function has($key): bool
85
    {
86
        return isset($this->tableMapCache[$key]);
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function clear(): bool
93
    {
94
        $this->tableMapCache = [];
95
96
        return true;
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function setMultiple($values, $ttl = null): bool
0 ignored issues
show
Unused Code introduced by
The parameter $ttl is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

102
    public function setMultiple($values, /** @scrutinizer ignore-unused */ $ttl = null): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        foreach ($values as $key => $value) {
105
            $this->set($key, $value);
106
        }
107
108
        return true;
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function set($key, $value, $ttl = null): bool
0 ignored issues
show
Unused Code introduced by
The parameter $ttl is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

114
    public function set($key, $value, /** @scrutinizer ignore-unused */ $ttl = null): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        // automatically clear table cache to save memory
117
        if (count($this->tableMapCache) > $this->tableCacheSize) {
118
            $this->tableMapCache = array_slice(
119
                $this->tableMapCache,
120
                (int)($this->tableCacheSize / 2),
121
                null,
122
                true
123
            );
124
        }
125
126
        $this->tableMapCache[$key] = $value;
127
128
        return true;
129
    }
130
131
    /**
132
     * @inheritDoc
133
     */
134
    public function deleteMultiple($keys): bool
135
    {
136
        foreach ($keys as $key) {
137
            $this->delete($key);
138
        }
139
140
        return true;
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146
    public function delete($key): bool
147
    {
148
        unset($this->tableMapCache[$key]);
149
150
        return true;
151
    }
152
}
153