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
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The base Cache Adapter class. |
20
|
|
|
*/ |
21
|
|
|
class Cache |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The cache driver. |
25
|
|
|
* |
26
|
|
|
* @var null|CacheInterface |
27
|
|
|
*/ |
28
|
|
|
protected $driver; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param string|CacheInterface $driver The cache driver. |
34
|
|
|
* @param array $settings The settings. |
35
|
|
|
* |
36
|
|
|
* @throws CacheException |
37
|
|
|
*/ |
38
|
26 |
|
public function __construct($driver = '', array $settings = []) |
39
|
|
|
{ |
40
|
26 |
|
if ($driver instanceof CacheInterface) { |
41
|
2 |
|
$this->driver = $driver; |
42
|
|
|
|
43
|
24 |
|
} elseif (is_string($driver)) { |
|
|
|
|
44
|
24 |
|
$class = ucfirst(strtolower($driver)); |
45
|
|
|
|
46
|
24 |
|
if (file_exists(__DIR__ . '/Driver/' . $class . '.php')) { |
47
|
22 |
|
$class = '\Shieldon\SimpleCache\Driver\\' . $class; |
48
|
|
|
|
49
|
22 |
|
$this->driver = new $class($settings); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
22 |
|
if (!$this->driver) { |
54
|
2 |
|
throw new CacheArgumentException( |
55
|
2 |
|
'The data driver is not set correctly.' |
56
|
|
|
); |
57
|
|
|
} |
58
|
20 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc CacheInterface |
62
|
|
|
*/ |
63
|
4 |
|
public function get($key, $default = null) |
64
|
|
|
{ |
65
|
4 |
|
return $this->driver->get($key, $default); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc CacheInterface |
70
|
|
|
*/ |
71
|
6 |
|
public function set($key, $value, $ttl = null) |
72
|
|
|
{ |
73
|
6 |
|
return $this->driver->set($key, $value, $ttl); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc CacheInterface |
78
|
|
|
*/ |
79
|
2 |
|
public function delete($key) |
80
|
|
|
{ |
81
|
2 |
|
return $this->driver->delete($key); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc CacheInterface |
86
|
|
|
*/ |
87
|
2 |
|
public function clear() |
88
|
|
|
{ |
89
|
2 |
|
return $this->driver->clear(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc CacheInterface |
94
|
|
|
*/ |
95
|
4 |
|
public function has($key) |
96
|
|
|
{ |
97
|
4 |
|
return $this->driver->has($key); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritDoc CacheInterface |
102
|
|
|
*/ |
103
|
2 |
|
public function getMultiple($keys, $default = null) |
104
|
|
|
{ |
105
|
2 |
|
return $this->driver->getMultiple($keys, $default); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc CacheInterface |
110
|
|
|
*/ |
111
|
6 |
|
public function setMultiple($values, $ttl = null) |
112
|
|
|
{ |
113
|
6 |
|
return $this->driver->setMultiple($values, $ttl); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritDoc CacheInterface |
118
|
|
|
*/ |
119
|
4 |
|
public function deleteMultiple($keys) |
120
|
|
|
{ |
121
|
4 |
|
return $this->driver->deleteMultiple($keys); |
122
|
|
|
} |
123
|
|
|
} |