-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachePoolAbstract.php
executable file
·218 lines (195 loc) · 5.37 KB
/
CachePoolAbstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace MaplePHP\Cache;
use MaplePHP\Cache\Interfaces\CacheItemInterface;
use MaplePHP\Cache\Interfaces\CacheItemPoolInterface;
use MaplePHP\Http\Interfaces\StreamInterface;
use MaplePHP\Cache\Exceptions\InvalidArgumentException;
use MaplePHP\Cache\CacheItem;
abstract class CachePoolAbstract implements CacheItemPoolInterface
{
private $items = array();
private $timestamp;
/**
* HANDLER: @setItems and the pass on to @getItems pool
* @param CacheItemInterface $item
* @return void
*/
abstract protected function setItem(CacheItemInterface $item): void;
/**
* HANDLER: @setSave items and the pass on to @save pool
* @param CacheItemInterface $item
* @return bool
*/
abstract protected function setSave(CacheItemInterface $item): bool;
/**
* HANDLER: @setDelete and the pass on to @delete pool
* @param CacheItemInterface $item
* @return bool
*/
abstract protected function setDelete($key): bool;
/**
* HANDLER: @setClear and the pass on to @clear pool
* @return bool
*/
abstract protected function setClear(): bool;
/**
* Get cache item instance
* @param string $key
* @return CacheItemInterface
*/
final public function getItem(string $key): CacheItemInterface
{
if (!isset($this->items[$key])) {
$this->validateKey($key);
$this->items[$key] = new CacheItem($key);
$this->setItem($this->items[$key]);
$value = $this->items[$key]->get();
if (!is_null($value) && $this->hasItemExpired($this->items[$key])) {
$this->items[$key] = new CacheItem($key);
}
}
return $this->items[$key];
}
/**
* Get multiple cache items
* @param array $keys [description]
* @return iterable
*/
public function getItems(array $keys = []): iterable
{
$items = array();
foreach ($keys as $key) {
$items[$key] = $this->getItem($key);
}
return $items;
}
/**
* Check if cache item exists
* @param string $key
* @return boolean
*/
public function hasItem(string $key): bool
{
$item = $this->getItem($key);
return ($item->isHit() || !is_null($item->get()));
}
/**
* Delete a cache item and data
* @param string $key
* @return bool
*/
public function deleteItem($key): bool
{
$this->validateKey($key);
if (isset($this->items[$key])) {
unset($this->items[$key]);
}
return $this->setDelete($key);
}
/**
* Delete multiple cache items and data
* @param array $key
* @return bool
*/
public function deleteItems(array $keys): bool
{
foreach ($keys as $key) {
if (!$this->deleteItem($key)) {
return false;
}
}
return true;
}
/**
* Clear and remove all cache items and data
* @return bool
*/
public function clear(): bool
{
$this->items = array();
return $this->setClear();
}
/**
* Save to cache file
* @param CacheItemInterface $item
* @return bool
*/
public function save(CacheItemInterface $item): bool
{
if ($this->saveDeferred($item)) {
if (!$this->setSave($item)) {
return false;
}
}
return true;
}
/**
* Prepare save (wont be saved before commit is trggered)
* @param CacheItemInterface $item
* @return bool
*/
public function saveDeferred(CacheItemInterface $item): bool
{
$value = $item->get();
if (!is_null($value)) {
if ($value instanceof StreamInterface) {
$value->seek(0);
$value->read((int)$value->getSize());
//$value = $value->read($value->getSize());
}
return true;
}
return false;
}
/**
* Commit prepared saves
* @return bool
*/
public function commit(): bool
{
foreach ($this->items as $item) {
if (!$this->save($item)) {
return false;
}
}
return true;
}
public function setExpiration(CacheItemInterface $item)
{
return ($item->getExpiration() > 0) ? time() + $item->getExpiration() : 0;
}
/**
* Check if cache file has expired
* @param CacheItemInterface $item
* @return bool
*/
final public function hasItemExpired(CacheItemInterface $item): bool
{
$expiration = $item->getExpiration();
return ($expiration > 0 && $expiration < $this->now());
}
/**
* Get curret timestamp
* @return int
*/
final public function now(): int
{
if (is_null($this->timestamp)) {
$dateTime = new \DateTime("now");
$this->timestamp = $dateTime->getTimestamp();
}
return $this->timestamp;
}
/**
* Validate the cache key
* @param string $key
* @return void
*/
final public function validateKey(string $key): void
{
if (!preg_match('/^[a-zA-Z0-9_\-.]+$/', $key)) {
throw new InvalidArgumentException('Invalid cache key. Only alphanumeric characters, ' .
'underscores, and dots are allowed.');
}
}
}