1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Cache |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Cache\Driver; |
16
|
|
|
|
17
|
|
|
use Psr\Cache\CacheItemInterface; |
18
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
19
|
|
|
use Phossa2\Shared\Error\ErrorAwareTrait; |
20
|
|
|
use Phossa2\Cache\Interfaces\DriverInterface; |
21
|
|
|
use Phossa2\Shared\Error\ErrorAwareInterface; |
22
|
|
|
use Phossa2\Cache\Interfaces\CacheItemExtendedInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* DriverAbstract |
26
|
|
|
* |
27
|
|
|
* @package Phossa2\Cache |
28
|
|
|
* @author Hong Zhang <[email protected]> |
29
|
|
|
* @see ObjectAbstract |
30
|
|
|
* @see DriverInterface |
31
|
|
|
* @see ErrorAwareInterface |
32
|
|
|
* @version 2.0.0 |
33
|
|
|
* @since 2.0.0 added |
34
|
|
|
*/ |
35
|
|
|
abstract class DriverAbstract extends ObjectAbstract implements DriverInterface, ErrorAwareInterface |
36
|
|
|
{ |
37
|
|
|
use ErrorAwareTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* number of items to defer, 0 means no defer |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
* @access protected |
44
|
|
|
*/ |
45
|
|
|
protected $defer_count = 0; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* defer pool |
49
|
|
|
* |
50
|
|
|
* @var CacheItemExtendedInterface[] |
51
|
|
|
* @access protected |
52
|
|
|
*/ |
53
|
|
|
protected $defer = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $properties |
57
|
|
|
* @access public |
58
|
|
|
*/ |
59
|
|
|
public function __construct(array $properties = []) |
60
|
|
|
{ |
61
|
|
|
$this->setProperties($properties); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function has(/*# string */ $key)/*# : array */ |
68
|
|
|
{ |
69
|
|
|
$meta = $this->isDeferred($key); |
70
|
|
|
if (!empty($meta)) { |
71
|
|
|
return $meta; |
72
|
|
|
} else { |
73
|
|
|
return $this->driverHas($key); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
|
|
public function get(/*# string */ $key) |
81
|
|
|
{ |
82
|
|
|
$meta = $this->isDeferred($key); |
83
|
|
|
if (!empty($meta)) { |
84
|
|
|
return $this->getFromDeferred($key); |
85
|
|
|
} else { |
86
|
|
|
return $this->driverGet($key); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function saveDeferred(CacheItemInterface $item)/*# : bool */ |
94
|
|
|
{ |
95
|
|
|
if ($this->defer_count) { |
96
|
|
|
if ($this->flushDeferred()) { |
97
|
|
|
$this->defer[$item->getKey()] = $item; |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
return false; |
101
|
|
|
} else { |
102
|
|
|
return $this->save($item); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
public function delete(CacheItemInterface $item)/*# : bool */ |
110
|
|
|
{ |
111
|
|
|
$meta = $this->isDeferred($item->getKey()); |
112
|
|
|
if (!empty($meta)) { |
113
|
|
|
unset($this->defer[$item->getKey()]); |
114
|
|
|
} |
115
|
|
|
return $this->driverDelete($item); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritDoc} |
120
|
|
|
*/ |
121
|
|
|
public function commit()/*# : bool */ |
122
|
|
|
{ |
123
|
|
|
foreach ($this->defer as $item) { |
124
|
|
|
if (!$this->save($item)) { |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
$this->defer = []; |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
|
|
public function clear()/*# : bool */ |
136
|
|
|
{ |
137
|
|
|
$this->defer = []; |
138
|
|
|
return $this->driverClear(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
*/ |
144
|
|
|
public function purage()/*# : bool */ |
145
|
|
|
{ |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
|
|
public function ping()/*# : bool */ |
153
|
|
|
{ |
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Flush deferred items |
159
|
|
|
* |
160
|
|
|
* @access protected |
161
|
|
|
*/ |
162
|
|
|
protected function flushDeferred() |
163
|
|
|
{ |
164
|
|
|
if (count($this->defer) > $this->defer_count) { |
165
|
|
|
return $this->commit(); |
166
|
|
|
} |
167
|
|
|
return true; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* In deferred array ? |
172
|
|
|
* |
173
|
|
|
* @param string $key |
174
|
|
|
* @return array |
175
|
|
|
* @access protected |
176
|
|
|
*/ |
177
|
|
|
protected function isDeferred(/*# string */ $key)/*# : array */ |
178
|
|
|
{ |
179
|
|
|
$res = []; |
180
|
|
|
if (isset($this->defer[$key])) { |
181
|
|
|
$res['expire'] = $this->defer[$key]->getExpiration()->getTimestamp(); |
182
|
|
|
} |
183
|
|
|
return $res; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get value from deferred array |
188
|
|
|
* @param string $key |
189
|
|
|
* @return string |
190
|
|
|
* @access protected |
191
|
|
|
*/ |
192
|
|
|
protected function getFromDeferred(/*# string */ $key)/*# : string */ |
193
|
|
|
{ |
194
|
|
|
return (string) $this->defer[$key]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Driver specific has() |
199
|
|
|
* |
200
|
|
|
* @param string $key |
201
|
|
|
* @return array |
202
|
|
|
* @access protected |
203
|
|
|
*/ |
204
|
|
|
abstract protected function driverHas(/*# string */ $key)/*# : array */; |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Driver specific get() |
208
|
|
|
* |
209
|
|
|
* @param string $key |
210
|
|
|
* @return string|null |
211
|
|
|
* @access protected |
212
|
|
|
*/ |
213
|
|
|
abstract protected function driverGet(/*# string */ $key); |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Driver specific delete() |
217
|
|
|
* |
218
|
|
|
* @param CacheItemInterface $item |
219
|
|
|
* @return bool |
220
|
|
|
* @access protected |
221
|
|
|
*/ |
222
|
|
|
abstract protected function driverDelete(CacheItemInterface $item)/*# : bool */; |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Driver specific clear() |
226
|
|
|
* |
227
|
|
|
* @return bool |
228
|
|
|
* @access protected |
229
|
|
|
*/ |
230
|
|
|
abstract protected function driverClear()/*# : bool */; |
231
|
|
|
} |
232
|
|
|
|