Cache   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 95.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 190
ccs 45
cts 47
cp 0.9574
rs 10
wmc 20

13 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 3 1
A getMultiple() 0 3 1
A rebuild() 0 7 2
A clear() 0 3 1
A gc() 0 16 3
A set() 0 3 1
A get() 0 3 1
A getType() 0 3 1
A setMultiple() 0 3 1
A __construct() 0 18 5
A delete() 0 3 1
A clearExpiredItems() 0 6 1
A deleteMultiple() 0 3 1
1
<?php
2
/*
3
 * This file is part of the Shieldon Simple Cache package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\SimpleCache;
14
15
use Psr\SimpleCache\CacheInterface;
16
use Shieldon\SimpleCache\Exception\CacheArgumentException;
17
use function file_exists;
18
use function is_string;
19
use function strtolower;
20
use function ucfirst;
21
use function method_exists;
22
23
/**
24
 * The base Cache Adapter class.
25
 */
26
class Cache
27
{
28
    /**
29
     * The cache driver.
30
     *
31
     * @var null|CacheInterface
32
     */
33
    protected $driver;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param string|CacheInterface $driver   The cache driver.
39
     * @param array                 $settings The settings.
40
     *
41
     * @throws CacheException
42
     */
43 50
    public function __construct($driver = null, array $settings = [])
44
    {
45 50
        if ($driver instanceof CacheInterface) {
46 2
            $this->driver = $driver;
47 48
        } elseif (is_string($driver)) {
48 48
            $class = ucfirst(strtolower($driver));
49
50 48
            if (file_exists(__DIR__ . '/Driver/' . $class . '.php')) {
51 46
                $class = '\Shieldon\SimpleCache\Driver\\' . $class;
52
53 46
                $this->driver = new $class($settings);
54 42
                $this->gc($settings);
55
            }
56
        }
57
58 46
        if (!$this->driver) {
59 2
            throw new CacheArgumentException(
60 2
                'The data driver is not set correctly.'
61
            );
62
        }
63 44
    }
64
65
    /**
66
     * Get the cache.
67
     *
68
     * @param string $ket The key of a cache.
69
     * @param mixed  $val The value of a cache.
70
     * @return mixed
71
     */
72 24
    public function get($key, $default = null)
73
    {
74 24
        return $this->driver->get($key, $default);
75
    }
76
77
    /**
78
     * Set a cache.
79
     *
80
     * @param string $key   The key of a cache.
81
     * @param mixed  $value The value of a cache.
82
     * @param int    $ttl   The time to live.
83
     * @return bool
84
     */
85 26
    public function set($key, $value, $ttl = null)
86
    {
87 26
        return $this->driver->set($key, $value, $ttl);
88
    }
89
90
    /**
91
     * Delete a cache.
92
     *
93
     * @param string $key The key of a cache.
94
     * @return bool
95
     */
96 2
    public function delete($key)
97
    {
98 2
        return $this->driver->delete($key);
99
    }
100
101
    /**
102
     * Clear all caches.
103
     *
104
     * @return bool
105
     */
106 22
    public function clear()
107
    {
108 22
        return $this->driver->clear();
109
    }
110
111
    /**
112
     * Check if a cache exists.
113
     *
114
     * @param string $key The key of a cache.
115
     * @return bool
116
     */
117 4
    public function has($key)
118
    {
119 4
        return $this->driver->has($key);
120
    }
121
122
    /**
123
     * Get multiple caches.
124
     *
125
     * @param array $keys    The keys of a cache.
126
     * @param mixed $default The default value.
127
     * @return iterable
128
     */
129 2
    public function getMultiple($keys, $default = null)
130
    {
131 2
        return $this->driver->getMultiple($keys, $default);
132
    }
133
134
    /**
135
     * Set multiple caches.
136
     *
137
     * @param array $values The keys and values of a cache.
138
     * @param int   $ttl    The number of seconds until the cache will expire.
139
     * @return bool
140
     */
141 6
    public function setMultiple($values, $ttl = null)
142
    {
143 6
        return $this->driver->setMultiple($values, $ttl);
144
    }
145
146
    /**
147
     * Delete multiple caches.
148
     *
149
     * @param array $keys The keys of a cache.
150
     * @return bool
151
     */
152 4
    public function deleteMultiple($keys)
153
    {
154 4
        return $this->driver->deleteMultiple($keys);
155
    }
156
157
    /**
158
     * Create or rebuid the data schema. [Non-PSR-16]
159
     * This method is avaialbe for Mysql and Sqlite drivers.
160
     *
161
     * @return bool
162
     */
163 4
    public function rebuild(): bool
164
    {
165 4
        if (method_exists($this->driver, 'rebuild')) {
166 2
            return $this->driver->rebuild();
167
        }
168
169 2
        return false;
170
    }
171
172
    /**
173
     * Clear all expired items. [Non-PSR-16]
174
     *
175
     * @return array The list of the removed items.
176
     */
177 2
    public function clearExpiredItems(): array
178
    {
179 2
        return $this->gc([
180 2
            'gc_enable'      => true,
181
            'gc_probability' => 1,
182
            'gc_divisor'     => 1,
183
        ]);
184
    }
185
186
    public function getType(): string
187
    {
188
        return $this->driver->getType();
189
    }
190
191
    /**
192
     * Performing cache data garbage collection for drivers that don't have
193
     * ability to remove expired items automatically.
194
     * This method is not needed for Redis and Memcached driver.
195
     *
196
     * @param array $settings [bool $gc_enable, int $gc_probability, int $gc_divisor]
197
     *
198
     * @return array The list of the removed items.
199
     */
200 42
    protected function gc(array $settings = []): array
201
    {
202 42
        if (empty($settings['gc_enable'])) {
203 42
            return [];
204
        }
205
206 20
        $removedList = [];
207
208 20
        $probability = $settings['gc_probability'] ?? 1;
209 20
        $divisor     = $settings['gc_divisor'] ?? 100;
210
211 20
        if (method_exists($this->driver, 'gc')) {
212 20
            $removedList = $this->driver->gc($probability, $divisor);
213
        }
214
215 20
        return $removedList;
216
    }
217
}
218