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
|
30 |
|
public function __construct($driver = '', array $settings = []) |
43
|
|
|
{ |
44
|
30 |
|
if ($driver instanceof CacheInterface) { |
45
|
2 |
|
$this->driver = $driver; |
46
|
|
|
|
47
|
28 |
|
} elseif (is_string($driver)) { |
|
|
|
|
48
|
28 |
|
$class = ucfirst(strtolower($driver)); |
49
|
|
|
|
50
|
28 |
|
if (file_exists(__DIR__ . '/Driver/' . $class . '.php')) { |
51
|
26 |
|
$class = '\Shieldon\SimpleCache\Driver\\' . $class; |
52
|
|
|
|
53
|
26 |
|
$this->driver = new $class($settings); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
26 |
|
if (!$this->driver) { |
58
|
2 |
|
throw new CacheArgumentException( |
59
|
2 |
|
'The data driver is not set correctly.' |
60
|
|
|
); |
61
|
|
|
} |
62
|
24 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc CacheInterface |
66
|
|
|
*/ |
67
|
4 |
|
public function get($key, $default = null) |
68
|
|
|
{ |
69
|
4 |
|
return $this->driver->get($key, $default); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc CacheInterface |
74
|
|
|
*/ |
75
|
6 |
|
public function set($key, $value, $ttl = null) |
76
|
|
|
{ |
77
|
6 |
|
return $this->driver->set($key, $value, $ttl); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc CacheInterface |
82
|
|
|
*/ |
83
|
2 |
|
public function delete($key) |
84
|
|
|
{ |
85
|
2 |
|
return $this->driver->delete($key); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc CacheInterface |
90
|
|
|
*/ |
91
|
2 |
|
public function clear() |
92
|
|
|
{ |
93
|
2 |
|
return $this->driver->clear(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc CacheInterface |
98
|
|
|
*/ |
99
|
4 |
|
public function has($key) |
100
|
|
|
{ |
101
|
4 |
|
return $this->driver->has($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritDoc CacheInterface |
106
|
|
|
*/ |
107
|
2 |
|
public function getMultiple($keys, $default = null) |
108
|
|
|
{ |
109
|
2 |
|
return $this->driver->getMultiple($keys, $default); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritDoc CacheInterface |
114
|
|
|
*/ |
115
|
6 |
|
public function setMultiple($values, $ttl = null) |
116
|
|
|
{ |
117
|
6 |
|
return $this->driver->setMultiple($values, $ttl); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritDoc CacheInterface |
122
|
|
|
*/ |
123
|
4 |
|
public function deleteMultiple($keys) |
124
|
|
|
{ |
125
|
4 |
|
return $this->driver->deleteMultiple($keys); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Create or rebuid the data schema. |
130
|
|
|
* This method is avaialbe for Mysql and Sqlite drivers. |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
4 |
|
public function rebuild(): bool |
135
|
|
|
{ |
136
|
4 |
|
if (method_exists($this->driver, 'rebuild')) { |
137
|
2 |
|
return $this->driver->rebuild(); |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
return false; |
141
|
|
|
} |
142
|
|
|
} |