@@ -21,102 +21,102 @@ |
||
21 | 21 | |
22 | 22 | interface TaggableCacheItemInterface |
23 | 23 | { |
24 | - /** |
|
25 | - * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
26 | - * Default behavior |
|
27 | - */ |
|
28 | - public const TAG_STRATEGY_ONE = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE; |
|
29 | - |
|
30 | - /** |
|
31 | - * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
32 | - * The cache item *CAN* have additional tag(s) |
|
33 | - */ |
|
34 | - public const TAG_STRATEGY_ALL = TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL; |
|
35 | - |
|
36 | - /** |
|
37 | - * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
38 | - * The cache item *CANNOT* have additional tag(s) |
|
39 | - */ |
|
40 | - public const TAG_STRATEGY_ONLY = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY; |
|
41 | - |
|
42 | - /** |
|
43 | - * @param string $tagName |
|
44 | - * |
|
45 | - * @return ExtendedCacheItemInterface |
|
46 | - * @throws PhpfastcacheInvalidArgumentException |
|
47 | - */ |
|
48 | - public function addTag(string $tagName): ExtendedCacheItemInterface; |
|
49 | - |
|
50 | - /** |
|
51 | - * @param string[] $tagNames |
|
52 | - * |
|
53 | - * @return ExtendedCacheItemInterface |
|
54 | - * @throws PhpfastcacheInvalidArgumentException |
|
55 | - */ |
|
56 | - public function addTags(array $tagNames): ExtendedCacheItemInterface; |
|
57 | - |
|
58 | - /** |
|
59 | - * @param string $tagName |
|
60 | - * |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - public function hasTag(string $tagName): bool; |
|
64 | - |
|
65 | - /** |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function isTagged(): bool; |
|
69 | - |
|
70 | - /** |
|
71 | - * @param string[] $tagNames |
|
72 | - * @param int $strategy |
|
73 | - * @return bool |
|
74 | - * @throws PhpfastcacheInvalidArgumentException |
|
75 | - */ |
|
76 | - public function hasTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
77 | - |
|
78 | - /** |
|
79 | - * @param string[] $tags |
|
80 | - * |
|
81 | - * @return ExtendedCacheItemInterface |
|
82 | - * @throws PhpfastcacheInvalidArgumentException |
|
83 | - */ |
|
84 | - public function setTags(array $tags): ExtendedCacheItemInterface; |
|
85 | - |
|
86 | - /** |
|
87 | - * @return string[] |
|
88 | - */ |
|
89 | - public function getTags(): array; |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string $separator |
|
93 | - * |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - public function getTagsAsString(string $separator = ', '): string; |
|
97 | - |
|
98 | - /** |
|
99 | - * @param string $tagName |
|
100 | - * |
|
101 | - * @return ExtendedCacheItemInterface |
|
102 | - * @throws PhpfastcacheInvalidArgumentException |
|
103 | - */ |
|
104 | - public function removeTag(string $tagName): ExtendedCacheItemInterface; |
|
105 | - |
|
106 | - /** |
|
107 | - * @param string[] $tagNames |
|
108 | - * |
|
109 | - * @return ExtendedCacheItemInterface |
|
110 | - */ |
|
111 | - public function removeTags(array $tagNames): ExtendedCacheItemInterface; |
|
112 | - |
|
113 | - /** |
|
114 | - * @return string[] |
|
115 | - */ |
|
116 | - public function getRemovedTags(): array; |
|
117 | - |
|
118 | - /** |
|
119 | - * @return ExtendedCacheItemInterface |
|
120 | - */ |
|
121 | - public function clearRemovedTags(): ExtendedCacheItemInterface; |
|
24 | + /** |
|
25 | + * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
26 | + * Default behavior |
|
27 | + */ |
|
28 | + public const TAG_STRATEGY_ONE = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE; |
|
29 | + |
|
30 | + /** |
|
31 | + * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
32 | + * The cache item *CAN* have additional tag(s) |
|
33 | + */ |
|
34 | + public const TAG_STRATEGY_ALL = TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL; |
|
35 | + |
|
36 | + /** |
|
37 | + * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
38 | + * The cache item *CANNOT* have additional tag(s) |
|
39 | + */ |
|
40 | + public const TAG_STRATEGY_ONLY = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY; |
|
41 | + |
|
42 | + /** |
|
43 | + * @param string $tagName |
|
44 | + * |
|
45 | + * @return ExtendedCacheItemInterface |
|
46 | + * @throws PhpfastcacheInvalidArgumentException |
|
47 | + */ |
|
48 | + public function addTag(string $tagName): ExtendedCacheItemInterface; |
|
49 | + |
|
50 | + /** |
|
51 | + * @param string[] $tagNames |
|
52 | + * |
|
53 | + * @return ExtendedCacheItemInterface |
|
54 | + * @throws PhpfastcacheInvalidArgumentException |
|
55 | + */ |
|
56 | + public function addTags(array $tagNames): ExtendedCacheItemInterface; |
|
57 | + |
|
58 | + /** |
|
59 | + * @param string $tagName |
|
60 | + * |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + public function hasTag(string $tagName): bool; |
|
64 | + |
|
65 | + /** |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function isTagged(): bool; |
|
69 | + |
|
70 | + /** |
|
71 | + * @param string[] $tagNames |
|
72 | + * @param int $strategy |
|
73 | + * @return bool |
|
74 | + * @throws PhpfastcacheInvalidArgumentException |
|
75 | + */ |
|
76 | + public function hasTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
77 | + |
|
78 | + /** |
|
79 | + * @param string[] $tags |
|
80 | + * |
|
81 | + * @return ExtendedCacheItemInterface |
|
82 | + * @throws PhpfastcacheInvalidArgumentException |
|
83 | + */ |
|
84 | + public function setTags(array $tags): ExtendedCacheItemInterface; |
|
85 | + |
|
86 | + /** |
|
87 | + * @return string[] |
|
88 | + */ |
|
89 | + public function getTags(): array; |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string $separator |
|
93 | + * |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + public function getTagsAsString(string $separator = ', '): string; |
|
97 | + |
|
98 | + /** |
|
99 | + * @param string $tagName |
|
100 | + * |
|
101 | + * @return ExtendedCacheItemInterface |
|
102 | + * @throws PhpfastcacheInvalidArgumentException |
|
103 | + */ |
|
104 | + public function removeTag(string $tagName): ExtendedCacheItemInterface; |
|
105 | + |
|
106 | + /** |
|
107 | + * @param string[] $tagNames |
|
108 | + * |
|
109 | + * @return ExtendedCacheItemInterface |
|
110 | + */ |
|
111 | + public function removeTags(array $tagNames): ExtendedCacheItemInterface; |
|
112 | + |
|
113 | + /** |
|
114 | + * @return string[] |
|
115 | + */ |
|
116 | + public function getRemovedTags(): array; |
|
117 | + |
|
118 | + /** |
|
119 | + * @return ExtendedCacheItemInterface |
|
120 | + */ |
|
121 | + public function clearRemovedTags(): ExtendedCacheItemInterface; |
|
122 | 122 | } |
@@ -22,171 +22,171 @@ |
||
22 | 22 | |
23 | 23 | trait TaggableCacheItemTrait |
24 | 24 | { |
25 | - use ExtendedCacheItemTrait; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var string[] |
|
29 | - */ |
|
30 | - protected array $tags = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var string[] |
|
34 | - */ |
|
35 | - protected array $removedTags = []; |
|
36 | - |
|
37 | - /** |
|
38 | - * @param string[] $tagNames |
|
39 | - * @return ExtendedCacheItemInterface |
|
40 | - */ |
|
41 | - public function addTags(array $tagNames): ExtendedCacheItemInterface |
|
42 | - { |
|
43 | - foreach ($tagNames as $tagName) { |
|
44 | - $this->addTag($tagName); |
|
45 | - } |
|
46 | - |
|
47 | - return $this; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * @param string $tagName |
|
52 | - * @return ExtendedCacheItemInterface |
|
53 | - */ |
|
54 | - public function addTag(string $tagName): ExtendedCacheItemInterface |
|
55 | - { |
|
56 | - $this->tags = \array_unique(\array_merge($this->tags, [$tagName])); |
|
57 | - |
|
58 | - return $this; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @param string $tagName |
|
63 | - * |
|
64 | - * @return bool |
|
65 | - */ |
|
66 | - public function hasTag(string $tagName): bool |
|
67 | - { |
|
68 | - return \in_array($tagName, $this->tags, true); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return bool |
|
73 | - */ |
|
74 | - public function isTagged(): bool |
|
75 | - { |
|
76 | - return !empty($this->tags); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @param string[] $tagNames |
|
81 | - * @param int $strategy |
|
82 | - * @return bool |
|
83 | - * @throws PhpfastcacheInvalidArgumentException |
|
84 | - */ |
|
85 | - public function hasTags(array $tagNames, int $strategy = TaggableCacheItemInterface::TAG_STRATEGY_ONE): bool |
|
86 | - { |
|
87 | - return match ($strategy) { |
|
88 | - TaggableCacheItemInterface::TAG_STRATEGY_ONE => !empty(array_intersect($tagNames, $this->tags)), |
|
89 | - TaggableCacheItemInterface::TAG_STRATEGY_ALL => empty(\array_diff($tagNames, $this->tags)), |
|
90 | - TaggableCacheItemInterface::TAG_STRATEGY_ONLY => empty(\array_diff($tagNames, $this->tags)) && empty(\array_diff($this->tags, $tagNames)), |
|
91 | - default => throw new PhpfastcacheInvalidArgumentException('Invalid strategy constant provided.'), |
|
92 | - }; |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * @param string[] $tags |
|
98 | - * @return ExtendedCacheItemInterface |
|
99 | - * @throws PhpfastcacheInvalidArgumentException |
|
100 | - */ |
|
101 | - public function setTags(array $tags): ExtendedCacheItemInterface |
|
102 | - { |
|
103 | - if ($tags === [] || \array_filter($tags, 'is_string')) { |
|
104 | - $this->tags = $tags; |
|
105 | - } else { |
|
106 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string'); |
|
107 | - } |
|
108 | - |
|
109 | - return $this; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @return string[] |
|
114 | - */ |
|
115 | - public function getTags(): array |
|
116 | - { |
|
117 | - return $this->tags; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @param string $separator |
|
122 | - * @return string |
|
123 | - */ |
|
124 | - public function getTagsAsString(string $separator = ', '): string |
|
125 | - { |
|
126 | - return \implode($separator, $this->tags); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @param string[] $tagNames |
|
131 | - * @return ExtendedCacheItemInterface |
|
132 | - */ |
|
133 | - public function removeTags(array $tagNames): ExtendedCacheItemInterface |
|
134 | - { |
|
135 | - foreach ($tagNames as $tagName) { |
|
136 | - $this->removeTag($tagName); |
|
137 | - } |
|
138 | - |
|
139 | - return $this; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param string $tagName |
|
144 | - * @return ExtendedCacheItemInterface |
|
145 | - */ |
|
146 | - public function removeTag(string $tagName): ExtendedCacheItemInterface |
|
147 | - { |
|
148 | - if (($key = \array_search($tagName, $this->tags, true)) !== false) { |
|
149 | - unset($this->tags[$key]); |
|
150 | - $this->removedTags[] = $tagName; |
|
151 | - } |
|
152 | - |
|
153 | - return $this; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @return string[] |
|
158 | - */ |
|
159 | - public function getRemovedTags(): array |
|
160 | - { |
|
161 | - return \array_diff($this->removedTags, $this->tags); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @return ExtendedCacheItemInterface |
|
166 | - */ |
|
167 | - public function clearRemovedTags(): ExtendedCacheItemInterface |
|
168 | - { |
|
169 | - $this->removedTags = []; |
|
170 | - |
|
171 | - return $this; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @throws PhpfastcacheLogicException |
|
176 | - * @throws PhpfastcacheInvalidArgumentException |
|
177 | - */ |
|
178 | - public function cloneInto(ExtendedCacheItemInterface $itemTarget, ?ExtendedCacheItemPoolInterface $itemPoolTarget = null): void |
|
179 | - { |
|
180 | - $itemTarget->setEventManager($this->getEventManager()) |
|
181 | - ->set($this->_getData()) |
|
182 | - ->setHit($this->isHit()) |
|
183 | - ->setTags($this->getTags()) |
|
184 | - ->expiresAt(clone $this->getExpirationDate()) |
|
185 | - ->setDriver($itemPoolTarget ?? $this->driver); |
|
186 | - |
|
187 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
188 | - $itemTarget->setCreationDate(clone $this->getCreationDate()) |
|
189 | - ->setModificationDate(clone $this->getModificationDate()); |
|
190 | - } |
|
191 | - } |
|
25 | + use ExtendedCacheItemTrait; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var string[] |
|
29 | + */ |
|
30 | + protected array $tags = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var string[] |
|
34 | + */ |
|
35 | + protected array $removedTags = []; |
|
36 | + |
|
37 | + /** |
|
38 | + * @param string[] $tagNames |
|
39 | + * @return ExtendedCacheItemInterface |
|
40 | + */ |
|
41 | + public function addTags(array $tagNames): ExtendedCacheItemInterface |
|
42 | + { |
|
43 | + foreach ($tagNames as $tagName) { |
|
44 | + $this->addTag($tagName); |
|
45 | + } |
|
46 | + |
|
47 | + return $this; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * @param string $tagName |
|
52 | + * @return ExtendedCacheItemInterface |
|
53 | + */ |
|
54 | + public function addTag(string $tagName): ExtendedCacheItemInterface |
|
55 | + { |
|
56 | + $this->tags = \array_unique(\array_merge($this->tags, [$tagName])); |
|
57 | + |
|
58 | + return $this; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @param string $tagName |
|
63 | + * |
|
64 | + * @return bool |
|
65 | + */ |
|
66 | + public function hasTag(string $tagName): bool |
|
67 | + { |
|
68 | + return \in_array($tagName, $this->tags, true); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return bool |
|
73 | + */ |
|
74 | + public function isTagged(): bool |
|
75 | + { |
|
76 | + return !empty($this->tags); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @param string[] $tagNames |
|
81 | + * @param int $strategy |
|
82 | + * @return bool |
|
83 | + * @throws PhpfastcacheInvalidArgumentException |
|
84 | + */ |
|
85 | + public function hasTags(array $tagNames, int $strategy = TaggableCacheItemInterface::TAG_STRATEGY_ONE): bool |
|
86 | + { |
|
87 | + return match ($strategy) { |
|
88 | + TaggableCacheItemInterface::TAG_STRATEGY_ONE => !empty(array_intersect($tagNames, $this->tags)), |
|
89 | + TaggableCacheItemInterface::TAG_STRATEGY_ALL => empty(\array_diff($tagNames, $this->tags)), |
|
90 | + TaggableCacheItemInterface::TAG_STRATEGY_ONLY => empty(\array_diff($tagNames, $this->tags)) && empty(\array_diff($this->tags, $tagNames)), |
|
91 | + default => throw new PhpfastcacheInvalidArgumentException('Invalid strategy constant provided.'), |
|
92 | + }; |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * @param string[] $tags |
|
98 | + * @return ExtendedCacheItemInterface |
|
99 | + * @throws PhpfastcacheInvalidArgumentException |
|
100 | + */ |
|
101 | + public function setTags(array $tags): ExtendedCacheItemInterface |
|
102 | + { |
|
103 | + if ($tags === [] || \array_filter($tags, 'is_string')) { |
|
104 | + $this->tags = $tags; |
|
105 | + } else { |
|
106 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string'); |
|
107 | + } |
|
108 | + |
|
109 | + return $this; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @return string[] |
|
114 | + */ |
|
115 | + public function getTags(): array |
|
116 | + { |
|
117 | + return $this->tags; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @param string $separator |
|
122 | + * @return string |
|
123 | + */ |
|
124 | + public function getTagsAsString(string $separator = ', '): string |
|
125 | + { |
|
126 | + return \implode($separator, $this->tags); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @param string[] $tagNames |
|
131 | + * @return ExtendedCacheItemInterface |
|
132 | + */ |
|
133 | + public function removeTags(array $tagNames): ExtendedCacheItemInterface |
|
134 | + { |
|
135 | + foreach ($tagNames as $tagName) { |
|
136 | + $this->removeTag($tagName); |
|
137 | + } |
|
138 | + |
|
139 | + return $this; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param string $tagName |
|
144 | + * @return ExtendedCacheItemInterface |
|
145 | + */ |
|
146 | + public function removeTag(string $tagName): ExtendedCacheItemInterface |
|
147 | + { |
|
148 | + if (($key = \array_search($tagName, $this->tags, true)) !== false) { |
|
149 | + unset($this->tags[$key]); |
|
150 | + $this->removedTags[] = $tagName; |
|
151 | + } |
|
152 | + |
|
153 | + return $this; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @return string[] |
|
158 | + */ |
|
159 | + public function getRemovedTags(): array |
|
160 | + { |
|
161 | + return \array_diff($this->removedTags, $this->tags); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @return ExtendedCacheItemInterface |
|
166 | + */ |
|
167 | + public function clearRemovedTags(): ExtendedCacheItemInterface |
|
168 | + { |
|
169 | + $this->removedTags = []; |
|
170 | + |
|
171 | + return $this; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @throws PhpfastcacheLogicException |
|
176 | + * @throws PhpfastcacheInvalidArgumentException |
|
177 | + */ |
|
178 | + public function cloneInto(ExtendedCacheItemInterface $itemTarget, ?ExtendedCacheItemPoolInterface $itemPoolTarget = null): void |
|
179 | + { |
|
180 | + $itemTarget->setEventManager($this->getEventManager()) |
|
181 | + ->set($this->_getData()) |
|
182 | + ->setHit($this->isHit()) |
|
183 | + ->setTags($this->getTags()) |
|
184 | + ->expiresAt(clone $this->getExpirationDate()) |
|
185 | + ->setDriver($itemPoolTarget ?? $this->driver); |
|
186 | + |
|
187 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
188 | + $itemTarget->setCreationDate(clone $this->getCreationDate()) |
|
189 | + ->setModificationDate(clone $this->getModificationDate()); |
|
190 | + } |
|
191 | + } |
|
192 | 192 | } |
@@ -26,282 +26,282 @@ |
||
26 | 26 | |
27 | 27 | trait ExtendedCacheItemTrait |
28 | 28 | { |
29 | - use CacheItemTrait; |
|
30 | - |
|
31 | - protected ExtendedCacheItemPoolInterface $driver; |
|
32 | - |
|
33 | - protected string $encodedKey; |
|
34 | - |
|
35 | - /** |
|
36 | - * Item constructor. |
|
37 | - * @param ExtendedCacheItemPoolInterface $driver |
|
38 | - * @param string $key |
|
39 | - * @param EventManagerInterface $em |
|
40 | - * @throws PhpfastcacheInvalidArgumentException |
|
41 | - */ |
|
42 | - public function __construct(ExtendedCacheItemPoolInterface $driver, string $key, EventManagerInterface $em) |
|
43 | - { |
|
44 | - $this->data = null; |
|
45 | - $this->key = $key; |
|
46 | - $this->setEventManager($em); |
|
47 | - $this->setDriver($driver); |
|
48 | - if ($driver->getConfig()->isUseStaticItemCaching()) { |
|
49 | - $this->driver->setItem($this); |
|
50 | - } |
|
51 | - $this->expirationDate = new DateTime(); |
|
52 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
53 | - $this->creationDate = new DateTime(); |
|
54 | - $this->modificationDate = new DateTime(); |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param ExtendedCacheItemPoolInterface $driver |
|
60 | - * @return ExtendedCacheItemInterface |
|
61 | - * @throws PhpfastcacheInvalidArgumentException |
|
62 | - */ |
|
63 | - public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCacheItemInterface |
|
64 | - { |
|
65 | - $driverClass = $this->getDriverClass(); |
|
66 | - if ($driver instanceof $driverClass) { |
|
67 | - $this->driver = $driver; |
|
68 | - |
|
69 | - return $this; |
|
70 | - } |
|
71 | - |
|
72 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid driver instance "%s" for cache item "%s"', $driver::class, static::class)); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @inheritDoc |
|
77 | - */ |
|
78 | - public function getEncodedKey(): string |
|
79 | - { |
|
80 | - if (!isset($this->encodedKey)) { |
|
81 | - $this->encodedKey = $this->driver->getEncodedKey($this->getKey()); |
|
82 | - } |
|
83 | - |
|
84 | - return $this->encodedKey; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @inheritDoc |
|
89 | - * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
90 | - */ |
|
91 | - public function _getData(): mixed // @phpcs:ignore |
|
92 | - { |
|
93 | - return $this->data; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @inheritDoc |
|
98 | - */ |
|
99 | - public function getExpirationDate(): DateTimeInterface |
|
100 | - { |
|
101 | - return $this->expirationDate; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @inheritDoc |
|
106 | - * @throws PhpfastcacheInvalidArgumentException |
|
107 | - */ |
|
108 | - public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface |
|
109 | - { |
|
110 | - return $this->expiresAt($expiration); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @inheritDoc |
|
115 | - * @throws PhpfastcacheLogicException |
|
116 | - */ |
|
117 | - public function getCreationDate(): DateTimeInterface |
|
118 | - { |
|
119 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
120 | - return $this->creationDate; |
|
121 | - } |
|
122 | - |
|
123 | - throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @inheritDoc |
|
128 | - * @throws PhpfastcacheLogicException |
|
129 | - */ |
|
130 | - public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
131 | - { |
|
132 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
133 | - $this->creationDate = $this->demutateDatetime($date); |
|
134 | - return $this; |
|
135 | - } |
|
136 | - |
|
137 | - throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @inheritDoc |
|
142 | - * @throws PhpfastcacheLogicException |
|
143 | - */ |
|
144 | - public function getModificationDate(): DateTimeInterface |
|
145 | - { |
|
146 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
147 | - return $this->modificationDate; |
|
148 | - } |
|
149 | - |
|
150 | - throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @inheritDoc |
|
155 | - * @throws PhpfastcacheLogicException |
|
156 | - */ |
|
157 | - public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
158 | - { |
|
159 | - if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
160 | - $this->modificationDate = $this->demutateDatetime($date); |
|
161 | - return $this; |
|
162 | - } |
|
163 | - |
|
164 | - throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
165 | - } |
|
166 | - |
|
167 | - public function getTtl(): int |
|
168 | - { |
|
169 | - return \max(0, $this->expirationDate->getTimestamp() - \time()); |
|
170 | - } |
|
171 | - |
|
172 | - public function isExpired(): bool |
|
173 | - { |
|
174 | - return $this->getTtl() <= 0; |
|
175 | - } |
|
176 | - |
|
177 | - public function isNull(): bool |
|
178 | - { |
|
179 | - return $this->get() === null; |
|
180 | - } |
|
181 | - |
|
182 | - public function isEmpty(): bool |
|
183 | - { |
|
184 | - return empty($this->get()); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Return the data length: |
|
189 | - * Either the string length if it's a string (binary mode) |
|
190 | - * # or the number of element (count) if it's an array |
|
191 | - * # or the number returned by count() if it's an object implementing \Countable interface |
|
192 | - * # -1 for anything else |
|
193 | - * @return int |
|
194 | - */ |
|
195 | - public function getLength(): int |
|
196 | - { |
|
197 | - switch (\gettype($this->data)) { |
|
198 | - case 'array': |
|
199 | - case 'object': |
|
200 | - if (\is_countable($this->data)) { |
|
201 | - return \count($this->data); |
|
202 | - } |
|
203 | - break; |
|
204 | - |
|
205 | - case 'string': |
|
206 | - return \strlen($this->data); |
|
207 | - } |
|
208 | - |
|
209 | - return -1; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * @throws PhpfastcacheInvalidTypeException |
|
214 | - */ |
|
215 | - public function increment(int $step = 1): ExtendedCacheItemInterface |
|
216 | - { |
|
217 | - if ($this->data !== null && !\is_numeric($this->data)) { |
|
218 | - throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot increment on a "%s" type.', \gettype($this->data))); |
|
219 | - } |
|
220 | - |
|
221 | - $this->data += $step; |
|
222 | - |
|
223 | - return $this; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @throws PhpfastcacheInvalidTypeException |
|
228 | - */ |
|
229 | - public function decrement(int $step = 1): ExtendedCacheItemInterface |
|
230 | - { |
|
231 | - if ($this->data !== null && !\is_numeric($this->data)) { |
|
232 | - throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot decrement on a "%s" type.', \gettype($this->data))); |
|
233 | - } |
|
234 | - |
|
235 | - $this->data -= $step; |
|
236 | - |
|
237 | - return $this; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @throws PhpfastcacheInvalidTypeException |
|
242 | - */ |
|
243 | - public function append(array|string $data): ExtendedCacheItemInterface |
|
244 | - { |
|
245 | - if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
|
246 | - throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot append on a "%s" type.', \gettype($this->data))); |
|
247 | - } |
|
248 | - |
|
249 | - if (\is_array($this->data)) { |
|
250 | - $this->data[] = $data; |
|
251 | - } else { |
|
252 | - $this->data .= $data; |
|
253 | - } |
|
254 | - |
|
255 | - return $this; |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * @throws PhpfastcacheInvalidTypeException |
|
260 | - */ |
|
261 | - public function prepend(array|string $data): ExtendedCacheItemInterface |
|
262 | - { |
|
263 | - if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
|
264 | - throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot prepend on a "%s" type.', \gettype($this->data))); |
|
265 | - } |
|
266 | - |
|
267 | - if (\is_array($this->data)) { |
|
268 | - \array_unshift($this->data, $data); |
|
269 | - } else { |
|
270 | - $this->data = $data . $this->data; |
|
271 | - } |
|
272 | - |
|
273 | - return $this; |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Return the data as a well-formatted string. |
|
278 | - * Any scalar value will be cast to an array |
|
279 | - * @param int $options \json_encode() options |
|
280 | - * @param int $depth \json_encode() depth |
|
281 | - * @return string |
|
282 | - */ |
|
283 | - public function getDataAsJsonString(int $options = JSON_THROW_ON_ERROR, int $depth = 512): string |
|
284 | - { |
|
285 | - $data = $this->get(); |
|
286 | - |
|
287 | - if (\is_object($data) || \is_array($data)) { |
|
288 | - $data = \json_encode($data, $options, $depth); |
|
289 | - } else { |
|
290 | - $data = \json_encode([$data], $options, $depth); |
|
291 | - } |
|
292 | - |
|
293 | - return \json_encode($data, $options, $depth); |
|
294 | - } |
|
295 | - |
|
296 | - public function jsonSerialize(): mixed |
|
297 | - { |
|
298 | - return $this->get(); |
|
299 | - } |
|
300 | - |
|
301 | - public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool |
|
302 | - { |
|
303 | - return $driverPool::getClassNamespace() === self::getClassNamespace(); |
|
304 | - } |
|
305 | - |
|
306 | - abstract protected function getDriverClass(): string; |
|
29 | + use CacheItemTrait; |
|
30 | + |
|
31 | + protected ExtendedCacheItemPoolInterface $driver; |
|
32 | + |
|
33 | + protected string $encodedKey; |
|
34 | + |
|
35 | + /** |
|
36 | + * Item constructor. |
|
37 | + * @param ExtendedCacheItemPoolInterface $driver |
|
38 | + * @param string $key |
|
39 | + * @param EventManagerInterface $em |
|
40 | + * @throws PhpfastcacheInvalidArgumentException |
|
41 | + */ |
|
42 | + public function __construct(ExtendedCacheItemPoolInterface $driver, string $key, EventManagerInterface $em) |
|
43 | + { |
|
44 | + $this->data = null; |
|
45 | + $this->key = $key; |
|
46 | + $this->setEventManager($em); |
|
47 | + $this->setDriver($driver); |
|
48 | + if ($driver->getConfig()->isUseStaticItemCaching()) { |
|
49 | + $this->driver->setItem($this); |
|
50 | + } |
|
51 | + $this->expirationDate = new DateTime(); |
|
52 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
53 | + $this->creationDate = new DateTime(); |
|
54 | + $this->modificationDate = new DateTime(); |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param ExtendedCacheItemPoolInterface $driver |
|
60 | + * @return ExtendedCacheItemInterface |
|
61 | + * @throws PhpfastcacheInvalidArgumentException |
|
62 | + */ |
|
63 | + public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCacheItemInterface |
|
64 | + { |
|
65 | + $driverClass = $this->getDriverClass(); |
|
66 | + if ($driver instanceof $driverClass) { |
|
67 | + $this->driver = $driver; |
|
68 | + |
|
69 | + return $this; |
|
70 | + } |
|
71 | + |
|
72 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid driver instance "%s" for cache item "%s"', $driver::class, static::class)); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @inheritDoc |
|
77 | + */ |
|
78 | + public function getEncodedKey(): string |
|
79 | + { |
|
80 | + if (!isset($this->encodedKey)) { |
|
81 | + $this->encodedKey = $this->driver->getEncodedKey($this->getKey()); |
|
82 | + } |
|
83 | + |
|
84 | + return $this->encodedKey; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @inheritDoc |
|
89 | + * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
90 | + */ |
|
91 | + public function _getData(): mixed // @phpcs:ignore |
|
92 | + { |
|
93 | + return $this->data; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @inheritDoc |
|
98 | + */ |
|
99 | + public function getExpirationDate(): DateTimeInterface |
|
100 | + { |
|
101 | + return $this->expirationDate; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @inheritDoc |
|
106 | + * @throws PhpfastcacheInvalidArgumentException |
|
107 | + */ |
|
108 | + public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface |
|
109 | + { |
|
110 | + return $this->expiresAt($expiration); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @inheritDoc |
|
115 | + * @throws PhpfastcacheLogicException |
|
116 | + */ |
|
117 | + public function getCreationDate(): DateTimeInterface |
|
118 | + { |
|
119 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
120 | + return $this->creationDate; |
|
121 | + } |
|
122 | + |
|
123 | + throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @inheritDoc |
|
128 | + * @throws PhpfastcacheLogicException |
|
129 | + */ |
|
130 | + public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
131 | + { |
|
132 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
133 | + $this->creationDate = $this->demutateDatetime($date); |
|
134 | + return $this; |
|
135 | + } |
|
136 | + |
|
137 | + throw new PhpfastcacheLogicException('Cannot access to the creation date when the "itemDetailedDate" configuration is disabled.'); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @inheritDoc |
|
142 | + * @throws PhpfastcacheLogicException |
|
143 | + */ |
|
144 | + public function getModificationDate(): DateTimeInterface |
|
145 | + { |
|
146 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
147 | + return $this->modificationDate; |
|
148 | + } |
|
149 | + |
|
150 | + throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @inheritDoc |
|
155 | + * @throws PhpfastcacheLogicException |
|
156 | + */ |
|
157 | + public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface |
|
158 | + { |
|
159 | + if ($this->driver->getConfig()->isItemDetailedDate()) { |
|
160 | + $this->modificationDate = $this->demutateDatetime($date); |
|
161 | + return $this; |
|
162 | + } |
|
163 | + |
|
164 | + throw new PhpfastcacheLogicException('Cannot access to the modification date when the "itemDetailedDate" configuration is disabled.'); |
|
165 | + } |
|
166 | + |
|
167 | + public function getTtl(): int |
|
168 | + { |
|
169 | + return \max(0, $this->expirationDate->getTimestamp() - \time()); |
|
170 | + } |
|
171 | + |
|
172 | + public function isExpired(): bool |
|
173 | + { |
|
174 | + return $this->getTtl() <= 0; |
|
175 | + } |
|
176 | + |
|
177 | + public function isNull(): bool |
|
178 | + { |
|
179 | + return $this->get() === null; |
|
180 | + } |
|
181 | + |
|
182 | + public function isEmpty(): bool |
|
183 | + { |
|
184 | + return empty($this->get()); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Return the data length: |
|
189 | + * Either the string length if it's a string (binary mode) |
|
190 | + * # or the number of element (count) if it's an array |
|
191 | + * # or the number returned by count() if it's an object implementing \Countable interface |
|
192 | + * # -1 for anything else |
|
193 | + * @return int |
|
194 | + */ |
|
195 | + public function getLength(): int |
|
196 | + { |
|
197 | + switch (\gettype($this->data)) { |
|
198 | + case 'array': |
|
199 | + case 'object': |
|
200 | + if (\is_countable($this->data)) { |
|
201 | + return \count($this->data); |
|
202 | + } |
|
203 | + break; |
|
204 | + |
|
205 | + case 'string': |
|
206 | + return \strlen($this->data); |
|
207 | + } |
|
208 | + |
|
209 | + return -1; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * @throws PhpfastcacheInvalidTypeException |
|
214 | + */ |
|
215 | + public function increment(int $step = 1): ExtendedCacheItemInterface |
|
216 | + { |
|
217 | + if ($this->data !== null && !\is_numeric($this->data)) { |
|
218 | + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot increment on a "%s" type.', \gettype($this->data))); |
|
219 | + } |
|
220 | + |
|
221 | + $this->data += $step; |
|
222 | + |
|
223 | + return $this; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @throws PhpfastcacheInvalidTypeException |
|
228 | + */ |
|
229 | + public function decrement(int $step = 1): ExtendedCacheItemInterface |
|
230 | + { |
|
231 | + if ($this->data !== null && !\is_numeric($this->data)) { |
|
232 | + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot decrement on a "%s" type.', \gettype($this->data))); |
|
233 | + } |
|
234 | + |
|
235 | + $this->data -= $step; |
|
236 | + |
|
237 | + return $this; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @throws PhpfastcacheInvalidTypeException |
|
242 | + */ |
|
243 | + public function append(array|string $data): ExtendedCacheItemInterface |
|
244 | + { |
|
245 | + if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
|
246 | + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot append on a "%s" type.', \gettype($this->data))); |
|
247 | + } |
|
248 | + |
|
249 | + if (\is_array($this->data)) { |
|
250 | + $this->data[] = $data; |
|
251 | + } else { |
|
252 | + $this->data .= $data; |
|
253 | + } |
|
254 | + |
|
255 | + return $this; |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * @throws PhpfastcacheInvalidTypeException |
|
260 | + */ |
|
261 | + public function prepend(array|string $data): ExtendedCacheItemInterface |
|
262 | + { |
|
263 | + if ($this->data !== null && !\is_string($this->data) && !\is_array($this->data)) { |
|
264 | + throw new PhpfastcacheInvalidTypeException(\sprintf('Cannot prepend on a "%s" type.', \gettype($this->data))); |
|
265 | + } |
|
266 | + |
|
267 | + if (\is_array($this->data)) { |
|
268 | + \array_unshift($this->data, $data); |
|
269 | + } else { |
|
270 | + $this->data = $data . $this->data; |
|
271 | + } |
|
272 | + |
|
273 | + return $this; |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Return the data as a well-formatted string. |
|
278 | + * Any scalar value will be cast to an array |
|
279 | + * @param int $options \json_encode() options |
|
280 | + * @param int $depth \json_encode() depth |
|
281 | + * @return string |
|
282 | + */ |
|
283 | + public function getDataAsJsonString(int $options = JSON_THROW_ON_ERROR, int $depth = 512): string |
|
284 | + { |
|
285 | + $data = $this->get(); |
|
286 | + |
|
287 | + if (\is_object($data) || \is_array($data)) { |
|
288 | + $data = \json_encode($data, $options, $depth); |
|
289 | + } else { |
|
290 | + $data = \json_encode([$data], $options, $depth); |
|
291 | + } |
|
292 | + |
|
293 | + return \json_encode($data, $options, $depth); |
|
294 | + } |
|
295 | + |
|
296 | + public function jsonSerialize(): mixed |
|
297 | + { |
|
298 | + return $this->get(); |
|
299 | + } |
|
300 | + |
|
301 | + public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool |
|
302 | + { |
|
303 | + return $driverPool::getClassNamespace() === self::getClassNamespace(); |
|
304 | + } |
|
305 | + |
|
306 | + abstract protected function getDriverClass(): string; |
|
307 | 307 | } |
@@ -27,181 +27,181 @@ |
||
27 | 27 | use Psr\Cache\CacheItemInterface; |
28 | 28 | |
29 | 29 | interface ExtendedCacheItemInterface extends |
30 | - CacheItemInterface, |
|
31 | - EventManagerDispatcherInterface, |
|
32 | - ClassNamespaceResolverInterface, |
|
33 | - JsonSerializable, |
|
34 | - TaggableCacheItemInterface |
|
30 | + CacheItemInterface, |
|
31 | + EventManagerDispatcherInterface, |
|
32 | + ClassNamespaceResolverInterface, |
|
33 | + JsonSerializable, |
|
34 | + TaggableCacheItemInterface |
|
35 | 35 | { |
36 | - /** |
|
37 | - * Returns the encoded key for the current cache item. |
|
38 | - * Is a MD5 (default),SHA1,SHA256 hash if "defaultKeyHashFunction" config option is configured |
|
39 | - * Else return the plain cache item key "defaultKeyHashFunction" config option is emptied |
|
40 | - * |
|
41 | - * @return string |
|
42 | - * The encoded key string for this cache item. |
|
43 | - */ |
|
44 | - public function getEncodedKey(): string; |
|
45 | - |
|
46 | - /** |
|
47 | - * Returns the cache item value, regardless of hit status. |
|
48 | - * This method can be called if the cache item is NOT YET |
|
49 | - * persisted, and you need to access to its set value. |
|
50 | - * |
|
51 | - * Although not part of the CacheItemInterface, this method is used by |
|
52 | - * the pool for extracting information for saving. |
|
53 | - * |
|
54 | - * @return mixed |
|
55 | - * |
|
56 | - * @internal This method is used internally by phpfastcache, so you should not use it in your application. |
|
57 | - * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
58 | - */ |
|
59 | - public function _getData(): mixed; // @phpcs:ignore |
|
60 | - |
|
61 | - /** |
|
62 | - * @return DateTimeInterface |
|
63 | - */ |
|
64 | - public function getExpirationDate(): DateTimeInterface; |
|
65 | - |
|
66 | - /** |
|
67 | - * Alias of expireAt() with forced $expiration param |
|
68 | - * |
|
69 | - * @param DateTimeInterface $expiration |
|
70 | - * The point in time after which the item MUST be considered expired. |
|
71 | - * If null is passed explicitly, a default value MAY be used. If none is set, |
|
72 | - * the value should be stored permanently or for as long as the |
|
73 | - * implementation allows. |
|
74 | - * |
|
75 | - * @return ExtendedCacheItemInterface |
|
76 | - * The called object. |
|
77 | - */ |
|
78 | - public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface; |
|
79 | - |
|
80 | - /** |
|
81 | - * @return DateTimeInterface |
|
82 | - * @throws PhpfastcacheLogicException |
|
83 | - */ |
|
84 | - public function getCreationDate(): DateTimeInterface; |
|
85 | - |
|
86 | - /** |
|
87 | - * @return DateTimeInterface |
|
88 | - * @throws PhpfastcacheLogicException |
|
89 | - */ |
|
90 | - public function getModificationDate(): DateTimeInterface; |
|
91 | - |
|
92 | - /** |
|
93 | - * @param $date DateTimeInterface |
|
94 | - * |
|
95 | - * @return ExtendedCacheItemInterface |
|
96 | - * @throws PhpfastcacheLogicException |
|
97 | - */ |
|
98 | - public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface; |
|
99 | - |
|
100 | - /** |
|
101 | - * @param $date DateTimeInterface |
|
102 | - * |
|
103 | - * @return ExtendedCacheItemInterface |
|
104 | - * @throws PhpfastcacheLogicException |
|
105 | - */ |
|
106 | - public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface; |
|
107 | - |
|
108 | - /** |
|
109 | - * @return int |
|
110 | - */ |
|
111 | - public function getTtl(): int; |
|
112 | - |
|
113 | - /** |
|
114 | - * @return bool |
|
115 | - */ |
|
116 | - public function isExpired(): bool; |
|
117 | - |
|
118 | - /** |
|
119 | - * @return bool |
|
120 | - */ |
|
121 | - public function isNull(): bool; |
|
122 | - |
|
123 | - /** |
|
124 | - * @return bool |
|
125 | - */ |
|
126 | - public function isEmpty(): bool; |
|
127 | - |
|
128 | - /** |
|
129 | - * Return the data length: |
|
130 | - * - Either the number of char if it's a string (binary mode) |
|
131 | - * - or the number of element if it's an array |
|
132 | - * - or the number returned by count() if it's an object implementing \Countable interface |
|
133 | - * - or -1 for anything else |
|
134 | - * |
|
135 | - * @return int |
|
136 | - */ |
|
137 | - public function getLength(): int; |
|
138 | - |
|
139 | - /** |
|
140 | - * @param ExtendedCacheItemPoolInterface $driver |
|
141 | - * |
|
142 | - * @return ExtendedCacheItemInterface |
|
143 | - */ |
|
144 | - public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCacheItemInterface; |
|
145 | - |
|
146 | - /** |
|
147 | - * @param bool $isHit |
|
148 | - * |
|
149 | - * @return ExtendedCacheItemInterface |
|
150 | - */ |
|
151 | - public function setHit(bool $isHit): ExtendedCacheItemInterface; |
|
152 | - |
|
153 | - /** |
|
154 | - * @param int $step |
|
155 | - * |
|
156 | - * @return ExtendedCacheItemInterface |
|
157 | - * @throws PhpfastcacheInvalidTypeException |
|
158 | - */ |
|
159 | - public function increment(int $step = 1): ExtendedCacheItemInterface; |
|
160 | - |
|
161 | - /** |
|
162 | - * @param int $step |
|
163 | - * |
|
164 | - * @return ExtendedCacheItemInterface |
|
165 | - * @throws PhpfastcacheInvalidTypeException |
|
166 | - */ |
|
167 | - public function decrement(int $step = 1): ExtendedCacheItemInterface; |
|
168 | - |
|
169 | - /** |
|
170 | - * @param mixed[]|string $data |
|
171 | - * |
|
172 | - * @return ExtendedCacheItemInterface |
|
173 | - * @throws PhpfastcacheInvalidTypeException |
|
174 | - */ |
|
175 | - public function append(array|string $data): ExtendedCacheItemInterface; |
|
176 | - |
|
177 | - /** |
|
178 | - * @param mixed[]|string $data |
|
179 | - * |
|
180 | - * @return ExtendedCacheItemInterface |
|
181 | - * @throws PhpfastcacheInvalidTypeException |
|
182 | - */ |
|
183 | - public function prepend(array|string $data): ExtendedCacheItemInterface; |
|
184 | - |
|
185 | - /** |
|
186 | - * Return the data as a well-formatted string. |
|
187 | - * Any scalar value will be casted to an array |
|
188 | - * |
|
189 | - * @param int $options \json_encode() options |
|
190 | - * @param int $depth \json_encode() depth |
|
191 | - * |
|
192 | - * @return string |
|
193 | - */ |
|
194 | - public function getDataAsJsonString(int $options = JSON_THROW_ON_ERROR, int $depth = 512): string; |
|
195 | - |
|
196 | - /** |
|
197 | - * @param ExtendedCacheItemPoolInterface $driverPool |
|
198 | - * @return bool |
|
199 | - */ |
|
200 | - public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool; |
|
201 | - |
|
202 | - /** |
|
203 | - * @param ExtendedCacheItemInterface $itemTarget |
|
204 | - * @param ExtendedCacheItemPoolInterface|null $itemPoolTarget |
|
205 | - */ |
|
206 | - public function cloneInto(ExtendedCacheItemInterface $itemTarget, ?ExtendedCacheItemPoolInterface $itemPoolTarget = null): void; |
|
36 | + /** |
|
37 | + * Returns the encoded key for the current cache item. |
|
38 | + * Is a MD5 (default),SHA1,SHA256 hash if "defaultKeyHashFunction" config option is configured |
|
39 | + * Else return the plain cache item key "defaultKeyHashFunction" config option is emptied |
|
40 | + * |
|
41 | + * @return string |
|
42 | + * The encoded key string for this cache item. |
|
43 | + */ |
|
44 | + public function getEncodedKey(): string; |
|
45 | + |
|
46 | + /** |
|
47 | + * Returns the cache item value, regardless of hit status. |
|
48 | + * This method can be called if the cache item is NOT YET |
|
49 | + * persisted, and you need to access to its set value. |
|
50 | + * |
|
51 | + * Although not part of the CacheItemInterface, this method is used by |
|
52 | + * the pool for extracting information for saving. |
|
53 | + * |
|
54 | + * @return mixed |
|
55 | + * |
|
56 | + * @internal This method is used internally by phpfastcache, so you should not use it in your application. |
|
57 | + * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
|
58 | + */ |
|
59 | + public function _getData(): mixed; // @phpcs:ignore |
|
60 | + |
|
61 | + /** |
|
62 | + * @return DateTimeInterface |
|
63 | + */ |
|
64 | + public function getExpirationDate(): DateTimeInterface; |
|
65 | + |
|
66 | + /** |
|
67 | + * Alias of expireAt() with forced $expiration param |
|
68 | + * |
|
69 | + * @param DateTimeInterface $expiration |
|
70 | + * The point in time after which the item MUST be considered expired. |
|
71 | + * If null is passed explicitly, a default value MAY be used. If none is set, |
|
72 | + * the value should be stored permanently or for as long as the |
|
73 | + * implementation allows. |
|
74 | + * |
|
75 | + * @return ExtendedCacheItemInterface |
|
76 | + * The called object. |
|
77 | + */ |
|
78 | + public function setExpirationDate(DateTimeInterface $expiration): ExtendedCacheItemInterface; |
|
79 | + |
|
80 | + /** |
|
81 | + * @return DateTimeInterface |
|
82 | + * @throws PhpfastcacheLogicException |
|
83 | + */ |
|
84 | + public function getCreationDate(): DateTimeInterface; |
|
85 | + |
|
86 | + /** |
|
87 | + * @return DateTimeInterface |
|
88 | + * @throws PhpfastcacheLogicException |
|
89 | + */ |
|
90 | + public function getModificationDate(): DateTimeInterface; |
|
91 | + |
|
92 | + /** |
|
93 | + * @param $date DateTimeInterface |
|
94 | + * |
|
95 | + * @return ExtendedCacheItemInterface |
|
96 | + * @throws PhpfastcacheLogicException |
|
97 | + */ |
|
98 | + public function setCreationDate(DateTimeInterface $date): ExtendedCacheItemInterface; |
|
99 | + |
|
100 | + /** |
|
101 | + * @param $date DateTimeInterface |
|
102 | + * |
|
103 | + * @return ExtendedCacheItemInterface |
|
104 | + * @throws PhpfastcacheLogicException |
|
105 | + */ |
|
106 | + public function setModificationDate(DateTimeInterface $date): ExtendedCacheItemInterface; |
|
107 | + |
|
108 | + /** |
|
109 | + * @return int |
|
110 | + */ |
|
111 | + public function getTtl(): int; |
|
112 | + |
|
113 | + /** |
|
114 | + * @return bool |
|
115 | + */ |
|
116 | + public function isExpired(): bool; |
|
117 | + |
|
118 | + /** |
|
119 | + * @return bool |
|
120 | + */ |
|
121 | + public function isNull(): bool; |
|
122 | + |
|
123 | + /** |
|
124 | + * @return bool |
|
125 | + */ |
|
126 | + public function isEmpty(): bool; |
|
127 | + |
|
128 | + /** |
|
129 | + * Return the data length: |
|
130 | + * - Either the number of char if it's a string (binary mode) |
|
131 | + * - or the number of element if it's an array |
|
132 | + * - or the number returned by count() if it's an object implementing \Countable interface |
|
133 | + * - or -1 for anything else |
|
134 | + * |
|
135 | + * @return int |
|
136 | + */ |
|
137 | + public function getLength(): int; |
|
138 | + |
|
139 | + /** |
|
140 | + * @param ExtendedCacheItemPoolInterface $driver |
|
141 | + * |
|
142 | + * @return ExtendedCacheItemInterface |
|
143 | + */ |
|
144 | + public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCacheItemInterface; |
|
145 | + |
|
146 | + /** |
|
147 | + * @param bool $isHit |
|
148 | + * |
|
149 | + * @return ExtendedCacheItemInterface |
|
150 | + */ |
|
151 | + public function setHit(bool $isHit): ExtendedCacheItemInterface; |
|
152 | + |
|
153 | + /** |
|
154 | + * @param int $step |
|
155 | + * |
|
156 | + * @return ExtendedCacheItemInterface |
|
157 | + * @throws PhpfastcacheInvalidTypeException |
|
158 | + */ |
|
159 | + public function increment(int $step = 1): ExtendedCacheItemInterface; |
|
160 | + |
|
161 | + /** |
|
162 | + * @param int $step |
|
163 | + * |
|
164 | + * @return ExtendedCacheItemInterface |
|
165 | + * @throws PhpfastcacheInvalidTypeException |
|
166 | + */ |
|
167 | + public function decrement(int $step = 1): ExtendedCacheItemInterface; |
|
168 | + |
|
169 | + /** |
|
170 | + * @param mixed[]|string $data |
|
171 | + * |
|
172 | + * @return ExtendedCacheItemInterface |
|
173 | + * @throws PhpfastcacheInvalidTypeException |
|
174 | + */ |
|
175 | + public function append(array|string $data): ExtendedCacheItemInterface; |
|
176 | + |
|
177 | + /** |
|
178 | + * @param mixed[]|string $data |
|
179 | + * |
|
180 | + * @return ExtendedCacheItemInterface |
|
181 | + * @throws PhpfastcacheInvalidTypeException |
|
182 | + */ |
|
183 | + public function prepend(array|string $data): ExtendedCacheItemInterface; |
|
184 | + |
|
185 | + /** |
|
186 | + * Return the data as a well-formatted string. |
|
187 | + * Any scalar value will be casted to an array |
|
188 | + * |
|
189 | + * @param int $options \json_encode() options |
|
190 | + * @param int $depth \json_encode() depth |
|
191 | + * |
|
192 | + * @return string |
|
193 | + */ |
|
194 | + public function getDataAsJsonString(int $options = JSON_THROW_ON_ERROR, int $depth = 512): string; |
|
195 | + |
|
196 | + /** |
|
197 | + * @param ExtendedCacheItemPoolInterface $driverPool |
|
198 | + * @return bool |
|
199 | + */ |
|
200 | + public function doesItemBelongToThatDriverBackend(ExtendedCacheItemPoolInterface $driverPool): bool; |
|
201 | + |
|
202 | + /** |
|
203 | + * @param ExtendedCacheItemInterface $itemTarget |
|
204 | + * @param ExtendedCacheItemPoolInterface|null $itemPoolTarget |
|
205 | + */ |
|
206 | + public function cloneInto(ExtendedCacheItemInterface $itemTarget, ?ExtendedCacheItemPoolInterface $itemPoolTarget = null): void; |
|
207 | 207 | } |
@@ -27,123 +27,123 @@ |
||
27 | 27 | |
28 | 28 | trait CacheItemTrait |
29 | 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 months, 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 | - } |
|
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 months, 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 | 149 | } |
@@ -27,109 +27,109 @@ |
||
27 | 27 | |
28 | 28 | trait ExtendedCacheItemPoolTrait |
29 | 29 | { |
30 | - use CacheItemPoolTrait; |
|
31 | - use AggregatablePoolTrait; |
|
32 | - |
|
33 | - /** |
|
34 | - * @inheritDoc |
|
35 | - * @param string $pattern |
|
36 | - * @return array<string, mixed> |
|
37 | - */ |
|
38 | - public function getAllItems(string $pattern = ''): iterable |
|
39 | - { |
|
40 | - $driverReadAllKeysCallback = fn (string $pattern): iterable => $this->driverReadAllKeys($pattern); |
|
41 | - |
|
42 | - /** |
|
43 | - * This event allow you to customize the callback and wrap it to an invoker |
|
44 | - * like SebastianBergmann\Invoke\Invoke, so you can set up custom timeouts. |
|
45 | - */ |
|
46 | - $this->eventManager->dispatch(Event::CACHE_GET_ALL_ITEMS, $this, new EventReferenceParameter($driverReadAllKeysCallback)); |
|
47 | - $keys = $driverReadAllKeysCallback($pattern); |
|
48 | - |
|
49 | - if (count($keys) > 0) { |
|
50 | - return $this->getItems($keys instanceof \Traversable ? iterator_to_array($keys) : $keys); |
|
51 | - } |
|
52 | - |
|
53 | - return []; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @inheritDoc |
|
58 | - * @throws PhpfastcacheCoreException |
|
59 | - * @throws PhpfastcacheDriverException |
|
60 | - * @throws PhpfastcacheInvalidArgumentException |
|
61 | - * @throws PhpfastcacheLogicException |
|
62 | - */ |
|
63 | - public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string |
|
64 | - { |
|
65 | - return \json_encode( |
|
66 | - \array_map( |
|
67 | - static fn(CacheItemInterface $item) => $item->get(), |
|
68 | - \array_values($this->getItems($keys)) |
|
69 | - ), |
|
70 | - $options, |
|
71 | - $depth |
|
72 | - ); |
|
73 | - } |
|
74 | - |
|
75 | - public function detachAllItems(): static |
|
76 | - { |
|
77 | - foreach ($this->itemInstances as $item) { |
|
78 | - $this->detachItem($item); |
|
79 | - } |
|
80 | - |
|
81 | - return $this; |
|
82 | - } |
|
83 | - |
|
84 | - public function detachItem(CacheItemInterface $item): static |
|
85 | - { |
|
86 | - if (isset($this->itemInstances[$item->getKey()])) { |
|
87 | - $this->deregisterItem($item->getKey()); |
|
88 | - } |
|
89 | - |
|
90 | - return $this; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param ExtendedCacheItemInterface ...$items |
|
95 | - * @return bool |
|
96 | - * @throws PhpfastcacheCoreException |
|
97 | - * @throws PhpfastcacheDriverException |
|
98 | - * @throws PhpfastcacheInvalidArgumentException |
|
99 | - * @throws PhpfastcacheLogicException |
|
100 | - */ |
|
101 | - public function saveMultiple(ExtendedCacheItemInterface ...$items): bool |
|
102 | - { |
|
103 | - $this->eventManager->dispatch(Event::CACHE_SAVE_MULTIPLE_ITEMS, $this, new EventReferenceParameter($items)); |
|
104 | - |
|
105 | - if (\count($items)) { |
|
106 | - foreach ($items as $item) { |
|
107 | - $this->save($item); |
|
108 | - } |
|
109 | - return true; |
|
110 | - } |
|
111 | - return false; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function getHelp(): string |
|
118 | - { |
|
119 | - return ''; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @throws PhpfastcacheInvalidArgumentException |
|
124 | - */ |
|
125 | - public function throwUnsupportedDriverReadAllPattern(string $linkReference = ''): void |
|
126 | - { |
|
127 | - throw new PhpfastcacheInvalidArgumentException( |
|
128 | - sprintf( |
|
129 | - '%s does not support a pattern argument.%s', |
|
130 | - $this->getDriverName(), |
|
131 | - $linkReference ? " See $linkReference" : '' |
|
132 | - ) |
|
133 | - ); |
|
134 | - } |
|
30 | + use CacheItemPoolTrait; |
|
31 | + use AggregatablePoolTrait; |
|
32 | + |
|
33 | + /** |
|
34 | + * @inheritDoc |
|
35 | + * @param string $pattern |
|
36 | + * @return array<string, mixed> |
|
37 | + */ |
|
38 | + public function getAllItems(string $pattern = ''): iterable |
|
39 | + { |
|
40 | + $driverReadAllKeysCallback = fn (string $pattern): iterable => $this->driverReadAllKeys($pattern); |
|
41 | + |
|
42 | + /** |
|
43 | + * This event allow you to customize the callback and wrap it to an invoker |
|
44 | + * like SebastianBergmann\Invoke\Invoke, so you can set up custom timeouts. |
|
45 | + */ |
|
46 | + $this->eventManager->dispatch(Event::CACHE_GET_ALL_ITEMS, $this, new EventReferenceParameter($driverReadAllKeysCallback)); |
|
47 | + $keys = $driverReadAllKeysCallback($pattern); |
|
48 | + |
|
49 | + if (count($keys) > 0) { |
|
50 | + return $this->getItems($keys instanceof \Traversable ? iterator_to_array($keys) : $keys); |
|
51 | + } |
|
52 | + |
|
53 | + return []; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @inheritDoc |
|
58 | + * @throws PhpfastcacheCoreException |
|
59 | + * @throws PhpfastcacheDriverException |
|
60 | + * @throws PhpfastcacheInvalidArgumentException |
|
61 | + * @throws PhpfastcacheLogicException |
|
62 | + */ |
|
63 | + public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string |
|
64 | + { |
|
65 | + return \json_encode( |
|
66 | + \array_map( |
|
67 | + static fn(CacheItemInterface $item) => $item->get(), |
|
68 | + \array_values($this->getItems($keys)) |
|
69 | + ), |
|
70 | + $options, |
|
71 | + $depth |
|
72 | + ); |
|
73 | + } |
|
74 | + |
|
75 | + public function detachAllItems(): static |
|
76 | + { |
|
77 | + foreach ($this->itemInstances as $item) { |
|
78 | + $this->detachItem($item); |
|
79 | + } |
|
80 | + |
|
81 | + return $this; |
|
82 | + } |
|
83 | + |
|
84 | + public function detachItem(CacheItemInterface $item): static |
|
85 | + { |
|
86 | + if (isset($this->itemInstances[$item->getKey()])) { |
|
87 | + $this->deregisterItem($item->getKey()); |
|
88 | + } |
|
89 | + |
|
90 | + return $this; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param ExtendedCacheItemInterface ...$items |
|
95 | + * @return bool |
|
96 | + * @throws PhpfastcacheCoreException |
|
97 | + * @throws PhpfastcacheDriverException |
|
98 | + * @throws PhpfastcacheInvalidArgumentException |
|
99 | + * @throws PhpfastcacheLogicException |
|
100 | + */ |
|
101 | + public function saveMultiple(ExtendedCacheItemInterface ...$items): bool |
|
102 | + { |
|
103 | + $this->eventManager->dispatch(Event::CACHE_SAVE_MULTIPLE_ITEMS, $this, new EventReferenceParameter($items)); |
|
104 | + |
|
105 | + if (\count($items)) { |
|
106 | + foreach ($items as $item) { |
|
107 | + $this->save($item); |
|
108 | + } |
|
109 | + return true; |
|
110 | + } |
|
111 | + return false; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function getHelp(): string |
|
118 | + { |
|
119 | + return ''; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @throws PhpfastcacheInvalidArgumentException |
|
124 | + */ |
|
125 | + public function throwUnsupportedDriverReadAllPattern(string $linkReference = ''): void |
|
126 | + { |
|
127 | + throw new PhpfastcacheInvalidArgumentException( |
|
128 | + sprintf( |
|
129 | + '%s does not support a pattern argument.%s', |
|
130 | + $this->getDriverName(), |
|
131 | + $linkReference ? " See $linkReference" : '' |
|
132 | + ) |
|
133 | + ); |
|
134 | + } |
|
135 | 135 | } |
@@ -41,590 +41,590 @@ |
||
41 | 41 | */ |
42 | 42 | trait CacheItemPoolTrait |
43 | 43 | { |
44 | - use DriverBaseTrait { |
|
45 | - DriverBaseTrait::__construct as __driverBaseConstruct; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @var string |
|
50 | - */ |
|
51 | - protected static string $unsupportedKeyChars = '{}()/\@:'; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
|
55 | - */ |
|
56 | - protected array $deferredList = []; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
|
60 | - */ |
|
61 | - protected array $itemInstances = []; |
|
62 | - |
|
63 | - protected DriverIO $IO; |
|
64 | - |
|
65 | - public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
66 | - { |
|
67 | - $this->IO = new DriverIO(); |
|
68 | - $this->__driverBaseConstruct($config, $instanceId, $em); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @throws PhpfastcacheLogicException |
|
73 | - * @throws PhpfastcacheInvalidArgumentException |
|
74 | - */ |
|
75 | - public function setItem(CacheItemInterface $item): static |
|
76 | - { |
|
77 | - if (self::getItemClass() === $item::class) { |
|
78 | - if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
79 | - throw new PhpfastcacheLogicException( |
|
80 | - 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
81 | - ); |
|
82 | - } |
|
83 | - |
|
84 | - $this->itemInstances[$item->getKey()] = $item; |
|
85 | - |
|
86 | - return $this; |
|
87 | - } |
|
88 | - throw new PhpfastcacheInvalidArgumentException( |
|
89 | - \sprintf( |
|
90 | - 'Invalid cache item class "%s" for driver "%s".', |
|
91 | - get_class($item), |
|
92 | - get_class($this) |
|
93 | - ) |
|
94 | - ); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @inheritDoc |
|
99 | - * @throws PhpfastcacheCoreException |
|
100 | - * @throws PhpfastcacheDriverException |
|
101 | - * @throws PhpfastcacheInvalidArgumentException |
|
102 | - * @throws PhpfastcacheLogicException |
|
103 | - */ |
|
104 | - public function getItems(array $keys = []): iterable |
|
105 | - { |
|
106 | - $items = []; |
|
107 | - |
|
108 | - /** |
|
109 | - * Usually, drivers that are able to enable cache slams |
|
110 | - * does not benefit of driverReadMultiple() call. |
|
111 | - */ |
|
112 | - if (!$this->getConfig()->isPreventCacheSlams()) { |
|
113 | - $this->validateCacheKeys(...$keys); |
|
114 | - |
|
115 | - /** |
|
116 | - * Check for local item instances first. |
|
117 | - */ |
|
118 | - foreach ($keys as $index => $key) { |
|
119 | - if (isset($this->itemInstances[$key]) && $this->getConfig()->isUseStaticItemCaching()) { |
|
120 | - $items[$key] = $this->itemInstances[$key]; |
|
121 | - // Key already exists in local item instances, no need to fetch it again. |
|
122 | - unset($keys[$index]); |
|
123 | - } |
|
124 | - } |
|
125 | - $keys = array_values($keys); |
|
126 | - |
|
127 | - /** |
|
128 | - * If there's still keys to fetch, let's choose the right method (if supported). |
|
129 | - */ |
|
130 | - if (count($keys) > 1) { |
|
131 | - $items = array_merge( |
|
132 | - array_combine($keys, array_map(fn($key) => new (self::getItemClass())($this, $key, $this->eventManager), $keys)), |
|
133 | - $items |
|
134 | - ); |
|
135 | - |
|
136 | - try { |
|
137 | - $driverArrays = $this->driverReadMultiple(...$items); |
|
138 | - } catch (PhpfastcacheUnsupportedMethodException) { |
|
139 | - /** |
|
140 | - * Fallback for drivers that does not yet implement driverReadMultiple() method. |
|
141 | - */ |
|
142 | - $driverArrays = array_combine( |
|
143 | - array_map(fn($item) => $item->getKey(), $items), |
|
144 | - array_map(fn($item) => $this->driverRead($item), $items) |
|
145 | - ); |
|
146 | - } finally { |
|
147 | - foreach ($items as $item) { |
|
148 | - $driverArray = $driverArrays[$item->getKey()] ?? null; |
|
149 | - if ($driverArray !== null) { |
|
150 | - $item->set($this->driverUnwrapData($driverArray)); |
|
151 | - $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
152 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
153 | - /** |
|
154 | - * If the itemDetailedDate has been |
|
155 | - * set after caching, we MUST inject |
|
156 | - * a new DateTime object on the fly |
|
157 | - */ |
|
158 | - $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
159 | - $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
160 | - } |
|
161 | - $item->setTags($this->driverUnwrapTags($driverArray)); |
|
162 | - $this->handleExpiredCacheItem($item); |
|
163 | - } else { |
|
164 | - $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
165 | - } |
|
166 | - $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
167 | - } |
|
168 | - } |
|
169 | - } else { |
|
170 | - $index = array_key_first($keys); |
|
171 | - if ($index !== null) { |
|
172 | - $items[$keys[$index]] = $this->getItem($keys[$index]); |
|
173 | - } |
|
174 | - } |
|
175 | - } else { |
|
176 | - $collection = []; |
|
177 | - |
|
178 | - foreach ($keys as $key) { |
|
179 | - $collection[$key] = $this->getItem($key); |
|
180 | - } |
|
181 | - |
|
182 | - return $collection; |
|
183 | - } |
|
184 | - |
|
185 | - $this->eventManager->dispatch(Event::CACHE_GET_ITEMS, $this, $items); |
|
186 | - |
|
187 | - return $items; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * @param string $key |
|
192 | - * @return ExtendedCacheItemInterface |
|
193 | - * @throws PhpfastcacheCoreException |
|
194 | - * @throws PhpfastcacheInvalidArgumentException |
|
195 | - * @throws PhpfastcacheLogicException |
|
196 | - * @throws PhpfastcacheDriverException |
|
197 | - * |
|
198 | - * @SuppressWarnings(PHPMD.NPathComplexity) |
|
199 | - * @SuppressWarnings(PHPMD.GotoStatement) |
|
200 | - */ |
|
201 | - public function getItem(string $key): ExtendedCacheItemInterface |
|
202 | - { |
|
203 | - /** |
|
204 | - * Replace array_key_exists by isset |
|
205 | - * due to performance issue on huge |
|
206 | - * loop dispatching operations |
|
207 | - */ |
|
208 | - if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
|
209 | - $this->validateCacheKeys($key); |
|
210 | - |
|
211 | - /** @var $item ExtendedCacheItemInterface */ |
|
212 | - $item = new (self::getItemClass())($this, $key, $this->eventManager); |
|
213 | - |
|
214 | - $getItemDriverRead = function (float $cacheSlamsSpendSeconds = 0) use (&$getItemDriverRead, $item): void { |
|
215 | - $config = $this->getConfig(); |
|
216 | - |
|
217 | - $driverArray = $this->driverRead($item); |
|
218 | - |
|
219 | - if ($driverArray) { |
|
220 | - $driverData = $this->driverUnwrapData($driverArray); |
|
221 | - |
|
222 | - if ($config instanceof IOConfigurationOptionInterface && $config->isPreventCacheSlams()) { |
|
223 | - while ($driverData instanceof ItemBatch) { |
|
224 | - if ($driverData->getItemDate()->getTimestamp() + $config->getCacheSlamsTimeout() < \time()) { |
|
225 | - /** |
|
226 | - * The timeout has been reached |
|
227 | - * Consider that the batch has |
|
228 | - * failed and serve an empty item |
|
229 | - * to avoid get stuck with a |
|
230 | - * batch item stored in driver |
|
231 | - */ |
|
232 | - $this->handleExpiredCacheItem($item); |
|
233 | - return; |
|
234 | - } |
|
235 | - |
|
236 | - $this->eventManager->dispatch(Event::CACHE_GET_ITEM_IN_SLAM_BATCH, $this, $driverData, $cacheSlamsSpendSeconds); |
|
237 | - |
|
238 | - /** |
|
239 | - * Wait for a second before |
|
240 | - * attempting to get exit |
|
241 | - * the current batch process |
|
242 | - */ |
|
243 | - \usleep(100000); |
|
244 | - |
|
245 | - $getItemDriverRead($cacheSlamsSpendSeconds + 0.1); |
|
246 | - } |
|
247 | - } |
|
248 | - |
|
249 | - $item->set($driverData); |
|
250 | - $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
251 | - |
|
252 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
253 | - /** |
|
254 | - * If the itemDetailedDate has been |
|
255 | - * set after caching, we MUST inject |
|
256 | - * a new DateTime object on the fly |
|
257 | - */ |
|
258 | - $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
259 | - $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
260 | - } |
|
261 | - |
|
262 | - $item->setTags($this->driverUnwrapTags($driverArray)); |
|
263 | - $this->handleExpiredCacheItem($item); |
|
264 | - } else { |
|
265 | - $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
266 | - } |
|
267 | - }; |
|
268 | - $getItemDriverRead(); |
|
269 | - } else { |
|
270 | - $item = $this->itemInstances[$key]; |
|
271 | - } |
|
272 | - |
|
273 | - $this->eventManager->dispatch(Event::CACHE_GET_ITEM, $this, $item); |
|
274 | - |
|
275 | - $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
276 | - |
|
277 | - return $item; |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * @param string $key |
|
282 | - * @return bool |
|
283 | - * @throws PhpfastcacheCoreException |
|
284 | - * @throws PhpfastcacheDriverException |
|
285 | - * @throws PhpfastcacheInvalidArgumentException |
|
286 | - * @throws PhpfastcacheLogicException |
|
287 | - */ |
|
288 | - public function hasItem(string $key): bool |
|
289 | - { |
|
290 | - return $this->getItem($key)->isHit(); |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * @return bool |
|
295 | - * @throws PhpfastcacheCoreException |
|
296 | - * @throws PhpfastcacheDriverException |
|
297 | - * @throws PhpfastcacheLogicException |
|
298 | - * @throws PhpfastcacheIOException |
|
299 | - */ |
|
300 | - public function clear(): bool |
|
301 | - { |
|
302 | - $this->eventManager->dispatch(Event::CACHE_CLEAR_ITEM, $this, $this->itemInstances); |
|
303 | - |
|
304 | - $this->getIO()->incWriteHit(); |
|
305 | - // Faster than detachAllItems() |
|
306 | - $this->itemInstances = []; |
|
307 | - |
|
308 | - return $this->driverClear(); |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * @inheritDoc |
|
313 | - * @throws PhpfastcacheCoreException |
|
314 | - * @throws PhpfastcacheDriverException |
|
315 | - * @throws PhpfastcacheInvalidArgumentException |
|
316 | - * @throws PhpfastcacheLogicException |
|
317 | - */ |
|
318 | - public function deleteItems(array $keys): bool |
|
319 | - { |
|
320 | - if (count($keys) > 1) { |
|
321 | - $return = true; |
|
322 | - try { |
|
323 | - $items = $this->getItems($keys); |
|
324 | - $return = $this->driverDeleteMultiple($keys); |
|
325 | - foreach ($items as $item) { |
|
326 | - $item->setHit(false); |
|
327 | - |
|
328 | - if (!\str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
329 | - $this->cleanItemTags($item); |
|
330 | - } |
|
331 | - } |
|
332 | - $this->getIO()->incWriteHit(); |
|
333 | - $this->eventManager->dispatch(Event::CACHE_DELETE_ITEMS, $this, $items); |
|
334 | - $this->deregisterItems($keys); |
|
335 | - } catch (PhpfastcacheUnsupportedMethodException) { |
|
336 | - foreach ($keys as $key) { |
|
337 | - $result = $this->deleteItem($key); |
|
338 | - if ($result !== true) { |
|
339 | - $return = false; |
|
340 | - } |
|
341 | - } |
|
342 | - } |
|
343 | - |
|
344 | - return $return; |
|
345 | - } |
|
346 | - |
|
347 | - $index = array_key_first($keys); |
|
348 | - if ($index !== null) { |
|
349 | - return $this->deleteItem($keys[$index]); |
|
350 | - } |
|
351 | - |
|
352 | - return false; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * @param string $key |
|
357 | - * @return bool |
|
358 | - * @throws PhpfastcacheCoreException |
|
359 | - * @throws PhpfastcacheDriverException |
|
360 | - * @throws PhpfastcacheInvalidArgumentException |
|
361 | - * @throws PhpfastcacheLogicException |
|
362 | - */ |
|
363 | - public function deleteItem(string $key): bool |
|
364 | - { |
|
365 | - $item = $this->getItem($key); |
|
366 | - if ($item->isHit()) { |
|
367 | - $result = $this->driverDelete($item->getKey(), $item->getEncodedKey()); |
|
368 | - $item->setHit(false); |
|
369 | - $this->getIO()->incWriteHit(); |
|
370 | - |
|
371 | - $this->eventManager->dispatch(Event::CACHE_DELETE_ITEM, $this, $item); |
|
372 | - |
|
373 | - /** |
|
374 | - * De-register the item instance |
|
375 | - * then collect gc cycles |
|
376 | - */ |
|
377 | - $this->deregisterItem($key); |
|
378 | - |
|
379 | - /** |
|
380 | - * Perform a tag cleanup to avoid memory leaks |
|
381 | - */ |
|
382 | - if (!\str_starts_with($key, TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
383 | - $this->cleanItemTags($item); |
|
384 | - } |
|
385 | - |
|
386 | - return $result; |
|
387 | - } |
|
388 | - |
|
389 | - return false; |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * @param CacheItemInterface $item |
|
394 | - * @return bool |
|
395 | - */ |
|
396 | - public function saveDeferred(CacheItemInterface $item): bool |
|
397 | - { |
|
398 | - $this->assertCacheItemType($item, self::getItemClass()); |
|
399 | - if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
|
400 | - $this->itemInstances[$item->getKey()] = $item; |
|
401 | - } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
402 | - throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
403 | - } |
|
404 | - |
|
405 | - $this->eventManager->dispatch(Event::CACHE_SAVE_DEFERRED_ITEM, $this, $item); |
|
406 | - $this->deferredList[$item->getKey()] = $item; |
|
407 | - |
|
408 | - return true; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * @return bool |
|
413 | - * @throws PhpfastcacheCoreException |
|
414 | - * @throws PhpfastcacheDriverException |
|
415 | - * @throws PhpfastcacheInvalidArgumentException |
|
416 | - * @throws PhpfastcacheLogicException |
|
417 | - */ |
|
418 | - public function commit(): bool |
|
419 | - { |
|
420 | - $this->eventManager->dispatch(Event::CACHE_COMMIT_ITEM, $this, new EventReferenceParameter($this->deferredList)); |
|
421 | - |
|
422 | - if (\count($this->deferredList)) { |
|
423 | - $return = true; |
|
424 | - foreach ($this->deferredList as $key => $item) { |
|
425 | - $result = $this->save($item); |
|
426 | - unset($this->deferredList[$key]); |
|
427 | - if ($result !== true) { |
|
428 | - $return = $result; |
|
429 | - } |
|
430 | - } |
|
431 | - |
|
432 | - return $return; |
|
433 | - } |
|
434 | - return false; |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * @param CacheItemInterface $item |
|
439 | - * @return bool |
|
440 | - * @throws PhpfastcacheCoreException |
|
441 | - * @throws PhpfastcacheDriverException |
|
442 | - * @throws PhpfastcacheIOException |
|
443 | - * @throws PhpfastcacheInvalidArgumentException |
|
444 | - * @throws PhpfastcacheLogicException |
|
445 | - */ |
|
446 | - public function save(CacheItemInterface $item): bool |
|
447 | - { |
|
448 | - $this->assertCacheItemType($item, self::getItemClass()); |
|
449 | - /** |
|
450 | - * @var ExtendedCacheItemInterface $item |
|
451 | - * |
|
452 | - * Replace array_key_exists by isset |
|
453 | - * due to performance issue on huge |
|
454 | - * loop dispatching operations |
|
455 | - */ |
|
456 | - if (!isset($this->itemInstances[$item->getKey()])) { |
|
457 | - if ($this->getConfig()->isUseStaticItemCaching()) { |
|
458 | - $this->itemInstances[$item->getKey()] = $item; |
|
459 | - } |
|
460 | - } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
461 | - throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
462 | - } |
|
463 | - |
|
464 | - $this->assertCacheItemType($item, self::getItemClass()); |
|
465 | - $this->eventManager->dispatch(Event::CACHE_SAVE_ITEM, $this, $item); |
|
466 | - |
|
467 | - if ($this->getConfig() instanceof IOConfigurationOptionInterface && $this->getConfig()->isPreventCacheSlams()) { |
|
468 | - /** |
|
469 | - * @var $itemBatch ExtendedCacheItemInterface |
|
470 | - */ |
|
471 | - $itemClassName = self::getItemClass(); |
|
472 | - $itemBatch = new $itemClassName($this, $item->getKey(), $this->eventManager); |
|
473 | - $itemBatch->set(new ItemBatch($item->getKey(), new DateTime())) |
|
474 | - ->expiresAfter($this->getConfig()->getCacheSlamsTimeout()); |
|
475 | - |
|
476 | - /** |
|
477 | - * To avoid SPL mismatches |
|
478 | - * we have to re-attach the |
|
479 | - * original item to the pool |
|
480 | - */ |
|
481 | - $this->driverWrite($itemBatch); |
|
482 | - $this->detachItem($itemBatch); |
|
483 | - $this->attachItem($item); |
|
484 | - } |
|
485 | - |
|
486 | - |
|
487 | - if ($this->driverWrite($item) && $this->driverWriteTags($item)) { |
|
488 | - $item->setHit(true) |
|
489 | - ->clearRemovedTags(); |
|
490 | - |
|
491 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
492 | - $item->setModificationDate(new \DateTime()); |
|
493 | - } |
|
494 | - |
|
495 | - $this->getIO()->incWriteHit(); |
|
496 | - |
|
497 | - return true; |
|
498 | - } |
|
499 | - |
|
500 | - return false; |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * @return DriverIO |
|
505 | - */ |
|
506 | - public function getIO(): DriverIO |
|
507 | - { |
|
508 | - return $this->IO; |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * @internal This method de-register an item from $this->itemInstances |
|
513 | - */ |
|
514 | - protected function deregisterItem(string $itemKey): static |
|
515 | - { |
|
516 | - unset($this->itemInstances[$itemKey]); |
|
517 | - |
|
518 | - if (\gc_enabled()) { |
|
519 | - \gc_collect_cycles(); |
|
520 | - } |
|
521 | - |
|
522 | - return $this; |
|
523 | - } |
|
524 | - |
|
525 | - /** |
|
526 | - * @param string[] $itemKeys |
|
527 | - * @internal This method de-register multiple items from $this->itemInstances |
|
528 | - */ |
|
529 | - protected function deregisterItems(array $itemKeys): static |
|
530 | - { |
|
531 | - $this->itemInstances = array_diff_key($this->itemInstances, array_flip($itemKeys)); |
|
532 | - |
|
533 | - if (\gc_enabled()) { |
|
534 | - \gc_collect_cycles(); |
|
535 | - } |
|
536 | - |
|
537 | - return $this; |
|
538 | - } |
|
539 | - |
|
540 | - /** |
|
541 | - * @throws PhpfastcacheLogicException |
|
542 | - */ |
|
543 | - public function attachItem(CacheItemInterface $item): static |
|
544 | - { |
|
545 | - if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
546 | - throw new PhpfastcacheLogicException( |
|
547 | - 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! |
|
44 | + use DriverBaseTrait { |
|
45 | + DriverBaseTrait::__construct as __driverBaseConstruct; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @var string |
|
50 | + */ |
|
51 | + protected static string $unsupportedKeyChars = '{}()/\@:'; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
|
55 | + */ |
|
56 | + protected array $deferredList = []; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
|
60 | + */ |
|
61 | + protected array $itemInstances = []; |
|
62 | + |
|
63 | + protected DriverIO $IO; |
|
64 | + |
|
65 | + public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
66 | + { |
|
67 | + $this->IO = new DriverIO(); |
|
68 | + $this->__driverBaseConstruct($config, $instanceId, $em); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @throws PhpfastcacheLogicException |
|
73 | + * @throws PhpfastcacheInvalidArgumentException |
|
74 | + */ |
|
75 | + public function setItem(CacheItemInterface $item): static |
|
76 | + { |
|
77 | + if (self::getItemClass() === $item::class) { |
|
78 | + if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
79 | + throw new PhpfastcacheLogicException( |
|
80 | + 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
81 | + ); |
|
82 | + } |
|
83 | + |
|
84 | + $this->itemInstances[$item->getKey()] = $item; |
|
85 | + |
|
86 | + return $this; |
|
87 | + } |
|
88 | + throw new PhpfastcacheInvalidArgumentException( |
|
89 | + \sprintf( |
|
90 | + 'Invalid cache item class "%s" for driver "%s".', |
|
91 | + get_class($item), |
|
92 | + get_class($this) |
|
93 | + ) |
|
94 | + ); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @inheritDoc |
|
99 | + * @throws PhpfastcacheCoreException |
|
100 | + * @throws PhpfastcacheDriverException |
|
101 | + * @throws PhpfastcacheInvalidArgumentException |
|
102 | + * @throws PhpfastcacheLogicException |
|
103 | + */ |
|
104 | + public function getItems(array $keys = []): iterable |
|
105 | + { |
|
106 | + $items = []; |
|
107 | + |
|
108 | + /** |
|
109 | + * Usually, drivers that are able to enable cache slams |
|
110 | + * does not benefit of driverReadMultiple() call. |
|
111 | + */ |
|
112 | + if (!$this->getConfig()->isPreventCacheSlams()) { |
|
113 | + $this->validateCacheKeys(...$keys); |
|
114 | + |
|
115 | + /** |
|
116 | + * Check for local item instances first. |
|
117 | + */ |
|
118 | + foreach ($keys as $index => $key) { |
|
119 | + if (isset($this->itemInstances[$key]) && $this->getConfig()->isUseStaticItemCaching()) { |
|
120 | + $items[$key] = $this->itemInstances[$key]; |
|
121 | + // Key already exists in local item instances, no need to fetch it again. |
|
122 | + unset($keys[$index]); |
|
123 | + } |
|
124 | + } |
|
125 | + $keys = array_values($keys); |
|
126 | + |
|
127 | + /** |
|
128 | + * If there's still keys to fetch, let's choose the right method (if supported). |
|
129 | + */ |
|
130 | + if (count($keys) > 1) { |
|
131 | + $items = array_merge( |
|
132 | + array_combine($keys, array_map(fn($key) => new (self::getItemClass())($this, $key, $this->eventManager), $keys)), |
|
133 | + $items |
|
134 | + ); |
|
135 | + |
|
136 | + try { |
|
137 | + $driverArrays = $this->driverReadMultiple(...$items); |
|
138 | + } catch (PhpfastcacheUnsupportedMethodException) { |
|
139 | + /** |
|
140 | + * Fallback for drivers that does not yet implement driverReadMultiple() method. |
|
141 | + */ |
|
142 | + $driverArrays = array_combine( |
|
143 | + array_map(fn($item) => $item->getKey(), $items), |
|
144 | + array_map(fn($item) => $this->driverRead($item), $items) |
|
145 | + ); |
|
146 | + } finally { |
|
147 | + foreach ($items as $item) { |
|
148 | + $driverArray = $driverArrays[$item->getKey()] ?? null; |
|
149 | + if ($driverArray !== null) { |
|
150 | + $item->set($this->driverUnwrapData($driverArray)); |
|
151 | + $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
152 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
153 | + /** |
|
154 | + * If the itemDetailedDate has been |
|
155 | + * set after caching, we MUST inject |
|
156 | + * a new DateTime object on the fly |
|
157 | + */ |
|
158 | + $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
159 | + $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
160 | + } |
|
161 | + $item->setTags($this->driverUnwrapTags($driverArray)); |
|
162 | + $this->handleExpiredCacheItem($item); |
|
163 | + } else { |
|
164 | + $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
165 | + } |
|
166 | + $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
167 | + } |
|
168 | + } |
|
169 | + } else { |
|
170 | + $index = array_key_first($keys); |
|
171 | + if ($index !== null) { |
|
172 | + $items[$keys[$index]] = $this->getItem($keys[$index]); |
|
173 | + } |
|
174 | + } |
|
175 | + } else { |
|
176 | + $collection = []; |
|
177 | + |
|
178 | + foreach ($keys as $key) { |
|
179 | + $collection[$key] = $this->getItem($key); |
|
180 | + } |
|
181 | + |
|
182 | + return $collection; |
|
183 | + } |
|
184 | + |
|
185 | + $this->eventManager->dispatch(Event::CACHE_GET_ITEMS, $this, $items); |
|
186 | + |
|
187 | + return $items; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * @param string $key |
|
192 | + * @return ExtendedCacheItemInterface |
|
193 | + * @throws PhpfastcacheCoreException |
|
194 | + * @throws PhpfastcacheInvalidArgumentException |
|
195 | + * @throws PhpfastcacheLogicException |
|
196 | + * @throws PhpfastcacheDriverException |
|
197 | + * |
|
198 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
|
199 | + * @SuppressWarnings(PHPMD.GotoStatement) |
|
200 | + */ |
|
201 | + public function getItem(string $key): ExtendedCacheItemInterface |
|
202 | + { |
|
203 | + /** |
|
204 | + * Replace array_key_exists by isset |
|
205 | + * due to performance issue on huge |
|
206 | + * loop dispatching operations |
|
207 | + */ |
|
208 | + if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
|
209 | + $this->validateCacheKeys($key); |
|
210 | + |
|
211 | + /** @var $item ExtendedCacheItemInterface */ |
|
212 | + $item = new (self::getItemClass())($this, $key, $this->eventManager); |
|
213 | + |
|
214 | + $getItemDriverRead = function (float $cacheSlamsSpendSeconds = 0) use (&$getItemDriverRead, $item): void { |
|
215 | + $config = $this->getConfig(); |
|
216 | + |
|
217 | + $driverArray = $this->driverRead($item); |
|
218 | + |
|
219 | + if ($driverArray) { |
|
220 | + $driverData = $this->driverUnwrapData($driverArray); |
|
221 | + |
|
222 | + if ($config instanceof IOConfigurationOptionInterface && $config->isPreventCacheSlams()) { |
|
223 | + while ($driverData instanceof ItemBatch) { |
|
224 | + if ($driverData->getItemDate()->getTimestamp() + $config->getCacheSlamsTimeout() < \time()) { |
|
225 | + /** |
|
226 | + * The timeout has been reached |
|
227 | + * Consider that the batch has |
|
228 | + * failed and serve an empty item |
|
229 | + * to avoid get stuck with a |
|
230 | + * batch item stored in driver |
|
231 | + */ |
|
232 | + $this->handleExpiredCacheItem($item); |
|
233 | + return; |
|
234 | + } |
|
235 | + |
|
236 | + $this->eventManager->dispatch(Event::CACHE_GET_ITEM_IN_SLAM_BATCH, $this, $driverData, $cacheSlamsSpendSeconds); |
|
237 | + |
|
238 | + /** |
|
239 | + * Wait for a second before |
|
240 | + * attempting to get exit |
|
241 | + * the current batch process |
|
242 | + */ |
|
243 | + \usleep(100000); |
|
244 | + |
|
245 | + $getItemDriverRead($cacheSlamsSpendSeconds + 0.1); |
|
246 | + } |
|
247 | + } |
|
248 | + |
|
249 | + $item->set($driverData); |
|
250 | + $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
251 | + |
|
252 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
253 | + /** |
|
254 | + * If the itemDetailedDate has been |
|
255 | + * set after caching, we MUST inject |
|
256 | + * a new DateTime object on the fly |
|
257 | + */ |
|
258 | + $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
259 | + $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
260 | + } |
|
261 | + |
|
262 | + $item->setTags($this->driverUnwrapTags($driverArray)); |
|
263 | + $this->handleExpiredCacheItem($item); |
|
264 | + } else { |
|
265 | + $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
266 | + } |
|
267 | + }; |
|
268 | + $getItemDriverRead(); |
|
269 | + } else { |
|
270 | + $item = $this->itemInstances[$key]; |
|
271 | + } |
|
272 | + |
|
273 | + $this->eventManager->dispatch(Event::CACHE_GET_ITEM, $this, $item); |
|
274 | + |
|
275 | + $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
276 | + |
|
277 | + return $item; |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * @param string $key |
|
282 | + * @return bool |
|
283 | + * @throws PhpfastcacheCoreException |
|
284 | + * @throws PhpfastcacheDriverException |
|
285 | + * @throws PhpfastcacheInvalidArgumentException |
|
286 | + * @throws PhpfastcacheLogicException |
|
287 | + */ |
|
288 | + public function hasItem(string $key): bool |
|
289 | + { |
|
290 | + return $this->getItem($key)->isHit(); |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * @return bool |
|
295 | + * @throws PhpfastcacheCoreException |
|
296 | + * @throws PhpfastcacheDriverException |
|
297 | + * @throws PhpfastcacheLogicException |
|
298 | + * @throws PhpfastcacheIOException |
|
299 | + */ |
|
300 | + public function clear(): bool |
|
301 | + { |
|
302 | + $this->eventManager->dispatch(Event::CACHE_CLEAR_ITEM, $this, $this->itemInstances); |
|
303 | + |
|
304 | + $this->getIO()->incWriteHit(); |
|
305 | + // Faster than detachAllItems() |
|
306 | + $this->itemInstances = []; |
|
307 | + |
|
308 | + return $this->driverClear(); |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * @inheritDoc |
|
313 | + * @throws PhpfastcacheCoreException |
|
314 | + * @throws PhpfastcacheDriverException |
|
315 | + * @throws PhpfastcacheInvalidArgumentException |
|
316 | + * @throws PhpfastcacheLogicException |
|
317 | + */ |
|
318 | + public function deleteItems(array $keys): bool |
|
319 | + { |
|
320 | + if (count($keys) > 1) { |
|
321 | + $return = true; |
|
322 | + try { |
|
323 | + $items = $this->getItems($keys); |
|
324 | + $return = $this->driverDeleteMultiple($keys); |
|
325 | + foreach ($items as $item) { |
|
326 | + $item->setHit(false); |
|
327 | + |
|
328 | + if (!\str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
329 | + $this->cleanItemTags($item); |
|
330 | + } |
|
331 | + } |
|
332 | + $this->getIO()->incWriteHit(); |
|
333 | + $this->eventManager->dispatch(Event::CACHE_DELETE_ITEMS, $this, $items); |
|
334 | + $this->deregisterItems($keys); |
|
335 | + } catch (PhpfastcacheUnsupportedMethodException) { |
|
336 | + foreach ($keys as $key) { |
|
337 | + $result = $this->deleteItem($key); |
|
338 | + if ($result !== true) { |
|
339 | + $return = false; |
|
340 | + } |
|
341 | + } |
|
342 | + } |
|
343 | + |
|
344 | + return $return; |
|
345 | + } |
|
346 | + |
|
347 | + $index = array_key_first($keys); |
|
348 | + if ($index !== null) { |
|
349 | + return $this->deleteItem($keys[$index]); |
|
350 | + } |
|
351 | + |
|
352 | + return false; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * @param string $key |
|
357 | + * @return bool |
|
358 | + * @throws PhpfastcacheCoreException |
|
359 | + * @throws PhpfastcacheDriverException |
|
360 | + * @throws PhpfastcacheInvalidArgumentException |
|
361 | + * @throws PhpfastcacheLogicException |
|
362 | + */ |
|
363 | + public function deleteItem(string $key): bool |
|
364 | + { |
|
365 | + $item = $this->getItem($key); |
|
366 | + if ($item->isHit()) { |
|
367 | + $result = $this->driverDelete($item->getKey(), $item->getEncodedKey()); |
|
368 | + $item->setHit(false); |
|
369 | + $this->getIO()->incWriteHit(); |
|
370 | + |
|
371 | + $this->eventManager->dispatch(Event::CACHE_DELETE_ITEM, $this, $item); |
|
372 | + |
|
373 | + /** |
|
374 | + * De-register the item instance |
|
375 | + * then collect gc cycles |
|
376 | + */ |
|
377 | + $this->deregisterItem($key); |
|
378 | + |
|
379 | + /** |
|
380 | + * Perform a tag cleanup to avoid memory leaks |
|
381 | + */ |
|
382 | + if (!\str_starts_with($key, TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
383 | + $this->cleanItemTags($item); |
|
384 | + } |
|
385 | + |
|
386 | + return $result; |
|
387 | + } |
|
388 | + |
|
389 | + return false; |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * @param CacheItemInterface $item |
|
394 | + * @return bool |
|
395 | + */ |
|
396 | + public function saveDeferred(CacheItemInterface $item): bool |
|
397 | + { |
|
398 | + $this->assertCacheItemType($item, self::getItemClass()); |
|
399 | + if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
|
400 | + $this->itemInstances[$item->getKey()] = $item; |
|
401 | + } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
402 | + throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
403 | + } |
|
404 | + |
|
405 | + $this->eventManager->dispatch(Event::CACHE_SAVE_DEFERRED_ITEM, $this, $item); |
|
406 | + $this->deferredList[$item->getKey()] = $item; |
|
407 | + |
|
408 | + return true; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * @return bool |
|
413 | + * @throws PhpfastcacheCoreException |
|
414 | + * @throws PhpfastcacheDriverException |
|
415 | + * @throws PhpfastcacheInvalidArgumentException |
|
416 | + * @throws PhpfastcacheLogicException |
|
417 | + */ |
|
418 | + public function commit(): bool |
|
419 | + { |
|
420 | + $this->eventManager->dispatch(Event::CACHE_COMMIT_ITEM, $this, new EventReferenceParameter($this->deferredList)); |
|
421 | + |
|
422 | + if (\count($this->deferredList)) { |
|
423 | + $return = true; |
|
424 | + foreach ($this->deferredList as $key => $item) { |
|
425 | + $result = $this->save($item); |
|
426 | + unset($this->deferredList[$key]); |
|
427 | + if ($result !== true) { |
|
428 | + $return = $result; |
|
429 | + } |
|
430 | + } |
|
431 | + |
|
432 | + return $return; |
|
433 | + } |
|
434 | + return false; |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * @param CacheItemInterface $item |
|
439 | + * @return bool |
|
440 | + * @throws PhpfastcacheCoreException |
|
441 | + * @throws PhpfastcacheDriverException |
|
442 | + * @throws PhpfastcacheIOException |
|
443 | + * @throws PhpfastcacheInvalidArgumentException |
|
444 | + * @throws PhpfastcacheLogicException |
|
445 | + */ |
|
446 | + public function save(CacheItemInterface $item): bool |
|
447 | + { |
|
448 | + $this->assertCacheItemType($item, self::getItemClass()); |
|
449 | + /** |
|
450 | + * @var ExtendedCacheItemInterface $item |
|
451 | + * |
|
452 | + * Replace array_key_exists by isset |
|
453 | + * due to performance issue on huge |
|
454 | + * loop dispatching operations |
|
455 | + */ |
|
456 | + if (!isset($this->itemInstances[$item->getKey()])) { |
|
457 | + if ($this->getConfig()->isUseStaticItemCaching()) { |
|
458 | + $this->itemInstances[$item->getKey()] = $item; |
|
459 | + } |
|
460 | + } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
461 | + throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
462 | + } |
|
463 | + |
|
464 | + $this->assertCacheItemType($item, self::getItemClass()); |
|
465 | + $this->eventManager->dispatch(Event::CACHE_SAVE_ITEM, $this, $item); |
|
466 | + |
|
467 | + if ($this->getConfig() instanceof IOConfigurationOptionInterface && $this->getConfig()->isPreventCacheSlams()) { |
|
468 | + /** |
|
469 | + * @var $itemBatch ExtendedCacheItemInterface |
|
470 | + */ |
|
471 | + $itemClassName = self::getItemClass(); |
|
472 | + $itemBatch = new $itemClassName($this, $item->getKey(), $this->eventManager); |
|
473 | + $itemBatch->set(new ItemBatch($item->getKey(), new DateTime())) |
|
474 | + ->expiresAfter($this->getConfig()->getCacheSlamsTimeout()); |
|
475 | + |
|
476 | + /** |
|
477 | + * To avoid SPL mismatches |
|
478 | + * we have to re-attach the |
|
479 | + * original item to the pool |
|
480 | + */ |
|
481 | + $this->driverWrite($itemBatch); |
|
482 | + $this->detachItem($itemBatch); |
|
483 | + $this->attachItem($item); |
|
484 | + } |
|
485 | + |
|
486 | + |
|
487 | + if ($this->driverWrite($item) && $this->driverWriteTags($item)) { |
|
488 | + $item->setHit(true) |
|
489 | + ->clearRemovedTags(); |
|
490 | + |
|
491 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
492 | + $item->setModificationDate(new \DateTime()); |
|
493 | + } |
|
494 | + |
|
495 | + $this->getIO()->incWriteHit(); |
|
496 | + |
|
497 | + return true; |
|
498 | + } |
|
499 | + |
|
500 | + return false; |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * @return DriverIO |
|
505 | + */ |
|
506 | + public function getIO(): DriverIO |
|
507 | + { |
|
508 | + return $this->IO; |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * @internal This method de-register an item from $this->itemInstances |
|
513 | + */ |
|
514 | + protected function deregisterItem(string $itemKey): static |
|
515 | + { |
|
516 | + unset($this->itemInstances[$itemKey]); |
|
517 | + |
|
518 | + if (\gc_enabled()) { |
|
519 | + \gc_collect_cycles(); |
|
520 | + } |
|
521 | + |
|
522 | + return $this; |
|
523 | + } |
|
524 | + |
|
525 | + /** |
|
526 | + * @param string[] $itemKeys |
|
527 | + * @internal This method de-register multiple items from $this->itemInstances |
|
528 | + */ |
|
529 | + protected function deregisterItems(array $itemKeys): static |
|
530 | + { |
|
531 | + $this->itemInstances = array_diff_key($this->itemInstances, array_flip($itemKeys)); |
|
532 | + |
|
533 | + if (\gc_enabled()) { |
|
534 | + \gc_collect_cycles(); |
|
535 | + } |
|
536 | + |
|
537 | + return $this; |
|
538 | + } |
|
539 | + |
|
540 | + /** |
|
541 | + * @throws PhpfastcacheLogicException |
|
542 | + */ |
|
543 | + public function attachItem(CacheItemInterface $item): static |
|
544 | + { |
|
545 | + if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
546 | + throw new PhpfastcacheLogicException( |
|
547 | + 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! |
|
548 | 548 | You probably tried to re-attach a detached item which has been already retrieved from cache.' |
549 | - ); |
|
550 | - } |
|
551 | - |
|
552 | - if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
553 | - throw new PhpfastcacheLogicException( |
|
554 | - 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
555 | - ); |
|
556 | - } |
|
557 | - |
|
558 | - $this->itemInstances[$item->getKey()] = $item; |
|
559 | - |
|
560 | - return $this; |
|
561 | - } |
|
562 | - |
|
563 | - public function isAttached(CacheItemInterface $item): bool |
|
564 | - { |
|
565 | - if (isset($this->itemInstances[$item->getKey()])) { |
|
566 | - return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
|
567 | - } |
|
568 | - return false; |
|
569 | - } |
|
570 | - |
|
571 | - protected function validateCacheKeys(string ...$keys): void |
|
572 | - { |
|
573 | - foreach ($keys as $key) { |
|
574 | - if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
|
575 | - throw new PhpfastcacheInvalidArgumentException( |
|
576 | - 'Unsupported key character detected: "' . $matches[1] . '". |
|
549 | + ); |
|
550 | + } |
|
551 | + |
|
552 | + if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
553 | + throw new PhpfastcacheLogicException( |
|
554 | + 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
555 | + ); |
|
556 | + } |
|
557 | + |
|
558 | + $this->itemInstances[$item->getKey()] = $item; |
|
559 | + |
|
560 | + return $this; |
|
561 | + } |
|
562 | + |
|
563 | + public function isAttached(CacheItemInterface $item): bool |
|
564 | + { |
|
565 | + if (isset($this->itemInstances[$item->getKey()])) { |
|
566 | + return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
|
567 | + } |
|
568 | + return false; |
|
569 | + } |
|
570 | + |
|
571 | + protected function validateCacheKeys(string ...$keys): void |
|
572 | + { |
|
573 | + foreach ($keys as $key) { |
|
574 | + if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
|
575 | + throw new PhpfastcacheInvalidArgumentException( |
|
576 | + 'Unsupported key character detected: "' . $matches[1] . '". |
|
577 | 577 | Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers' |
578 | - ); |
|
579 | - } |
|
580 | - } |
|
581 | - } |
|
582 | - |
|
583 | - protected function handleExpiredCacheItem(ExtendedCacheItemInterface $item): void |
|
584 | - { |
|
585 | - if ($item->isExpired()) { |
|
586 | - /** |
|
587 | - * Using driverDelete() instead of delete() |
|
588 | - * to avoid infinite loop caused by |
|
589 | - * getItem() call in delete() method |
|
590 | - * As we MUST return an item in any |
|
591 | - * way, we do not de-register here |
|
592 | - */ |
|
593 | - $this->driverDelete($item->getKey(), $item->getEncodedKey()); |
|
594 | - |
|
595 | - /** |
|
596 | - * Reset the Item |
|
597 | - */ |
|
598 | - $item->set(null) |
|
599 | - ->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())) |
|
600 | - ->setHit(false) |
|
601 | - ->setTags([]); |
|
602 | - |
|
603 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
604 | - /** |
|
605 | - * If the itemDetailedDate has been |
|
606 | - * set after caching, we MUST inject |
|
607 | - * a new DateTime object on the fly |
|
608 | - */ |
|
609 | - $item->setCreationDate(new DateTime()); |
|
610 | - $item->setModificationDate(new DateTime()); |
|
611 | - } |
|
612 | - } else { |
|
613 | - $item->setHit(true); |
|
614 | - } |
|
615 | - } |
|
616 | - |
|
617 | - /** |
|
618 | - * @param ExtendedCacheItemInterface[] $items |
|
619 | - * @param bool $encoded |
|
620 | - * @param string $keyPrefix |
|
621 | - * @return string[] |
|
622 | - */ |
|
623 | - protected function getKeys(array $items, bool $encoded = false, string $keyPrefix = ''): array |
|
624 | - { |
|
625 | - return array_map( |
|
626 | - static fn(ExtendedCacheItemInterface $item) => $keyPrefix . ($encoded ? $item->getEncodedKey() : $item->getKey()), |
|
627 | - $items |
|
628 | - ); |
|
629 | - } |
|
578 | + ); |
|
579 | + } |
|
580 | + } |
|
581 | + } |
|
582 | + |
|
583 | + protected function handleExpiredCacheItem(ExtendedCacheItemInterface $item): void |
|
584 | + { |
|
585 | + if ($item->isExpired()) { |
|
586 | + /** |
|
587 | + * Using driverDelete() instead of delete() |
|
588 | + * to avoid infinite loop caused by |
|
589 | + * getItem() call in delete() method |
|
590 | + * As we MUST return an item in any |
|
591 | + * way, we do not de-register here |
|
592 | + */ |
|
593 | + $this->driverDelete($item->getKey(), $item->getEncodedKey()); |
|
594 | + |
|
595 | + /** |
|
596 | + * Reset the Item |
|
597 | + */ |
|
598 | + $item->set(null) |
|
599 | + ->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())) |
|
600 | + ->setHit(false) |
|
601 | + ->setTags([]); |
|
602 | + |
|
603 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
604 | + /** |
|
605 | + * If the itemDetailedDate has been |
|
606 | + * set after caching, we MUST inject |
|
607 | + * a new DateTime object on the fly |
|
608 | + */ |
|
609 | + $item->setCreationDate(new DateTime()); |
|
610 | + $item->setModificationDate(new DateTime()); |
|
611 | + } |
|
612 | + } else { |
|
613 | + $item->setHit(true); |
|
614 | + } |
|
615 | + } |
|
616 | + |
|
617 | + /** |
|
618 | + * @param ExtendedCacheItemInterface[] $items |
|
619 | + * @param bool $encoded |
|
620 | + * @param string $keyPrefix |
|
621 | + * @return string[] |
|
622 | + */ |
|
623 | + protected function getKeys(array $items, bool $encoded = false, string $keyPrefix = ''): array |
|
624 | + { |
|
625 | + return array_map( |
|
626 | + static fn(ExtendedCacheItemInterface $item) => $keyPrefix . ($encoded ? $item->getEncodedKey() : $item->getKey()), |
|
627 | + $items |
|
628 | + ); |
|
629 | + } |
|
630 | 630 | } |
@@ -21,268 +21,268 @@ |
||
21 | 21 | |
22 | 22 | interface TaggableCacheItemPoolInterface |
23 | 23 | { |
24 | - public const DRIVER_TAGS_KEY_PREFIX = '_TAG_'; |
|
24 | + public const DRIVER_TAGS_KEY_PREFIX = '_TAG_'; |
|
25 | 25 | |
26 | - public const DRIVER_TAGS_WRAPPER_INDEX = 'g'; |
|
26 | + public const DRIVER_TAGS_WRAPPER_INDEX = 'g'; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
30 | - * Default behavior |
|
31 | - */ |
|
32 | - public const TAG_STRATEGY_ONE = 1; |
|
28 | + /** |
|
29 | + * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
30 | + * Default behavior |
|
31 | + */ |
|
32 | + public const TAG_STRATEGY_ONE = 1; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
36 | - * The cache item *CAN* have additional tag(s) |
|
37 | - */ |
|
38 | - public const TAG_STRATEGY_ALL = 2; |
|
34 | + /** |
|
35 | + * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
36 | + * The cache item *CAN* have additional tag(s) |
|
37 | + */ |
|
38 | + public const TAG_STRATEGY_ALL = 2; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
42 | - * The cache item *CANNOT* have additional tag(s) |
|
43 | - */ |
|
44 | - public const TAG_STRATEGY_ONLY = 4; |
|
40 | + /** |
|
41 | + * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
42 | + * The cache item *CANNOT* have additional tag(s) |
|
43 | + */ |
|
44 | + public const TAG_STRATEGY_ONLY = 4; |
|
45 | 45 | |
46 | - /** |
|
47 | - * Returns a traversable set of cache items by a tag name. |
|
48 | - * |
|
49 | - * @param string $tagName |
|
50 | - * An indexed array of keys of items to retrieve. |
|
51 | - * |
|
52 | - * @param int $strategy |
|
53 | - * |
|
54 | - * @return ExtendedCacheItemInterface[] |
|
55 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
56 | - * each item. A Cache item will be returned for each key, even if that |
|
57 | - * key is not found. However, if no keys are specified then an empty |
|
58 | - * traversable MUST be returned instead. |
|
59 | - * @throws InvalidArgumentException |
|
60 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
61 | - * MUST be thrown. |
|
62 | - * |
|
63 | - */ |
|
64 | - public function getItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
46 | + /** |
|
47 | + * Returns a traversable set of cache items by a tag name. |
|
48 | + * |
|
49 | + * @param string $tagName |
|
50 | + * An indexed array of keys of items to retrieve. |
|
51 | + * |
|
52 | + * @param int $strategy |
|
53 | + * |
|
54 | + * @return ExtendedCacheItemInterface[] |
|
55 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
56 | + * each item. A Cache item will be returned for each key, even if that |
|
57 | + * key is not found. However, if no keys are specified then an empty |
|
58 | + * traversable MUST be returned instead. |
|
59 | + * @throws InvalidArgumentException |
|
60 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
61 | + * MUST be thrown. |
|
62 | + * |
|
63 | + */ |
|
64 | + public function getItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
65 | 65 | |
66 | - /** |
|
67 | - * Returns a traversable set of cache items by one of multiple tag names. |
|
68 | - * |
|
69 | - * @param string[] $tagNames |
|
70 | - * An indexed array of keys of items to retrieve. |
|
71 | - * |
|
72 | - * @param int $strategy |
|
73 | - * |
|
74 | - * @return ExtendedCacheItemInterface[] |
|
75 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
76 | - * each item. A Cache item will be returned for each key, even if that |
|
77 | - * key is not found. However, if no keys are specified then an empty |
|
78 | - * traversable MUST be returned instead. |
|
79 | - * @throws InvalidArgumentException |
|
80 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
81 | - * MUST be thrown. |
|
82 | - * |
|
83 | - */ |
|
84 | - public function getItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
66 | + /** |
|
67 | + * Returns a traversable set of cache items by one of multiple tag names. |
|
68 | + * |
|
69 | + * @param string[] $tagNames |
|
70 | + * An indexed array of keys of items to retrieve. |
|
71 | + * |
|
72 | + * @param int $strategy |
|
73 | + * |
|
74 | + * @return ExtendedCacheItemInterface[] |
|
75 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
76 | + * each item. A Cache item will be returned for each key, even if that |
|
77 | + * key is not found. However, if no keys are specified then an empty |
|
78 | + * traversable MUST be returned instead. |
|
79 | + * @throws InvalidArgumentException |
|
80 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
81 | + * MUST be thrown. |
|
82 | + * |
|
83 | + */ |
|
84 | + public function getItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
85 | 85 | |
86 | - /** |
|
87 | - * Returns A json string that represents an array of items by tags-based. |
|
88 | - * |
|
89 | - * @param string[] $tagNames |
|
90 | - * An indexed array of keys of items to retrieve. |
|
91 | - * @param int $option \json_encode() options |
|
92 | - * @param int $depth \json_encode() depth |
|
93 | - * @param int $strategy |
|
94 | - * |
|
95 | - * @return string |
|
96 | - * @throws InvalidArgumentException |
|
97 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
98 | - * MUST be thrown. |
|
99 | - * |
|
100 | - */ |
|
101 | - public function getItemsByTagsAsJsonString(array $tagNames, int $option = \JSON_THROW_ON_ERROR, int $depth = 512, int $strategy = self::TAG_STRATEGY_ONE): string; |
|
86 | + /** |
|
87 | + * Returns A json string that represents an array of items by tags-based. |
|
88 | + * |
|
89 | + * @param string[] $tagNames |
|
90 | + * An indexed array of keys of items to retrieve. |
|
91 | + * @param int $option \json_encode() options |
|
92 | + * @param int $depth \json_encode() depth |
|
93 | + * @param int $strategy |
|
94 | + * |
|
95 | + * @return string |
|
96 | + * @throws InvalidArgumentException |
|
97 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
98 | + * MUST be thrown. |
|
99 | + * |
|
100 | + */ |
|
101 | + public function getItemsByTagsAsJsonString(array $tagNames, int $option = \JSON_THROW_ON_ERROR, int $depth = 512, int $strategy = self::TAG_STRATEGY_ONE): string; |
|
102 | 102 | |
103 | - /** |
|
104 | - * Removes the item from the pool by tag. |
|
105 | - * |
|
106 | - * @param string $tagName |
|
107 | - * The tag for which to delete |
|
108 | - * |
|
109 | - * @param int $strategy |
|
110 | - * |
|
111 | - * @return bool |
|
112 | - * True if the item was successfully removed. False if there was an error. |
|
113 | - * @throws InvalidArgumentException |
|
114 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
115 | - * MUST be thrown. |
|
116 | - * |
|
117 | - */ |
|
118 | - public function deleteItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
103 | + /** |
|
104 | + * Removes the item from the pool by tag. |
|
105 | + * |
|
106 | + * @param string $tagName |
|
107 | + * The tag for which to delete |
|
108 | + * |
|
109 | + * @param int $strategy |
|
110 | + * |
|
111 | + * @return bool |
|
112 | + * True if the item was successfully removed. False if there was an error. |
|
113 | + * @throws InvalidArgumentException |
|
114 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
115 | + * MUST be thrown. |
|
116 | + * |
|
117 | + */ |
|
118 | + public function deleteItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
119 | 119 | |
120 | - /** |
|
121 | - * Removes the item from the pool by one of multiple tag names. |
|
122 | - * |
|
123 | - * @param string[] $tagNames |
|
124 | - * The tag for which to delete |
|
125 | - * |
|
126 | - * @param int $strategy |
|
127 | - * |
|
128 | - * @return bool |
|
129 | - * True if the items were successfully removed. False if there was an error. |
|
130 | - * @throws InvalidArgumentException |
|
131 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
132 | - * MUST be thrown. |
|
133 | - * |
|
134 | - */ |
|
135 | - public function deleteItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
120 | + /** |
|
121 | + * Removes the item from the pool by one of multiple tag names. |
|
122 | + * |
|
123 | + * @param string[] $tagNames |
|
124 | + * The tag for which to delete |
|
125 | + * |
|
126 | + * @param int $strategy |
|
127 | + * |
|
128 | + * @return bool |
|
129 | + * True if the items were successfully removed. False if there was an error. |
|
130 | + * @throws InvalidArgumentException |
|
131 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
132 | + * MUST be thrown. |
|
133 | + * |
|
134 | + */ |
|
135 | + public function deleteItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
136 | 136 | |
137 | - /** |
|
138 | - * Increment the items from the pool by tag. |
|
139 | - * |
|
140 | - * @param string $tagName |
|
141 | - * The tag for which to increment |
|
142 | - * |
|
143 | - * @param int $step |
|
144 | - * |
|
145 | - * @param int $strategy |
|
146 | - * |
|
147 | - * @return bool |
|
148 | - * True if the item was successfully incremented. False if there was an error. |
|
149 | - * @throws InvalidArgumentException |
|
150 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
151 | - * MUST be thrown. |
|
152 | - * |
|
153 | - */ |
|
154 | - public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
137 | + /** |
|
138 | + * Increment the items from the pool by tag. |
|
139 | + * |
|
140 | + * @param string $tagName |
|
141 | + * The tag for which to increment |
|
142 | + * |
|
143 | + * @param int $step |
|
144 | + * |
|
145 | + * @param int $strategy |
|
146 | + * |
|
147 | + * @return bool |
|
148 | + * True if the item was successfully incremented. False if there was an error. |
|
149 | + * @throws InvalidArgumentException |
|
150 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
151 | + * MUST be thrown. |
|
152 | + * |
|
153 | + */ |
|
154 | + public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
155 | 155 | |
156 | - /** |
|
157 | - * Increment the items from the pool by one of multiple tag names. |
|
158 | - * |
|
159 | - * @param string[] $tagNames |
|
160 | - * The tag for which to increment |
|
161 | - * |
|
162 | - * @param int $step |
|
163 | - * |
|
164 | - * @param int $strategy |
|
165 | - * |
|
166 | - * @return bool |
|
167 | - * True if the items were successfully incremented. False if there was an error. |
|
168 | - * @throws InvalidArgumentException |
|
169 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
170 | - * MUST be thrown. |
|
171 | - * |
|
172 | - */ |
|
173 | - public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
156 | + /** |
|
157 | + * Increment the items from the pool by one of multiple tag names. |
|
158 | + * |
|
159 | + * @param string[] $tagNames |
|
160 | + * The tag for which to increment |
|
161 | + * |
|
162 | + * @param int $step |
|
163 | + * |
|
164 | + * @param int $strategy |
|
165 | + * |
|
166 | + * @return bool |
|
167 | + * True if the items were successfully incremented. False if there was an error. |
|
168 | + * @throws InvalidArgumentException |
|
169 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
170 | + * MUST be thrown. |
|
171 | + * |
|
172 | + */ |
|
173 | + public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
174 | 174 | |
175 | - /** |
|
176 | - * Decrement the items from the pool by tag. |
|
177 | - * |
|
178 | - * @param string $tagName |
|
179 | - * The tag for which to decrement |
|
180 | - * |
|
181 | - * @param int $step |
|
182 | - * |
|
183 | - * @param int $strategy |
|
184 | - * |
|
185 | - * @return bool |
|
186 | - * True if the item was successfully decremented. False if there was an error. |
|
187 | - * @throws InvalidArgumentException |
|
188 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
189 | - * MUST be thrown. |
|
190 | - * |
|
191 | - */ |
|
192 | - public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
175 | + /** |
|
176 | + * Decrement the items from the pool by tag. |
|
177 | + * |
|
178 | + * @param string $tagName |
|
179 | + * The tag for which to decrement |
|
180 | + * |
|
181 | + * @param int $step |
|
182 | + * |
|
183 | + * @param int $strategy |
|
184 | + * |
|
185 | + * @return bool |
|
186 | + * True if the item was successfully decremented. False if there was an error. |
|
187 | + * @throws InvalidArgumentException |
|
188 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
189 | + * MUST be thrown. |
|
190 | + * |
|
191 | + */ |
|
192 | + public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
193 | 193 | |
194 | - /** |
|
195 | - * Decrement the items from the pool by one of multiple tag names. |
|
196 | - * |
|
197 | - * @param string[] $tagNames |
|
198 | - * The tag for which to decrement |
|
199 | - * |
|
200 | - * @param int $step |
|
201 | - * |
|
202 | - * @param int $strategy |
|
203 | - * |
|
204 | - * @return bool |
|
205 | - * True if the item was successfully decremented. False if there was an error. |
|
206 | - * @throws InvalidArgumentException |
|
207 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
208 | - * MUST be thrown. |
|
209 | - * |
|
210 | - */ |
|
211 | - public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
194 | + /** |
|
195 | + * Decrement the items from the pool by one of multiple tag names. |
|
196 | + * |
|
197 | + * @param string[] $tagNames |
|
198 | + * The tag for which to decrement |
|
199 | + * |
|
200 | + * @param int $step |
|
201 | + * |
|
202 | + * @param int $strategy |
|
203 | + * |
|
204 | + * @return bool |
|
205 | + * True if the item was successfully decremented. False if there was an error. |
|
206 | + * @throws InvalidArgumentException |
|
207 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
208 | + * MUST be thrown. |
|
209 | + * |
|
210 | + */ |
|
211 | + public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
212 | 212 | |
213 | - /** |
|
214 | - * Decrement the items from the pool by tag. |
|
215 | - * |
|
216 | - * @param string $tagName |
|
217 | - * The tag for which to append |
|
218 | - * |
|
219 | - * @param array<mixed>|string $data |
|
220 | - * |
|
221 | - * @param int $strategy |
|
222 | - * |
|
223 | - * @return bool |
|
224 | - * True if the item was successfully appended. False if there was an error. |
|
225 | - * @throws InvalidArgumentException |
|
226 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
227 | - * MUST be thrown. |
|
228 | - * |
|
229 | - */ |
|
230 | - public function appendItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
213 | + /** |
|
214 | + * Decrement the items from the pool by tag. |
|
215 | + * |
|
216 | + * @param string $tagName |
|
217 | + * The tag for which to append |
|
218 | + * |
|
219 | + * @param array<mixed>|string $data |
|
220 | + * |
|
221 | + * @param int $strategy |
|
222 | + * |
|
223 | + * @return bool |
|
224 | + * True if the item was successfully appended. False if there was an error. |
|
225 | + * @throws InvalidArgumentException |
|
226 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
227 | + * MUST be thrown. |
|
228 | + * |
|
229 | + */ |
|
230 | + public function appendItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
231 | 231 | |
232 | - /** |
|
233 | - * Append the items from the pool by one of multiple tag names. |
|
234 | - * |
|
235 | - * @param string[] $tagNames |
|
236 | - * The tag for which to append |
|
237 | - * |
|
238 | - * @param array<mixed>|string $data |
|
239 | - * |
|
240 | - * @param int $strategy |
|
241 | - * |
|
242 | - * @return bool |
|
243 | - * True if the items were successfully appended. False if there was an error. |
|
244 | - * @throws InvalidArgumentException |
|
245 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
246 | - * MUST be thrown. |
|
247 | - * |
|
248 | - */ |
|
249 | - public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
232 | + /** |
|
233 | + * Append the items from the pool by one of multiple tag names. |
|
234 | + * |
|
235 | + * @param string[] $tagNames |
|
236 | + * The tag for which to append |
|
237 | + * |
|
238 | + * @param array<mixed>|string $data |
|
239 | + * |
|
240 | + * @param int $strategy |
|
241 | + * |
|
242 | + * @return bool |
|
243 | + * True if the items were successfully appended. False if there was an error. |
|
244 | + * @throws InvalidArgumentException |
|
245 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
246 | + * MUST be thrown. |
|
247 | + * |
|
248 | + */ |
|
249 | + public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
250 | 250 | |
251 | - /** |
|
252 | - * Prepend the items from the pool by tag. |
|
253 | - * |
|
254 | - * @param string $tagName |
|
255 | - * The tag for which to prepend |
|
256 | - * |
|
257 | - * @param array<mixed>|string $data |
|
258 | - * |
|
259 | - * @param int $strategy |
|
260 | - * |
|
261 | - * @return bool |
|
262 | - * True if the item was successfully prepended. False if there was an error. |
|
263 | - * @throws InvalidArgumentException |
|
264 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
265 | - * MUST be thrown. |
|
266 | - * |
|
267 | - */ |
|
268 | - public function prependItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
251 | + /** |
|
252 | + * Prepend the items from the pool by tag. |
|
253 | + * |
|
254 | + * @param string $tagName |
|
255 | + * The tag for which to prepend |
|
256 | + * |
|
257 | + * @param array<mixed>|string $data |
|
258 | + * |
|
259 | + * @param int $strategy |
|
260 | + * |
|
261 | + * @return bool |
|
262 | + * True if the item was successfully prepended. False if there was an error. |
|
263 | + * @throws InvalidArgumentException |
|
264 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
265 | + * MUST be thrown. |
|
266 | + * |
|
267 | + */ |
|
268 | + public function prependItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
269 | 269 | |
270 | - /** |
|
271 | - * Prepend the items from the pool by one of multiple tag names. |
|
272 | - * |
|
273 | - * @param string[] $tagNames |
|
274 | - * The tag for which to prepend |
|
275 | - * |
|
276 | - * @param array<mixed>|string $data |
|
277 | - * |
|
278 | - * @param int $strategy |
|
279 | - * |
|
280 | - * @return bool |
|
281 | - * True if the item was successfully prepended. False if there was an error. |
|
282 | - * @throws InvalidArgumentException |
|
283 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
284 | - * MUST be thrown. |
|
285 | - * |
|
286 | - */ |
|
287 | - public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
270 | + /** |
|
271 | + * Prepend the items from the pool by one of multiple tag names. |
|
272 | + * |
|
273 | + * @param string[] $tagNames |
|
274 | + * The tag for which to prepend |
|
275 | + * |
|
276 | + * @param array<mixed>|string $data |
|
277 | + * |
|
278 | + * @param int $strategy |
|
279 | + * |
|
280 | + * @return bool |
|
281 | + * True if the item was successfully prepended. False if there was an error. |
|
282 | + * @throws InvalidArgumentException |
|
283 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
284 | + * MUST be thrown. |
|
285 | + * |
|
286 | + */ |
|
287 | + public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
288 | 288 | } |
@@ -24,101 +24,101 @@ |
||
24 | 24 | |
25 | 25 | trait DriverPoolAbstractTrait |
26 | 26 | { |
27 | - /** |
|
28 | - * @return bool |
|
29 | - */ |
|
30 | - abstract protected function driverCheck(): bool; |
|
31 | - |
|
32 | - /** |
|
33 | - * @return bool |
|
34 | - */ |
|
35 | - abstract protected function driverConnect(): bool; |
|
36 | - |
|
37 | - /** |
|
38 | - * @param ExtendedCacheItemInterface $item |
|
39 | - * @return ?array<string, mixed> |
|
40 | - */ |
|
41 | - abstract protected function driverRead(ExtendedCacheItemInterface $item): ?array; |
|
42 | - |
|
43 | - /** |
|
44 | - * @param ExtendedCacheItemInterface ...$items |
|
45 | - * @return array<array<string, mixed>> |
|
46 | - * @throws PhpfastcacheUnsupportedMethodException |
|
47 | - */ |
|
48 | - protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
49 | - { |
|
50 | - throw new PhpfastcacheUnsupportedMethodException(); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @return \Traversable<int, string> |
|
55 | - * @throws PhpfastcacheUnsupportedMethodException |
|
56 | - */ |
|
57 | - protected function driverReadAllKeys(string $pattern = ''): iterable |
|
58 | - { |
|
59 | - throw new PhpfastcacheUnsupportedMethodException( |
|
60 | - sprintf( |
|
61 | - 'The "readAll" operation is unsupported by the the "%s" driver. See the Wiki for more information at %s', |
|
62 | - $this->getDriverName(), |
|
63 | - Wiki::FETCH_ALL_KEY_URL, |
|
64 | - ) |
|
65 | - ); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @param ExtendedCacheItemInterface $item |
|
70 | - * @return bool |
|
71 | - */ |
|
72 | - abstract protected function driverWrite(ExtendedCacheItemInterface $item): bool; |
|
73 | - |
|
74 | - /** |
|
75 | - * @param ExtendedCacheItemInterface ...$item |
|
76 | - * @return bool |
|
77 | - * @throws PhpfastcacheUnsupportedMethodException |
|
78 | - */ |
|
79 | - protected function driverWriteMultiple(ExtendedCacheItemInterface ...$item): bool |
|
80 | - { |
|
81 | - /** |
|
82 | - * @todo Implement bulk writes to be for v10: |
|
83 | - * For methods commit() and saveMultiple() |
|
84 | - */ |
|
85 | - throw new PhpfastcacheUnsupportedMethodException(); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * @param string $key |
|
91 | - * @param string $encodedKey |
|
92 | - * @return bool |
|
93 | - */ |
|
94 | - abstract protected function driverDelete(string $key, string $encodedKey): bool; |
|
95 | - |
|
96 | - /** |
|
97 | - * @param string[] $keys |
|
98 | - * @return bool |
|
99 | - * @throws PhpfastcacheUnsupportedMethodException |
|
100 | - */ |
|
101 | - protected function driverDeleteMultiple(array $keys): bool |
|
102 | - { |
|
103 | - throw new PhpfastcacheUnsupportedMethodException(); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return bool |
|
108 | - */ |
|
109 | - abstract protected function driverClear(): bool; |
|
110 | - |
|
111 | - /** |
|
112 | - * @param CacheItemInterface $item |
|
113 | - * @param string $expectedClassType |
|
114 | - * @throws PhpfastcacheInvalidArgumentException |
|
115 | - */ |
|
116 | - protected function assertCacheItemType(CacheItemInterface $item, string $expectedClassType): void |
|
117 | - { |
|
118 | - if (!($item instanceof $expectedClassType)) { |
|
119 | - throw new PhpfastcacheInvalidArgumentException( |
|
120 | - \sprintf('Cross-driver type confusion detected: Expected "%s" object, got "%s"', $expectedClassType, $item::class) |
|
121 | - ); |
|
122 | - } |
|
123 | - } |
|
27 | + /** |
|
28 | + * @return bool |
|
29 | + */ |
|
30 | + abstract protected function driverCheck(): bool; |
|
31 | + |
|
32 | + /** |
|
33 | + * @return bool |
|
34 | + */ |
|
35 | + abstract protected function driverConnect(): bool; |
|
36 | + |
|
37 | + /** |
|
38 | + * @param ExtendedCacheItemInterface $item |
|
39 | + * @return ?array<string, mixed> |
|
40 | + */ |
|
41 | + abstract protected function driverRead(ExtendedCacheItemInterface $item): ?array; |
|
42 | + |
|
43 | + /** |
|
44 | + * @param ExtendedCacheItemInterface ...$items |
|
45 | + * @return array<array<string, mixed>> |
|
46 | + * @throws PhpfastcacheUnsupportedMethodException |
|
47 | + */ |
|
48 | + protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
49 | + { |
|
50 | + throw new PhpfastcacheUnsupportedMethodException(); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @return \Traversable<int, string> |
|
55 | + * @throws PhpfastcacheUnsupportedMethodException |
|
56 | + */ |
|
57 | + protected function driverReadAllKeys(string $pattern = ''): iterable |
|
58 | + { |
|
59 | + throw new PhpfastcacheUnsupportedMethodException( |
|
60 | + sprintf( |
|
61 | + 'The "readAll" operation is unsupported by the the "%s" driver. See the Wiki for more information at %s', |
|
62 | + $this->getDriverName(), |
|
63 | + Wiki::FETCH_ALL_KEY_URL, |
|
64 | + ) |
|
65 | + ); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @param ExtendedCacheItemInterface $item |
|
70 | + * @return bool |
|
71 | + */ |
|
72 | + abstract protected function driverWrite(ExtendedCacheItemInterface $item): bool; |
|
73 | + |
|
74 | + /** |
|
75 | + * @param ExtendedCacheItemInterface ...$item |
|
76 | + * @return bool |
|
77 | + * @throws PhpfastcacheUnsupportedMethodException |
|
78 | + */ |
|
79 | + protected function driverWriteMultiple(ExtendedCacheItemInterface ...$item): bool |
|
80 | + { |
|
81 | + /** |
|
82 | + * @todo Implement bulk writes to be for v10: |
|
83 | + * For methods commit() and saveMultiple() |
|
84 | + */ |
|
85 | + throw new PhpfastcacheUnsupportedMethodException(); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * @param string $key |
|
91 | + * @param string $encodedKey |
|
92 | + * @return bool |
|
93 | + */ |
|
94 | + abstract protected function driverDelete(string $key, string $encodedKey): bool; |
|
95 | + |
|
96 | + /** |
|
97 | + * @param string[] $keys |
|
98 | + * @return bool |
|
99 | + * @throws PhpfastcacheUnsupportedMethodException |
|
100 | + */ |
|
101 | + protected function driverDeleteMultiple(array $keys): bool |
|
102 | + { |
|
103 | + throw new PhpfastcacheUnsupportedMethodException(); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return bool |
|
108 | + */ |
|
109 | + abstract protected function driverClear(): bool; |
|
110 | + |
|
111 | + /** |
|
112 | + * @param CacheItemInterface $item |
|
113 | + * @param string $expectedClassType |
|
114 | + * @throws PhpfastcacheInvalidArgumentException |
|
115 | + */ |
|
116 | + protected function assertCacheItemType(CacheItemInterface $item, string $expectedClassType): void |
|
117 | + { |
|
118 | + if (!($item instanceof $expectedClassType)) { |
|
119 | + throw new PhpfastcacheInvalidArgumentException( |
|
120 | + \sprintf('Cross-driver type confusion detected: Expected "%s" object, got "%s"', $expectedClassType, $item::class) |
|
121 | + ); |
|
122 | + } |
|
123 | + } |
|
124 | 124 | } |