1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of Phpfastcache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Core\Item; |
17
|
|
|
|
18
|
|
|
use DateInterval; |
19
|
|
|
use DateTime; |
20
|
|
|
use DateTimeInterface; |
21
|
|
|
use Phpfastcache\Event\EventManagerDispatcherTrait; |
22
|
|
|
use Phpfastcache\Event\EventReferenceParameter; |
23
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
24
|
|
|
use Phpfastcache\Util\ClassNamespaceResolverTrait; |
25
|
|
|
|
26
|
|
|
trait CacheItemTrait |
27
|
|
|
{ |
28
|
|
|
use EventManagerDispatcherTrait; |
29
|
|
|
use ClassNamespaceResolverTrait; |
30
|
|
|
|
31
|
|
|
protected string $key; |
32
|
|
|
|
33
|
|
|
protected mixed $data; |
34
|
|
|
|
35
|
|
|
protected DateTimeInterface $expirationDate; |
36
|
|
|
|
37
|
|
|
protected DateTimeInterface $creationDate; |
38
|
|
|
|
39
|
|
|
protected DateTimeInterface $modificationDate; |
40
|
|
|
|
41
|
|
|
protected bool $isHit = false; |
42
|
|
|
|
43
|
|
|
public function getKey(): string |
44
|
|
|
{ |
45
|
|
|
return $this->key; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function get(): mixed |
49
|
|
|
{ |
50
|
|
|
if (!$this->isHit()) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->data; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
59
|
|
|
*/ |
60
|
|
|
public function set(mixed $value): static |
61
|
|
|
{ |
62
|
|
|
if ($value instanceof \Closure) { |
63
|
|
|
throw new PhpfastcacheInvalidArgumentException('The value set cannot be an instance of \\Closure.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @eventName CacheSaveDeferredItem |
68
|
|
|
* @param ExtendedCacheItemInterface $this |
69
|
|
|
* @param mixed $value |
70
|
|
|
* |
71
|
|
|
*/ |
72
|
|
|
$this->eventManager->dispatch('CacheItemSet', $this, new EventReferenceParameter($value, true)); |
73
|
|
|
|
74
|
|
|
$this->data = $value; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function isHit(): bool |
83
|
|
|
{ |
84
|
|
|
return $this->isHit; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param bool $isHit |
89
|
|
|
* @return ExtendedCacheItemInterface |
90
|
|
|
*/ |
91
|
|
|
public function setHit(bool $isHit): ExtendedCacheItemInterface |
92
|
|
|
{ |
93
|
|
|
$this->isHit = $isHit; |
94
|
|
|
|
95
|
|
|
return $this; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
100
|
|
|
*/ |
101
|
|
|
public function expiresAt(?\DateTimeInterface $expiration): static |
102
|
|
|
{ |
103
|
|
|
if ($expiration instanceof DateTimeInterface) { |
104
|
|
|
/** |
105
|
|
|
* @eventName CacheItemExpireAt |
106
|
|
|
* @param ExtendedCacheItemInterface $this |
107
|
|
|
* @param DateTimeInterface $expiration |
108
|
|
|
*/ |
109
|
|
|
$this->eventManager->dispatch('CacheItemExpireAt', $this, $expiration); |
110
|
|
|
$this->expirationDate = $expiration; |
111
|
|
|
} else { |
112
|
|
|
throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return $this |
120
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
121
|
|
|
* @throws \Exception |
122
|
|
|
*/ |
123
|
|
|
public function expiresAfter(int|\DateInterval|null $time): static |
124
|
|
|
{ |
125
|
|
|
if (\is_numeric($time)) { |
126
|
|
|
if ($time <= 0) { |
127
|
|
|
/** |
128
|
|
|
* 5 years, however memcached or memory cached will gone when u restart it |
129
|
|
|
* just recommended for sqlite. files |
130
|
|
|
*/ |
131
|
|
|
$time = 30 * 24 * 3600 * 5; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @eventName CacheItemExpireAt |
136
|
|
|
* @param ExtendedCacheItemInterface $this |
137
|
|
|
* @param DateTimeInterface $expiration |
138
|
|
|
*/ |
139
|
|
|
$this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); |
140
|
|
|
|
141
|
|
|
$this->expirationDate = (new DateTime())->add(new DateInterval(\sprintf('PT%dS', $time))); |
142
|
|
|
} elseif ($time instanceof DateInterval) { |
143
|
|
|
/** |
144
|
|
|
* @eventName CacheItemExpireAt |
145
|
|
|
* @param ExtendedCacheItemInterface $this |
146
|
|
|
* @param DateTimeInterface $expiration |
147
|
|
|
*/ |
148
|
|
|
$this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); |
149
|
|
|
|
150
|
|
|
$this->expirationDate = (new DateTime())->add($time); |
151
|
|
|
} else { |
152
|
|
|
throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid date format, got "%s"', \gettype($time))); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|