|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of phpFastCache. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com |
|
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
declare(strict_types=1); |
|
15
|
|
|
|
|
16
|
|
|
namespace Phpfastcache\Core\Item; |
|
17
|
|
|
|
|
18
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Trait ItemBaseTrait |
|
22
|
|
|
* @package phpFastCache\Core\Item |
|
23
|
|
|
*/ |
|
24
|
|
|
trait ItemBaseTrait |
|
25
|
|
|
{ |
|
26
|
|
|
use ItemExtendedTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $fetched = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $key; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var mixed |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $data; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \DateTimeInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $expirationDate; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var \DateTimeInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $creationDate; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var \DateTimeInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $modificationDate; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $tags = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $removedTags = []; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var bool |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $isHit = false; |
|
72
|
|
|
|
|
73
|
|
|
/******************** |
|
74
|
|
|
* |
|
75
|
|
|
* PSR-6 Methods |
|
76
|
|
|
* |
|
77
|
|
|
*******************/ |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getKey() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->key; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
|
|
public function get() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->data; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param mixed $value |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
|
|
public function set($value) |
|
100
|
|
|
{ |
|
101
|
|
|
/** |
|
102
|
|
|
* The user set a value, |
|
103
|
|
|
* therefore there is no need to |
|
104
|
|
|
* fetch from source anymore |
|
105
|
|
|
*/ |
|
106
|
|
|
$this->fetched = true; |
|
107
|
|
|
$this->data = $value; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @eventName CacheSaveDeferredItem |
|
111
|
|
|
* @param ExtendedCacheItemInterface $this |
|
112
|
|
|
* @param mixed $value |
|
113
|
|
|
* |
|
114
|
|
|
*/ |
|
115
|
|
|
$this->eventManager->dispatch('CacheItemSet', $this, $value); |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function isHit(): bool |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->isHit; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param bool $isHit |
|
130
|
|
|
* @return ExtendedCacheItemInterface |
|
131
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setHit($isHit): ExtendedCacheItemInterface |
|
134
|
|
|
{ |
|
135
|
|
|
if (\is_bool($isHit)) { |
|
|
|
|
|
|
136
|
|
|
$this->isHit = $isHit; |
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
throw new PhpfastcacheInvalidArgumentException('$isHit must be a boolean'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param \DateTimeInterface $expiration |
|
146
|
|
|
* @return ExtendedCacheItemInterface |
|
147
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
|
148
|
|
|
*/ |
|
149
|
|
|
public function expiresAt($expiration): ExtendedCacheItemInterface |
|
150
|
|
|
{ |
|
151
|
|
|
if ($expiration instanceof \DateTimeInterface) { |
|
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @eventName CacheItemExpireAt |
|
154
|
|
|
* @param ExtendedCacheItemInterface $this |
|
155
|
|
|
* @param \DateTimeInterface $expiration |
|
156
|
|
|
*/ |
|
157
|
|
|
$this->eventManager->dispatch('CacheItemExpireAt', $this, $expiration); |
|
158
|
|
|
$this->expirationDate = $expiration; |
|
159
|
|
|
} else { |
|
160
|
|
|
throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param \DateInterval|int $time |
|
168
|
|
|
* @return $this |
|
169
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
|
170
|
|
|
*/ |
|
171
|
|
|
public function expiresAfter($time) |
|
172
|
|
|
{ |
|
173
|
|
|
if (\is_numeric($time)) { |
|
174
|
|
|
if ($time <= 0) { |
|
175
|
|
|
/** |
|
176
|
|
|
* 5 years, however memcached or memory cached will gone when u restart it |
|
177
|
|
|
* just recommended for sqlite. files |
|
178
|
|
|
*/ |
|
179
|
|
|
$time = 30 * 24 * 3600 * 5; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @eventName CacheItemExpireAt |
|
184
|
|
|
* @param ExtendedCacheItemInterface $this |
|
185
|
|
|
* @param \DateTimeInterface $expiration |
|
186
|
|
|
*/ |
|
187
|
|
|
$this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); |
|
188
|
|
|
|
|
189
|
|
|
$this->expirationDate = (new \DateTime())->add(new \DateInterval(\sprintf('PT%dS', $time))); |
|
190
|
|
|
} else { |
|
191
|
|
|
if ($time instanceof \DateInterval) { |
|
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @eventName CacheItemExpireAt |
|
194
|
|
|
* @param ExtendedCacheItemInterface $this |
|
195
|
|
|
* @param \DateTimeInterface $expiration |
|
196
|
|
|
*/ |
|
197
|
|
|
$this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); |
|
198
|
|
|
|
|
199
|
|
|
$this->expirationDate = (new \DateTime())->add($time); |
|
200
|
|
|
} else { |
|
201
|
|
|
throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid date format, got "%s"', \gettype($time))); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|