Passed
Push — master ( 4bba79...d3b029 )
by Terry
17:12
created

Cache::clearExpiredItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
22
/**
23
 * The base Cache Adapter class.
24
 */
25
class Cache
26
{
27
    /**
28
     * The cache driver.
29
     *
30
     * @var null|CacheInterface
31
     */
32
    protected $driver;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param string|CacheInterface $driver   The cache driver.
38
     * @param array                 $settings The settings.
39
     * 
40
     * @throws CacheException
41
     */
42 50
    public function __construct($driver = '', array $settings = [])
43
    {
44 50
        if ($driver instanceof CacheInterface) {
45 2
            $this->driver = $driver;
46
47 48
        } elseif (is_string($driver)) {
0 ignored issues
show
introduced by
The condition is_string($driver) is always true.
Loading history...
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
     * @inheritDoc CacheInterface
67
     */
68 24
    public function get($key, $default = null)
69
    {
70 24
        return $this->driver->get($key, $default);
71
    }
72
73
    /**
74
     * @inheritDoc CacheInterface
75
     */
76 26
    public function set($key, $value, $ttl = null)
77
    {
78 26
        return $this->driver->set($key, $value, $ttl);
79
    }
80
81
    /**
82
     * @inheritDoc CacheInterface
83
     */
84 2
    public function delete($key)
85
    {
86 2
        return $this->driver->delete($key);
87
    }
88
89
    /**
90
     * @inheritDoc CacheInterface
91
     */
92 22
    public function clear()
93
    {
94 22
        return $this->driver->clear();
95
    }
96
97
    /**
98
     * @inheritDoc CacheInterface
99
     */
100 4
    public function has($key)
101
    {
102 4
        return $this->driver->has($key);
103
    }
104
105
    /**
106
     * @inheritDoc CacheInterface
107
     */
108 2
    public function getMultiple($keys, $default = null)
109
    {
110 2
        return $this->driver->getMultiple($keys, $default);
111
    }
112
113
    /**
114
     * @inheritDoc CacheInterface
115
     */
116 6
    public function setMultiple($values, $ttl = null)
117
    {
118 6
        return $this->driver->setMultiple($values, $ttl);
119
    }
120
121
    /**
122
     * @inheritDoc CacheInterface
123
     */
124 4
    public function deleteMultiple($keys)
125
    {
126 4
        return $this->driver->deleteMultiple($keys);
127
    }
128
129
    /**
130
     * Create or rebuid the data schema. [Non-PSR-16]
131
     * This method is avaialbe for Mysql and Sqlite drivers.
132
     *
133
     * @return bool
134
     */
135 4
    public function rebuild(): bool
136
    {
137 4
        if (method_exists($this->driver, 'rebuild')) {
138 2
            return $this->driver->rebuild();
139
        }
140
141 2
        return false;
142
    }
143
144
    /**
145
     * Clear all expired items. [Non-PSR-16]
146
     *
147
     * @return array The list of the removed items.
148
     */
149 2
    public function clearExpiredItems(): array
150
    {
151 2
        return $this->gc([
152 2
            'gc_enable'      => true,
153
            'gc_probability' => 1,
154
            'gc_divisor'     => 1,
155
        ]);
156
    }
157
158
    /**
159
     * Performing cache data garbage collection for drivers that don't have 
160
     * ability to remove expired items automatically.
161
     * This method is not needed for Redis and Memcached driver.
162
     *
163
     * @param array $settings [bool $gc_enable, int $gc_probability, int $gc_divisor]
164
     *
165
     * @return array The list of the removed items.
166
     */
167 42
    protected function gc(array $settings = []): array
168
    {
169 42
        if (empty($settings['gc_enable'])) {
170 42
            return [];
171
        }
172
173 20
        $removedList = [];
174
175 20
        $probability = $settings['gc_probability'] ?? 1;
176 20
        $divisor     = $settings['gc_divisor']     ?? 100;
177
178 20
        if (method_exists($this->driver, 'gc')) {
179 20
            $removedList = $this->driver->gc($probability, $divisor);
180
        }
181
182 20
        return $removedList;
183
    }
184
}