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
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phpfastcache\Core\Item; |
18
|
|
|
|
19
|
|
|
use DateInterval; |
20
|
|
|
use DateTime; |
21
|
|
|
use DateTimeInterface; |
22
|
|
|
use Phpfastcache\Event\Event; |
23
|
|
|
use Phpfastcache\Event\EventManagerDispatcherTrait; |
24
|
|
|
use Phpfastcache\Event\EventReferenceParameter; |
25
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
26
|
|
|
use Phpfastcache\Util\ClassNamespaceResolverTrait; |
27
|
|
|
|
28
|
|
|
trait CacheItemTrait |
29
|
|
|
{ |
30
|
|
|
use EventManagerDispatcherTrait; |
31
|
|
|
use ClassNamespaceResolverTrait; |
32
|
|
|
|
33
|
|
|
protected string $key; |
34
|
|
|
|
35
|
|
|
protected mixed $data; |
36
|
|
|
|
37
|
|
|
protected DateTimeInterface $expirationDate; |
38
|
|
|
|
39
|
|
|
protected DateTimeInterface $creationDate; |
40
|
|
|
|
41
|
|
|
protected DateTimeInterface $modificationDate; |
42
|
|
|
|
43
|
|
|
protected bool $isHit = false; |
44
|
|
|
|
45
|
|
|
public function getKey(): string |
46
|
|
|
{ |
47
|
|
|
return $this->key; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function get(): mixed |
51
|
|
|
{ |
52
|
|
|
if (!$this->isHit()) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->data; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
61
|
|
|
*/ |
62
|
|
|
public function set(mixed $value): static |
63
|
|
|
{ |
64
|
|
|
if ($value instanceof \Closure) { |
65
|
|
|
throw new PhpfastcacheInvalidArgumentException('The value set cannot be an instance of \\Closure.'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (\is_resource($value)) { |
69
|
|
|
throw new PhpfastcacheInvalidArgumentException('The value set cannot be a resource'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->eventManager->dispatch(Event::CACHE_ITEM_SET, $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
|
|
|
$this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AT, $this, $expiration); |
105
|
|
|
$this->expirationDate = $this->demutateDatetime($expiration); |
106
|
|
|
} else { |
107
|
|
|
throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return $this |
115
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
116
|
|
|
* @throws \Exception |
117
|
|
|
*/ |
118
|
|
|
public function expiresAfter(int|\DateInterval|null $time): static |
119
|
|
|
{ |
120
|
|
|
if (\is_numeric($time)) { |
121
|
|
|
if ($time <= 0) { |
122
|
|
|
/** |
123
|
|
|
* 5 years, however memcached or memory cached will be gone when u restart it |
124
|
|
|
* just recommended for sqlite. files |
125
|
|
|
*/ |
126
|
|
|
$time = 30 * 24 * 3600 * 5; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); |
130
|
|
|
|
131
|
|
|
$this->expirationDate = (new DateTime())->add(new DateInterval(\sprintf('PT%dS', $time))); |
132
|
|
|
} elseif ($time instanceof DateInterval) { |
133
|
|
|
$this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); |
134
|
|
|
|
135
|
|
|
$this->expirationDate = (new DateTime())->add($time); |
136
|
|
|
} else { |
137
|
|
|
throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid date format, got "%s"', \gettype($time))); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function demutateDatetime(\DateTimeInterface $dateTime): \DateTimeInterface |
144
|
|
|
{ |
145
|
|
|
return $dateTime instanceof \DateTimeImmutable |
146
|
|
|
? \DateTime::createFromImmutable($dateTime) |
147
|
|
|
: $dateTime; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|