Passed
Push — master ( a6a24c...80ac3a )
by kacper
03:22
created

ArrayCache::setMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace MySQLReplication\Cache;
4
5
use MySQLReplication\Config\Config;
6
use Psr\SimpleCache\CacheInterface;
7
8
/**
9
 * Class ArrayCache
10
 * @package MySQLReplication\Cache
11
 */
12
class ArrayCache implements CacheInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    private static $tableMapCache = [];
18
    
19
    /**
20
     * Fetches a value from the cache.
21
     *
22
     * @param string $key The unique key of this item in the cache.
23
     * @param mixed $default Default value to return if the key does not exist.
24
     *
25
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
26
     *
27
     * @throws \Psr\SimpleCache\InvalidArgumentException
28
     *   MUST be thrown if the $key string is not a legal value.
29
     */
30 57
    public function get($key, $default = null)
31
    {
32 57
        return $this->has($key) ? self::$tableMapCache[$key] : $default;
33
    }
34
35
    /**
36
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
37
     *
38
     * @param string $key The key of the item to store.
39
     * @param mixed $value The value of the item to store, must be serializable.
40
     * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
41
     *                                     the driver supports TTL then the library may set a default value
42
     *                                     for it or let the driver take care of that.
43
     *
44
     * @return bool True on success and false on failure.
45
     *
46
     * @throws \Psr\SimpleCache\InvalidArgumentException
47
     *   MUST be thrown if the $key string is not a legal value.
48
     */
49 61
    public function set($key, $value, $ttl = null)
50
    {
51
        // automatically clear table cache to save memory
52 61
        if (count(self::$tableMapCache) > Config::getTableCacheSize()) {
53 5
            self::$tableMapCache = array_slice(
54 5
                self::$tableMapCache,
55 5
                ceil(Config::getTableCacheSize() / 2),
0 ignored issues
show
Bug introduced by
ceil(MySQLReplication\Co...etTableCacheSize() / 2) of type double is incompatible with the type integer expected by parameter $offset of array_slice(). ( Ignorable by Annotation )

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

55
                /** @scrutinizer ignore-type */ ceil(Config::getTableCacheSize() / 2),
Loading history...
56 5
                null,
57
                true
58 5
            );
59 5
        }
60
61 61
        self::$tableMapCache[$key] = $value;
62
63 61
        return true;
64
    }
65
66
    /**
67
     * Delete an item from the cache by its unique key.
68
     *
69
     * @param string $key The unique cache key of the item to delete.
70
     *
71
     * @return bool True if the item was successfully removed. False if there was an error.
72
     *
73
     * @throws \Psr\SimpleCache\InvalidArgumentException
74
     *   MUST be thrown if the $key string is not a legal value.
75
     */
76 2
    public function delete($key)
77
    {
78 2
        unset(self::$tableMapCache[$key]);
79
80 2
        return true;
81
    }
82
83
    /**
84
     * Wipes clean the entire cache's keys.
85
     *
86
     * @return bool True on success and false on failure.
87
     */
88 1
    public function clear()
89
    {
90 1
        self::$tableMapCache = [];
91
92 1
        return true;
93
    }
94
95
    /**
96
     * Obtains multiple cache items by their unique keys.
97
     *
98
     * @param iterable $keys A list of keys that can obtained in a single operation.
99
     * @param mixed $default Default value to return for keys that do not exist.
100
     *
101
     * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
102
     *
103
     * @throws \Psr\SimpleCache\InvalidArgumentException
104
     *   MUST be thrown if $keys is neither an array nor a Traversable,
105
     *   or if any of the $keys are not a legal value.
106
     */
107 3
    public function getMultiple($keys, $default = null)
108
    {
109 3
        $data = [];
110 3
        foreach ($keys as $key) {
111 3
            if ($this->has($key)) {
112 3
                $data[$key] = self::$tableMapCache[$key];
113 3
            }
114 3
        }
115
116 3
        return [] !== $data ? $data : $default;
117
    }
118
119
    /**
120
     * Persists a set of key => value pairs in the cache, with an optional TTL.
121
     *
122
     * @param iterable $values A list of key => value pairs for a multiple-set operation.
123
     * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
124
     *                                      the driver supports TTL then the library may set a default value
125
     *                                      for it or let the driver take care of that.
126
     *
127
     * @return bool True on success and false on failure.
128
     *
129
     * @throws \Psr\SimpleCache\InvalidArgumentException
130
     *   MUST be thrown if $values is neither an array nor a Traversable,
131
     *   or if any of the $values are not a legal value.
132
     */
133 3
    public function setMultiple($values, $ttl = null)
134
    {
135 3
        foreach ($values as $key => $value) {
136 3
            $this->set($key, $value);
137 3
        }
138
139 3
        return true;
140
    }
141
142
    /**
143
     * Deletes multiple cache items in a single operation.
144
     *
145
     * @param iterable $keys A list of string-based keys to be deleted.
146
     *
147
     * @return bool True if the items were successfully removed. False if there was an error.
148
     *
149
     * @throws \Psr\SimpleCache\InvalidArgumentException
150
     *   MUST be thrown if $keys is neither an array nor a Traversable,
151
     *   or if any of the $keys are not a legal value.
152
     */
153 1
    public function deleteMultiple($keys)
154
    {
155 1
        foreach ($keys as $key) {
156 1
            $this->delete($key);
157 1
        }
158
159 1
        return true;
160
    }
161
162
    /**
163
     * Determines whether an item is present in the cache.
164
     *
165
     * NOTE: It is recommended that has() is only to be used for cache warming type purposes
166
     * and not to be used within your live applications operations for get/set, as this method
167
     * is subject to a race condition where your has() will return true and immediately after,
168
     * another script can remove it making the state of your app out of date.
169
     *
170
     * @param string $key The cache item key.
171
     *
172
     * @return bool
173
     *
174
     * @throws \Psr\SimpleCache\InvalidArgumentException
175
     *   MUST be thrown if the $key string is not a legal value.
176
     */
177 61
    public function has($key)
178
    {
179 61
        return isset(self::$tableMapCache[$key]);
180
    }
181
}