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; |
16
|
|
|
|
17
|
|
|
use Phossa2\Storage\Storage; |
18
|
|
|
use Phossa2\Storage\Filesystem; |
19
|
|
|
use Psr\Cache\CacheItemInterface; |
20
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
21
|
|
|
use Phossa2\Storage\Driver\LocalDriver; |
22
|
|
|
use Phossa2\Cache\Driver\StorageDriver; |
23
|
|
|
use Phossa2\Shared\Error\ErrorAwareTrait; |
24
|
|
|
use Phossa2\Cache\Traits\DriverAwareTrait; |
25
|
|
|
use Phossa2\Cache\Traits\UtilityAwareTrait; |
26
|
|
|
use Phossa2\Cache\Traits\FallbackAwareTrait; |
27
|
|
|
use Phossa2\Shared\Error\ErrorAwareInterface; |
28
|
|
|
use Phossa2\Cache\Traits\CacheItemAwareTrait; |
29
|
|
|
use Phossa2\Cache\Interfaces\DriverInterface; |
30
|
|
|
use Phossa2\Cache\Interfaces\DriverAwareInterface; |
31
|
|
|
use Phossa2\Cache\Interfaces\UtilityAwareInterface; |
32
|
|
|
use Phossa2\Cache\Interfaces\FallbackAwareInterface; |
33
|
|
|
use Phossa2\Event\EventableExtensionCapableAbstract; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* CachePool |
37
|
|
|
* |
38
|
|
|
* @package Phossa2\Cache |
39
|
|
|
* @author Hong Zhang <[email protected]> |
40
|
|
|
* @see EventableExtensionCapableAbstrac |
41
|
|
|
* @see CacheItemPoolInterface |
42
|
|
|
* @see DriverAwareInterface |
43
|
|
|
* @see ErrorAwareInterface |
44
|
|
|
* @version 2.0.1 |
45
|
|
|
* @since 2.0.0 added |
46
|
|
|
* @since 2.0.1 updated __construct() |
47
|
|
|
* moved to EventableExtensionCapableAbstrac |
48
|
|
|
*/ |
49
|
|
|
class CachePool extends EventableExtensionCapableAbstract implements CacheItemPoolInterface, DriverAwareInterface, FallbackAwareInterface, UtilityAwareInterface, ErrorAwareInterface |
50
|
|
|
{ |
51
|
|
|
use DriverAwareTrait, CacheItemAwareTrait, UtilityAwareTrait, FallbackAwareTrait, ErrorAwareTrait; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* event names |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
const EVENT_HAS_BEFORE = 'cache.has.before'; |
58
|
|
|
const EVENT_HAS_AFTER = 'cache.has.after'; |
59
|
|
|
const EVENT_GET_BEFORE = 'cache.get.before'; |
60
|
|
|
const EVENT_GET_AFTER = 'cache.get.after'; |
61
|
|
|
const EVENT_SET_BEFORE = 'cache.set.before'; |
62
|
|
|
const EVENT_SET_AFTER = 'cache.set.after'; |
63
|
|
|
const EVENT_CLEAR_BEFORE = 'cache.clear.before'; |
64
|
|
|
const EVENT_CLEAR_AFTER = 'cache.clear.after'; |
65
|
|
|
const EVENT_DELETE_BEFORE = 'cache.delete.before'; |
66
|
|
|
const EVENT_DELETE_AFTER = 'cache.delete.after'; |
67
|
|
|
const EVENT_SAVE_BEFORE = 'cache.save.before'; |
68
|
|
|
const EVENT_SAVE_AFTER = 'cache.save.after'; |
69
|
|
|
const EVENT_DEFER_BEFORE = 'cache.defer.before'; |
70
|
|
|
const EVENT_DEFER_AFTER = 'cache.defer.after'; |
71
|
|
|
const EVENT_COMMIT_BEFORE = 'cache.commit.before'; |
72
|
|
|
const EVENT_COMMIT_AFTER = 'cache.commit.after'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor |
76
|
|
|
* |
77
|
|
|
* @param DriverInterface $driver |
78
|
|
|
* @access public |
79
|
|
|
* @since 2.0.1 removed eventManager param |
80
|
|
|
*/ |
81
|
|
|
public function __construct(DriverInterface $driver = null) |
82
|
|
|
{ |
83
|
|
|
// use default driver |
84
|
|
|
if (is_null($driver)) { |
85
|
|
|
$driver = new StorageDriver( |
86
|
|
|
new Storage('/', new Filesystem(new LocalDriver(sys_get_temp_dir()))), |
87
|
|
|
'/cache' |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
$this->setDriver($driver); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Destructor |
95
|
|
|
* |
96
|
|
|
* @access public |
97
|
|
|
*/ |
98
|
|
|
public function __destruct() |
99
|
|
|
{ |
100
|
|
|
$this->commit(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
*/ |
106
|
|
|
public function getItem($key) |
107
|
|
|
{ |
108
|
|
|
return $this->getCacheItem($key); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
|
|
public function getItems(array $keys = array()) |
115
|
|
|
{ |
116
|
|
|
$result = []; |
117
|
|
|
foreach ($keys as $key) { |
118
|
|
|
$result[$key] = $this->getItem($key); |
119
|
|
|
} |
120
|
|
|
return $result; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
|
|
public function hasItem($key) |
127
|
|
|
{ |
128
|
|
|
return $this->getCacheItem($key)->isHit(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
|
|
public function clear() |
135
|
|
|
{ |
136
|
|
|
return $this->eventableAction( |
137
|
|
|
'clear', |
138
|
|
|
self::EVENT_CLEAR_BEFORE, |
139
|
|
|
self::EVENT_CLEAR_AFTER |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritDoc} |
145
|
|
|
*/ |
146
|
|
|
public function deleteItem($key) |
147
|
|
|
{ |
148
|
|
|
$item = $this->getItem($key); |
149
|
|
|
return $this->eventableAction( |
150
|
|
|
'delete', |
151
|
|
|
self::EVENT_DELETE_BEFORE, |
152
|
|
|
self::EVENT_DELETE_AFTER, |
153
|
|
|
$item |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritDoc} |
159
|
|
|
*/ |
160
|
|
|
public function deleteItems(array $keys) |
161
|
|
|
{ |
162
|
|
|
foreach ($keys as $key) { |
163
|
|
|
if (!$this->deleteItem($key)) { |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
return $this->flushError(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritDoc} |
172
|
|
|
*/ |
173
|
|
|
public function save(CacheItemInterface $item) |
174
|
|
|
{ |
175
|
|
|
return $this->eventableAction( |
176
|
|
|
'save', |
177
|
|
|
self::EVENT_SAVE_BEFORE, |
178
|
|
|
self::EVENT_SAVE_AFTER, |
179
|
|
|
$item |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritDoc} |
185
|
|
|
*/ |
186
|
|
|
public function saveDeferred(CacheItemInterface $item) |
187
|
|
|
{ |
188
|
|
|
return $this->eventableAction( |
189
|
|
|
'saveDeferred', |
190
|
|
|
self::EVENT_DEFER_BEFORE, |
191
|
|
|
self::EVENT_DEFER_AFTER, |
192
|
|
|
$item |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritDoc} |
198
|
|
|
*/ |
199
|
|
|
public function commit() |
200
|
|
|
{ |
201
|
|
|
return $this->eventableAction( |
202
|
|
|
'commit', |
203
|
|
|
self::EVENT_COMMIT_BEFORE, |
204
|
|
|
self::EVENT_COMMIT_AFTER |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Execute an action |
210
|
|
|
* |
211
|
|
|
* @param string $action |
212
|
|
|
* @param string $beforeEvent |
213
|
|
|
* @param string $afterEvent |
214
|
|
|
* @param CacheItemInterface $item |
215
|
|
|
* @return bool |
216
|
|
|
* @access protected |
217
|
|
|
*/ |
218
|
|
|
protected function eventableAction( |
219
|
|
|
/*# string */ $action, |
220
|
|
|
/*# string */ $beforeEvent, |
221
|
|
|
/*# string */ $afterEvent, |
222
|
|
|
CacheItemInterface $item = null |
223
|
|
|
)/*# : bool */ { |
224
|
|
|
$param = ['item' => $item]; |
225
|
|
|
if (!$this->trigger($beforeEvent, $param)) { |
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
if (!$this->driverAction($action, $item)) { |
229
|
|
|
$this->copyError($this->getDriver()); |
230
|
|
|
return false; |
231
|
|
|
} |
232
|
|
|
if (!$this->trigger($afterEvent, $param)) { |
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
return $this->flushError(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Execute an action with the driver |
240
|
|
|
* |
241
|
|
|
* @param string $action |
242
|
|
|
* @param CacheItemInterface|null $item |
243
|
|
|
* @access protected |
244
|
|
|
*/ |
245
|
|
|
protected function driverAction(/*# string */ $action, $item)/*# : bool */ |
246
|
|
|
{ |
247
|
|
|
if (is_null($item)) { |
248
|
|
|
return $this->getDriver()->{$action}(); |
249
|
|
|
} else { |
250
|
|
|
return $this->getDriver()->{$action}($item); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|