@@ -26,103 +26,103 @@ |
||
26 | 26 | */ |
27 | 27 | trait TaggableCacheItemTrait |
28 | 28 | { |
29 | - /** |
|
30 | - * @param array $tagNames |
|
31 | - * @return ExtendedCacheItemInterface |
|
32 | - * @throws PhpfastcacheInvalidArgumentException |
|
33 | - */ |
|
34 | - public function addTags(array $tagNames): ExtendedCacheItemInterface |
|
35 | - { |
|
36 | - foreach ($tagNames as $tagName) { |
|
37 | - $this->addTag($tagName); |
|
38 | - } |
|
39 | - |
|
40 | - return $this; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * @param $tagName |
|
45 | - * @return ExtendedCacheItemInterface |
|
46 | - * @throws PhpfastcacheInvalidArgumentException |
|
47 | - */ |
|
48 | - public function addTag(string $tagName): ExtendedCacheItemInterface |
|
49 | - { |
|
50 | - if (\is_string($tagName)) { |
|
51 | - $this->tags = \array_unique(\array_merge($this->tags, [$tagName])); |
|
52 | - |
|
53 | - return $this; |
|
54 | - } |
|
55 | - |
|
56 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param array $tags |
|
61 | - * @return ExtendedCacheItemInterface |
|
62 | - * @throws PhpfastcacheInvalidArgumentException |
|
63 | - */ |
|
64 | - public function setTags(array $tags): ExtendedCacheItemInterface |
|
65 | - { |
|
66 | - if (\count($tags)) { |
|
67 | - if (\array_filter($tags, 'is_string')) { |
|
68 | - $this->tags = $tags; |
|
69 | - } else { |
|
70 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string'); |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - return $this; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @return array |
|
79 | - */ |
|
80 | - public function getTags(): array |
|
81 | - { |
|
82 | - return $this->tags; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param string $separator |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - public function getTagsAsString(string $separator = ', '): string |
|
90 | - { |
|
91 | - return \implode($separator, $this->tags); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param array $tagNames |
|
96 | - * @return ExtendedCacheItemInterface |
|
97 | - */ |
|
98 | - public function removeTags(array $tagNames): ExtendedCacheItemInterface |
|
99 | - { |
|
100 | - foreach ($tagNames as $tagName) { |
|
101 | - $this->removeTag($tagName); |
|
102 | - } |
|
103 | - |
|
104 | - return $this; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @param $tagName |
|
109 | - * @return ExtendedCacheItemInterface |
|
110 | - */ |
|
111 | - public function removeTag(string $tagName): ExtendedCacheItemInterface |
|
112 | - { |
|
113 | - if (($key = \array_search($tagName, $this->tags, true)) !== false) { |
|
114 | - unset($this->tags[$key]); |
|
115 | - $this->removedTags[] = $tagName; |
|
116 | - } |
|
117 | - |
|
118 | - return $this; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @return array |
|
123 | - */ |
|
124 | - public function getRemovedTags(): array |
|
125 | - { |
|
126 | - return \array_diff($this->removedTags, $this->tags); |
|
127 | - } |
|
29 | + /** |
|
30 | + * @param array $tagNames |
|
31 | + * @return ExtendedCacheItemInterface |
|
32 | + * @throws PhpfastcacheInvalidArgumentException |
|
33 | + */ |
|
34 | + public function addTags(array $tagNames): ExtendedCacheItemInterface |
|
35 | + { |
|
36 | + foreach ($tagNames as $tagName) { |
|
37 | + $this->addTag($tagName); |
|
38 | + } |
|
39 | + |
|
40 | + return $this; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * @param $tagName |
|
45 | + * @return ExtendedCacheItemInterface |
|
46 | + * @throws PhpfastcacheInvalidArgumentException |
|
47 | + */ |
|
48 | + public function addTag(string $tagName): ExtendedCacheItemInterface |
|
49 | + { |
|
50 | + if (\is_string($tagName)) { |
|
51 | + $this->tags = \array_unique(\array_merge($this->tags, [$tagName])); |
|
52 | + |
|
53 | + return $this; |
|
54 | + } |
|
55 | + |
|
56 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param array $tags |
|
61 | + * @return ExtendedCacheItemInterface |
|
62 | + * @throws PhpfastcacheInvalidArgumentException |
|
63 | + */ |
|
64 | + public function setTags(array $tags): ExtendedCacheItemInterface |
|
65 | + { |
|
66 | + if (\count($tags)) { |
|
67 | + if (\array_filter($tags, 'is_string')) { |
|
68 | + $this->tags = $tags; |
|
69 | + } else { |
|
70 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be an array of string'); |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + return $this; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @return array |
|
79 | + */ |
|
80 | + public function getTags(): array |
|
81 | + { |
|
82 | + return $this->tags; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param string $separator |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + public function getTagsAsString(string $separator = ', '): string |
|
90 | + { |
|
91 | + return \implode($separator, $this->tags); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param array $tagNames |
|
96 | + * @return ExtendedCacheItemInterface |
|
97 | + */ |
|
98 | + public function removeTags(array $tagNames): ExtendedCacheItemInterface |
|
99 | + { |
|
100 | + foreach ($tagNames as $tagName) { |
|
101 | + $this->removeTag($tagName); |
|
102 | + } |
|
103 | + |
|
104 | + return $this; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @param $tagName |
|
109 | + * @return ExtendedCacheItemInterface |
|
110 | + */ |
|
111 | + public function removeTag(string $tagName): ExtendedCacheItemInterface |
|
112 | + { |
|
113 | + if (($key = \array_search($tagName, $this->tags, true)) !== false) { |
|
114 | + unset($this->tags[$key]); |
|
115 | + $this->removedTags[] = $tagName; |
|
116 | + } |
|
117 | + |
|
118 | + return $this; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @return array |
|
123 | + */ |
|
124 | + public function getRemovedTags(): array |
|
125 | + { |
|
126 | + return \array_diff($this->removedTags, $this->tags); |
|
127 | + } |
|
128 | 128 | } |
@@ -29,186 +29,186 @@ |
||
29 | 29 | */ |
30 | 30 | trait ItemBaseTrait |
31 | 31 | { |
32 | - use ItemExtendedTrait; |
|
33 | - use EventManagerDispatcherTrait; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var bool |
|
37 | - */ |
|
38 | - protected $fetched = false; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $key; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var mixed |
|
47 | - */ |
|
48 | - protected $data; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var DateTimeInterface |
|
52 | - */ |
|
53 | - protected $expirationDate; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var DateTimeInterface |
|
57 | - */ |
|
58 | - protected $creationDate; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var DateTimeInterface |
|
62 | - */ |
|
63 | - protected $modificationDate; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var array |
|
67 | - */ |
|
68 | - protected $tags = []; |
|
69 | - |
|
70 | - /** |
|
71 | - * @var array |
|
72 | - */ |
|
73 | - protected $removedTags = []; |
|
74 | - |
|
75 | - /** |
|
76 | - * @var bool |
|
77 | - */ |
|
78 | - protected $isHit = false; |
|
79 | - |
|
80 | - /******************** |
|
32 | + use ItemExtendedTrait; |
|
33 | + use EventManagerDispatcherTrait; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var bool |
|
37 | + */ |
|
38 | + protected $fetched = false; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $key; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var mixed |
|
47 | + */ |
|
48 | + protected $data; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var DateTimeInterface |
|
52 | + */ |
|
53 | + protected $expirationDate; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var DateTimeInterface |
|
57 | + */ |
|
58 | + protected $creationDate; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var DateTimeInterface |
|
62 | + */ |
|
63 | + protected $modificationDate; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var array |
|
67 | + */ |
|
68 | + protected $tags = []; |
|
69 | + |
|
70 | + /** |
|
71 | + * @var array |
|
72 | + */ |
|
73 | + protected $removedTags = []; |
|
74 | + |
|
75 | + /** |
|
76 | + * @var bool |
|
77 | + */ |
|
78 | + protected $isHit = false; |
|
79 | + |
|
80 | + /******************** |
|
81 | 81 | * |
82 | 82 | * PSR-6 Methods |
83 | 83 | * |
84 | 84 | *******************/ |
85 | 85 | |
86 | - /** |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - public function getKey() |
|
90 | - { |
|
91 | - return $this->key; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return mixed |
|
96 | - */ |
|
97 | - public function get() |
|
98 | - { |
|
99 | - return $this->data; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param mixed $value |
|
104 | - * @return $this |
|
105 | - */ |
|
106 | - public function set($value) |
|
107 | - { |
|
108 | - /** |
|
109 | - * The user set a value, |
|
110 | - * therefore there is no need to |
|
111 | - * fetch from source anymore |
|
112 | - */ |
|
113 | - $this->fetched = true; |
|
114 | - $this->data = $value; |
|
115 | - |
|
116 | - /** |
|
117 | - * @eventName CacheSaveDeferredItem |
|
118 | - * @param ExtendedCacheItemInterface $this |
|
119 | - * @param mixed $value |
|
120 | - * |
|
121 | - */ |
|
122 | - $this->eventManager->dispatch('CacheItemSet', $this, $value); |
|
123 | - |
|
124 | - return $this; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @return bool |
|
129 | - */ |
|
130 | - public function isHit(): bool |
|
131 | - { |
|
132 | - return $this->isHit; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @param bool $isHit |
|
137 | - * @return ExtendedCacheItemInterface |
|
138 | - * @throws PhpfastcacheInvalidArgumentException |
|
139 | - */ |
|
140 | - public function setHit($isHit): ExtendedCacheItemInterface |
|
141 | - { |
|
142 | - if (\is_bool($isHit)) { |
|
143 | - $this->isHit = $isHit; |
|
144 | - |
|
145 | - return $this; |
|
146 | - } |
|
147 | - |
|
148 | - throw new PhpfastcacheInvalidArgumentException('$isHit must be a boolean'); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @param DateTimeInterface $expiration |
|
153 | - * @return ExtendedCacheItemInterface |
|
154 | - * @throws PhpfastcacheInvalidArgumentException |
|
155 | - */ |
|
156 | - public function expiresAt($expiration): ExtendedCacheItemInterface |
|
157 | - { |
|
158 | - if ($expiration instanceof DateTimeInterface) { |
|
159 | - /** |
|
160 | - * @eventName CacheItemExpireAt |
|
161 | - * @param ExtendedCacheItemInterface $this |
|
162 | - * @param DateTimeInterface $expiration |
|
163 | - */ |
|
164 | - $this->eventManager->dispatch('CacheItemExpireAt', $this, $expiration); |
|
165 | - $this->expirationDate = $expiration; |
|
166 | - } else { |
|
167 | - throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration)); |
|
168 | - } |
|
169 | - |
|
170 | - return $this; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param DateInterval|int $time |
|
175 | - * @return $this |
|
176 | - * @throws PhpfastcacheInvalidArgumentException |
|
177 | - */ |
|
178 | - public function expiresAfter($time) |
|
179 | - { |
|
180 | - if (\is_numeric($time)) { |
|
181 | - if ($time <= 0) { |
|
182 | - /** |
|
183 | - * 5 years, however memcached or memory cached will gone when u restart it |
|
184 | - * just recommended for sqlite. files |
|
185 | - */ |
|
186 | - $time = 30 * 24 * 3600 * 5; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @eventName CacheItemExpireAt |
|
191 | - * @param ExtendedCacheItemInterface $this |
|
192 | - * @param DateTimeInterface $expiration |
|
193 | - */ |
|
194 | - $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); |
|
195 | - |
|
196 | - $this->expirationDate = (new DateTime())->add(new DateInterval(\sprintf('PT%dS', $time))); |
|
197 | - } else { |
|
198 | - if ($time instanceof DateInterval) { |
|
199 | - /** |
|
200 | - * @eventName CacheItemExpireAt |
|
201 | - * @param ExtendedCacheItemInterface $this |
|
202 | - * @param DateTimeInterface $expiration |
|
203 | - */ |
|
204 | - $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); |
|
205 | - |
|
206 | - $this->expirationDate = (new DateTime())->add($time); |
|
207 | - } else { |
|
208 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid date format, got "%s"', \gettype($time))); |
|
209 | - } |
|
210 | - } |
|
211 | - |
|
212 | - return $this; |
|
213 | - } |
|
86 | + /** |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + public function getKey() |
|
90 | + { |
|
91 | + return $this->key; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return mixed |
|
96 | + */ |
|
97 | + public function get() |
|
98 | + { |
|
99 | + return $this->data; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param mixed $value |
|
104 | + * @return $this |
|
105 | + */ |
|
106 | + public function set($value) |
|
107 | + { |
|
108 | + /** |
|
109 | + * The user set a value, |
|
110 | + * therefore there is no need to |
|
111 | + * fetch from source anymore |
|
112 | + */ |
|
113 | + $this->fetched = true; |
|
114 | + $this->data = $value; |
|
115 | + |
|
116 | + /** |
|
117 | + * @eventName CacheSaveDeferredItem |
|
118 | + * @param ExtendedCacheItemInterface $this |
|
119 | + * @param mixed $value |
|
120 | + * |
|
121 | + */ |
|
122 | + $this->eventManager->dispatch('CacheItemSet', $this, $value); |
|
123 | + |
|
124 | + return $this; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @return bool |
|
129 | + */ |
|
130 | + public function isHit(): bool |
|
131 | + { |
|
132 | + return $this->isHit; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @param bool $isHit |
|
137 | + * @return ExtendedCacheItemInterface |
|
138 | + * @throws PhpfastcacheInvalidArgumentException |
|
139 | + */ |
|
140 | + public function setHit($isHit): ExtendedCacheItemInterface |
|
141 | + { |
|
142 | + if (\is_bool($isHit)) { |
|
143 | + $this->isHit = $isHit; |
|
144 | + |
|
145 | + return $this; |
|
146 | + } |
|
147 | + |
|
148 | + throw new PhpfastcacheInvalidArgumentException('$isHit must be a boolean'); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @param DateTimeInterface $expiration |
|
153 | + * @return ExtendedCacheItemInterface |
|
154 | + * @throws PhpfastcacheInvalidArgumentException |
|
155 | + */ |
|
156 | + public function expiresAt($expiration): ExtendedCacheItemInterface |
|
157 | + { |
|
158 | + if ($expiration instanceof DateTimeInterface) { |
|
159 | + /** |
|
160 | + * @eventName CacheItemExpireAt |
|
161 | + * @param ExtendedCacheItemInterface $this |
|
162 | + * @param DateTimeInterface $expiration |
|
163 | + */ |
|
164 | + $this->eventManager->dispatch('CacheItemExpireAt', $this, $expiration); |
|
165 | + $this->expirationDate = $expiration; |
|
166 | + } else { |
|
167 | + throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration)); |
|
168 | + } |
|
169 | + |
|
170 | + return $this; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param DateInterval|int $time |
|
175 | + * @return $this |
|
176 | + * @throws PhpfastcacheInvalidArgumentException |
|
177 | + */ |
|
178 | + public function expiresAfter($time) |
|
179 | + { |
|
180 | + if (\is_numeric($time)) { |
|
181 | + if ($time <= 0) { |
|
182 | + /** |
|
183 | + * 5 years, however memcached or memory cached will gone when u restart it |
|
184 | + * just recommended for sqlite. files |
|
185 | + */ |
|
186 | + $time = 30 * 24 * 3600 * 5; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @eventName CacheItemExpireAt |
|
191 | + * @param ExtendedCacheItemInterface $this |
|
192 | + * @param DateTimeInterface $expiration |
|
193 | + */ |
|
194 | + $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); |
|
195 | + |
|
196 | + $this->expirationDate = (new DateTime())->add(new DateInterval(\sprintf('PT%dS', $time))); |
|
197 | + } else { |
|
198 | + if ($time instanceof DateInterval) { |
|
199 | + /** |
|
200 | + * @eventName CacheItemExpireAt |
|
201 | + * @param ExtendedCacheItemInterface $this |
|
202 | + * @param DateTimeInterface $expiration |
|
203 | + */ |
|
204 | + $this->eventManager->dispatch('CacheItemExpireAfter', $this, $time); |
|
205 | + |
|
206 | + $this->expirationDate = (new DateTime())->add($time); |
|
207 | + } else { |
|
208 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid date format, got "%s"', \gettype($time))); |
|
209 | + } |
|
210 | + } |
|
211 | + |
|
212 | + return $this; |
|
213 | + } |
|
214 | 214 | } |
@@ -27,129 +27,129 @@ |
||
27 | 27 | */ |
28 | 28 | trait ExtendedCacheItemPoolTrait |
29 | 29 | { |
30 | - use CacheItemPoolTrait; |
|
31 | - use AbstractDriverPoolTrait; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var DriverIO |
|
35 | - */ |
|
36 | - protected $IO; |
|
37 | - |
|
38 | - /** |
|
39 | - * @inheritdoc |
|
40 | - */ |
|
41 | - public function getItemsAsJsonString(array $keys = [], int $option = 0, int $depth = 512): string |
|
42 | - { |
|
43 | - $callback = static function (CacheItemInterface $item) { |
|
44 | - return $item->get(); |
|
45 | - }; |
|
46 | - return \json_encode(\array_map($callback, \array_values($this->getItems($keys))), $option, $depth); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @inheritdoc |
|
51 | - */ |
|
52 | - public function detachAllItems() |
|
53 | - { |
|
54 | - foreach ($this->itemInstances as $item) { |
|
55 | - $this->detachItem($item); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param CacheItemInterface $item |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function detachItem(CacheItemInterface $item) |
|
64 | - { |
|
65 | - if (isset($this->itemInstances[$item->getKey()])) { |
|
66 | - $this->deregisterItem($item->getKey()); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param string $item |
|
72 | - * @internal This method de-register an item from $this->itemInstances |
|
73 | - */ |
|
74 | - protected function deregisterItem(string $item) |
|
75 | - { |
|
76 | - unset($this->itemInstances[$item]); |
|
77 | - |
|
78 | - if (\gc_enabled()) { |
|
79 | - \gc_collect_cycles(); |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @inheritdoc |
|
85 | - */ |
|
86 | - public function attachItem(CacheItemInterface $item) |
|
87 | - { |
|
88 | - if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
89 | - throw new PhpfastcacheLogicException( |
|
90 | - 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! You probably tried to re-attach a detached item which has been already retrieved from cache.' |
|
91 | - ); |
|
92 | - } |
|
93 | - |
|
94 | - if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
95 | - throw new PhpfastcacheLogicException( |
|
96 | - 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - $this->itemInstances[$item->getKey()] = $item; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Returns true if the item exists, is attached and the Spl Hash matches |
|
105 | - * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
106 | - * Returns null if the item does not exists |
|
107 | - * |
|
108 | - * @param CacheItemInterface $item |
|
109 | - * @return bool|null |
|
110 | - */ |
|
111 | - public function isAttached(CacheItemInterface $item) |
|
112 | - { |
|
113 | - if (isset($this->itemInstances[$item->getKey()])) { |
|
114 | - return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
|
115 | - } |
|
116 | - return null; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @inheritdoc |
|
121 | - */ |
|
122 | - public function saveMultiple(...$items): bool |
|
123 | - { |
|
124 | - if (isset($items[0]) && \is_array($items[0])) { |
|
125 | - foreach ($items[0] as $item) { |
|
126 | - $this->save($item); |
|
127 | - } |
|
128 | - return true; |
|
129 | - } |
|
130 | - |
|
131 | - if (\is_array($items)) { |
|
132 | - foreach ($items as $item) { |
|
133 | - $this->save($item); |
|
134 | - } |
|
135 | - return true; |
|
136 | - } |
|
137 | - return false; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @return DriverIO |
|
142 | - */ |
|
143 | - public function getIO(): DriverIO |
|
144 | - { |
|
145 | - return $this->IO; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function getHelp(): string |
|
152 | - { |
|
153 | - return ''; |
|
154 | - } |
|
30 | + use CacheItemPoolTrait; |
|
31 | + use AbstractDriverPoolTrait; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var DriverIO |
|
35 | + */ |
|
36 | + protected $IO; |
|
37 | + |
|
38 | + /** |
|
39 | + * @inheritdoc |
|
40 | + */ |
|
41 | + public function getItemsAsJsonString(array $keys = [], int $option = 0, int $depth = 512): string |
|
42 | + { |
|
43 | + $callback = static function (CacheItemInterface $item) { |
|
44 | + return $item->get(); |
|
45 | + }; |
|
46 | + return \json_encode(\array_map($callback, \array_values($this->getItems($keys))), $option, $depth); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @inheritdoc |
|
51 | + */ |
|
52 | + public function detachAllItems() |
|
53 | + { |
|
54 | + foreach ($this->itemInstances as $item) { |
|
55 | + $this->detachItem($item); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param CacheItemInterface $item |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function detachItem(CacheItemInterface $item) |
|
64 | + { |
|
65 | + if (isset($this->itemInstances[$item->getKey()])) { |
|
66 | + $this->deregisterItem($item->getKey()); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param string $item |
|
72 | + * @internal This method de-register an item from $this->itemInstances |
|
73 | + */ |
|
74 | + protected function deregisterItem(string $item) |
|
75 | + { |
|
76 | + unset($this->itemInstances[$item]); |
|
77 | + |
|
78 | + if (\gc_enabled()) { |
|
79 | + \gc_collect_cycles(); |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @inheritdoc |
|
85 | + */ |
|
86 | + public function attachItem(CacheItemInterface $item) |
|
87 | + { |
|
88 | + if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
89 | + throw new PhpfastcacheLogicException( |
|
90 | + 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! You probably tried to re-attach a detached item which has been already retrieved from cache.' |
|
91 | + ); |
|
92 | + } |
|
93 | + |
|
94 | + if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
95 | + throw new PhpfastcacheLogicException( |
|
96 | + 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + $this->itemInstances[$item->getKey()] = $item; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Returns true if the item exists, is attached and the Spl Hash matches |
|
105 | + * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
106 | + * Returns null if the item does not exists |
|
107 | + * |
|
108 | + * @param CacheItemInterface $item |
|
109 | + * @return bool|null |
|
110 | + */ |
|
111 | + public function isAttached(CacheItemInterface $item) |
|
112 | + { |
|
113 | + if (isset($this->itemInstances[$item->getKey()])) { |
|
114 | + return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
|
115 | + } |
|
116 | + return null; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @inheritdoc |
|
121 | + */ |
|
122 | + public function saveMultiple(...$items): bool |
|
123 | + { |
|
124 | + if (isset($items[0]) && \is_array($items[0])) { |
|
125 | + foreach ($items[0] as $item) { |
|
126 | + $this->save($item); |
|
127 | + } |
|
128 | + return true; |
|
129 | + } |
|
130 | + |
|
131 | + if (\is_array($items)) { |
|
132 | + foreach ($items as $item) { |
|
133 | + $this->save($item); |
|
134 | + } |
|
135 | + return true; |
|
136 | + } |
|
137 | + return false; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @return DriverIO |
|
142 | + */ |
|
143 | + public function getIO(): DriverIO |
|
144 | + { |
|
145 | + return $this->IO; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function getHelp(): string |
|
152 | + { |
|
153 | + return ''; |
|
154 | + } |
|
155 | 155 | } |
@@ -29,387 +29,387 @@ |
||
29 | 29 | */ |
30 | 30 | trait TaggableCacheItemPoolTrait |
31 | 31 | { |
32 | - /** |
|
33 | - * @inheritdoc |
|
34 | - */ |
|
35 | - public function getItemsByTagsAsJsonString(array $tagNames, int $option = 0, int $depth = 512, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): string |
|
36 | - { |
|
37 | - $callback = static function (CacheItemInterface $item) { |
|
38 | - return $item->get(); |
|
39 | - }; |
|
40 | - |
|
41 | - return \json_encode(\array_map($callback, \array_values($this->getItemsByTags($tagNames, $strategy))), $option, $depth); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @inheritdoc |
|
46 | - */ |
|
47 | - public function getItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
48 | - { |
|
49 | - $items = []; |
|
50 | - foreach (\array_unique($tagNames) as $tagName) { |
|
51 | - if (\is_string($tagName)) { |
|
52 | - $items[] = $this->fetchItemsByTagFromBackend($tagName); |
|
53 | - } else { |
|
54 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a a string'); |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - $items = \array_merge([], ...$items); |
|
59 | - |
|
60 | - switch ($strategy) { |
|
61 | - case TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL: |
|
62 | - foreach ($items as $key => $item) { |
|
63 | - if (\array_diff($tagNames, $item->getTags())) { |
|
64 | - unset($items[$key]); |
|
65 | - } |
|
66 | - } |
|
67 | - break; |
|
68 | - |
|
69 | - case TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY: |
|
70 | - foreach ($items as $key => $item) { |
|
71 | - if (\array_diff($tagNames, $item->getTags()) || \array_diff($item->getTags(), $tagNames)) { |
|
72 | - unset($items[$key]); |
|
73 | - } |
|
74 | - } |
|
75 | - break; |
|
76 | - } |
|
77 | - return $items; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @param string $tagName |
|
82 | - * @return array |
|
83 | - * @throws PhpfastcacheInvalidArgumentException |
|
84 | - */ |
|
85 | - protected function fetchItemsByTagFromBackend(string $tagName): array |
|
86 | - { |
|
87 | - if (\is_string($tagName)) { |
|
88 | - $driverResponse = $this->getItem($this->getTagKey($tagName)); |
|
89 | - if ($driverResponse->isHit()) { |
|
90 | - $tagsItems = (array)$driverResponse->get(); |
|
91 | - |
|
92 | - /** |
|
93 | - * getItems() may provides expired item(s) |
|
94 | - * themselves provided by a cache of item |
|
95 | - * keys based stored the tag item. |
|
96 | - * Therefore we pass a filter callback |
|
97 | - * to remove the expired Item(s) provided by |
|
98 | - * the item keys passed through getItems() |
|
99 | - * |
|
100 | - * #headache |
|
101 | - */ |
|
102 | - return \array_filter( |
|
103 | - $this->getItems(\array_unique(\array_keys($tagsItems))), |
|
104 | - static function (ExtendedCacheItemInterface $item) { |
|
105 | - return $item->isHit(); |
|
106 | - } |
|
107 | - ); |
|
108 | - } |
|
109 | - return []; |
|
110 | - } |
|
111 | - |
|
112 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @param string $key |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - protected function getTagKey(string $key): string |
|
120 | - { |
|
121 | - return self::DRIVER_TAGS_KEY_PREFIX . $key; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @inheritdoc |
|
126 | - */ |
|
127 | - public function deleteItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
128 | - { |
|
129 | - $return = null; |
|
130 | - |
|
131 | - foreach ($this->getItemsByTags($tagNames, $strategy) as $item) { |
|
132 | - $result = $this->deleteItem($item->getKey()); |
|
133 | - if ($return !== false) { |
|
134 | - $return = $result; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - return (bool)$return; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @inheritdoc |
|
143 | - */ |
|
144 | - public function deleteItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
145 | - { |
|
146 | - if (\is_string($tagName)) { |
|
147 | - $return = null; |
|
148 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
149 | - $result = $this->deleteItem($item->getKey()); |
|
150 | - if ($return !== false) { |
|
151 | - $return = $result; |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - return (bool)$return; |
|
156 | - } |
|
157 | - |
|
158 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @inheritdoc |
|
163 | - */ |
|
164 | - public function getItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
165 | - { |
|
166 | - $items = $this->fetchItemsByTagFromBackend($tagName); |
|
167 | - if ($strategy === TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY) { |
|
168 | - foreach ($items as $key => $item) { |
|
169 | - if (\array_diff($item->getTags(), $tagName)) { |
|
170 | - unset($items[$key]); |
|
171 | - } |
|
172 | - } |
|
173 | - } |
|
174 | - return $items; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @inheritdoc |
|
179 | - */ |
|
180 | - public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
181 | - { |
|
182 | - $return = null; |
|
183 | - foreach ($tagNames as $tagName) { |
|
184 | - $result = $this->incrementItemsByTag($tagName, $step, $strategy); |
|
185 | - if ($return !== false) { |
|
186 | - $return = $result; |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - return (bool)$return; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @inheritdoc |
|
195 | - */ |
|
196 | - public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
197 | - { |
|
198 | - if (\is_string($tagName) && \is_int($step)) { |
|
199 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
200 | - $item->increment($step); |
|
201 | - $this->saveDeferred($item); |
|
202 | - } |
|
203 | - |
|
204 | - return (bool)$this->commit(); |
|
205 | - } |
|
206 | - |
|
207 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * @inheritdoc |
|
212 | - */ |
|
213 | - public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
214 | - { |
|
215 | - $return = null; |
|
216 | - foreach ($tagNames as $tagName) { |
|
217 | - $result = $this->decrementItemsByTag($tagName, $step, $strategy); |
|
218 | - if ($return !== false) { |
|
219 | - $return = $result; |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - return (bool)$return; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @inheritdoc |
|
228 | - */ |
|
229 | - public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
230 | - { |
|
231 | - if (\is_string($tagName) && \is_int($step)) { |
|
232 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
233 | - $item->decrement($step); |
|
234 | - $this->saveDeferred($item); |
|
235 | - } |
|
236 | - |
|
237 | - return (bool)$this->commit(); |
|
238 | - } |
|
239 | - |
|
240 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @inheritdoc |
|
245 | - */ |
|
246 | - public function appendItemsByTags(array $tagNames, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
247 | - { |
|
248 | - $return = null; |
|
249 | - foreach ($tagNames as $tagName) { |
|
250 | - $result = $this->appendItemsByTag($tagName, $data, $strategy); |
|
251 | - if ($return !== false) { |
|
252 | - $return = $result; |
|
253 | - } |
|
254 | - } |
|
255 | - |
|
256 | - return (bool)$return; |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * @inheritdoc |
|
261 | - */ |
|
262 | - public function appendItemsByTag(string $tagName, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
263 | - { |
|
264 | - if (\is_string($tagName)) { |
|
265 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
266 | - $item->append($data); |
|
267 | - $this->saveDeferred($item); |
|
268 | - } |
|
269 | - |
|
270 | - return (bool)$this->commit(); |
|
271 | - } |
|
272 | - |
|
273 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * @inheritdoc |
|
278 | - */ |
|
279 | - public function prependItemsByTags(array $tagNames, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
280 | - { |
|
281 | - $return = null; |
|
282 | - foreach ($tagNames as $tagName) { |
|
283 | - $result = $this->prependItemsByTag($tagName, $data, $strategy); |
|
284 | - if ($return !== false) { |
|
285 | - $return = $result; |
|
286 | - } |
|
287 | - } |
|
288 | - |
|
289 | - return (bool)$return; |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * @inheritdoc |
|
294 | - */ |
|
295 | - public function prependItemsByTag(string $tagName, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
296 | - { |
|
297 | - if (\is_string($tagName)) { |
|
298 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
299 | - $item->prepend($data); |
|
300 | - $this->saveDeferred($item); |
|
301 | - } |
|
302 | - |
|
303 | - return (bool)$this->commit(); |
|
304 | - } |
|
305 | - |
|
306 | - throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * @param array $wrapper |
|
311 | - * @return mixed |
|
312 | - */ |
|
313 | - protected function driverUnwrapTags(array $wrapper) |
|
314 | - { |
|
315 | - return $wrapper[self::DRIVER_TAGS_WRAPPER_INDEX]; |
|
316 | - } |
|
317 | - |
|
318 | - /** |
|
319 | - * @param ExtendedCacheItemInterface $item |
|
320 | - * @throws PhpfastcacheInvalidArgumentException |
|
321 | - * @throws PhpfastcacheLogicException |
|
322 | - */ |
|
323 | - protected function cleanItemTags(ExtendedCacheItemInterface $item) |
|
324 | - { |
|
325 | - $this->driverWriteTags($item->removeTags($item->getTags())); |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * @param ExtendedCacheItemInterface $item |
|
330 | - * @return bool |
|
331 | - * @throws PhpfastcacheInvalidArgumentException |
|
332 | - * @throws PhpfastcacheLogicException |
|
333 | - */ |
|
334 | - protected function driverWriteTags(ExtendedCacheItemInterface $item): bool |
|
335 | - { |
|
336 | - /** |
|
337 | - * Do not attempt to write tags |
|
338 | - * on tags item, it can leads |
|
339 | - * to an infinite recursive calls |
|
340 | - */ |
|
341 | - if (\strpos($item->getKey(), self::DRIVER_TAGS_KEY_PREFIX) === 0) { |
|
342 | - throw new PhpfastcacheLogicException('Trying to set tag(s) to an Tag item index: ' . $item->getKey()); |
|
343 | - } |
|
344 | - |
|
345 | - if (!$item->getTags() && !$item->getRemovedTags()) { |
|
346 | - return true; |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * @var $tagsItems ExtendedCacheItemInterface[] |
|
351 | - */ |
|
352 | - $tagsItems = $this->getItems($this->getTagKeys($item->getTags())); |
|
353 | - |
|
354 | - foreach ($tagsItems as $tagsItem) { |
|
355 | - $data = $tagsItem->get(); |
|
356 | - $expTimestamp = $item->getExpirationDate()->getTimestamp(); |
|
357 | - |
|
358 | - /** |
|
359 | - * Using the key will |
|
360 | - * avoid to use array_unique |
|
361 | - * that has slow performances |
|
362 | - */ |
|
363 | - |
|
364 | - $tagsItem->set(\array_merge((array)$data, [$item->getKey() => $expTimestamp])) |
|
365 | - ->expiresAt($item->getExpirationDate()); |
|
366 | - |
|
367 | - $this->driverWrite($tagsItem); |
|
368 | - $tagsItem->setHit(true); |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Also update removed tags to |
|
373 | - * keep the index up to date |
|
374 | - */ |
|
375 | - $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
|
376 | - |
|
377 | - foreach ($tagsItems as $tagsItem) { |
|
378 | - $data = (array)$tagsItem->get(); |
|
379 | - |
|
380 | - unset($data[$item->getKey()]); |
|
381 | - $tagsItem->set($data); |
|
382 | - |
|
383 | - /** |
|
384 | - * Recalculate the expiration date |
|
385 | - * |
|
386 | - * If the $tagsItem does not have |
|
387 | - * any cache item references left |
|
388 | - * then remove it from tagsItems index |
|
389 | - */ |
|
390 | - if (\count($data)) { |
|
391 | - $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
392 | - $this->driverWrite($tagsItem); |
|
393 | - $tagsItem->setHit(true); |
|
394 | - } else { |
|
395 | - $this->deleteItem($tagsItem->getKey()); |
|
396 | - } |
|
397 | - } |
|
398 | - |
|
399 | - return true; |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * @param array $keys |
|
404 | - * @return array |
|
405 | - */ |
|
406 | - protected function getTagKeys(array $keys): array |
|
407 | - { |
|
408 | - return \array_map( |
|
409 | - function (string $key) { |
|
410 | - return $this->getTagKey($key); |
|
411 | - }, |
|
412 | - $keys |
|
413 | - ); |
|
414 | - } |
|
32 | + /** |
|
33 | + * @inheritdoc |
|
34 | + */ |
|
35 | + public function getItemsByTagsAsJsonString(array $tagNames, int $option = 0, int $depth = 512, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): string |
|
36 | + { |
|
37 | + $callback = static function (CacheItemInterface $item) { |
|
38 | + return $item->get(); |
|
39 | + }; |
|
40 | + |
|
41 | + return \json_encode(\array_map($callback, \array_values($this->getItemsByTags($tagNames, $strategy))), $option, $depth); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @inheritdoc |
|
46 | + */ |
|
47 | + public function getItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
48 | + { |
|
49 | + $items = []; |
|
50 | + foreach (\array_unique($tagNames) as $tagName) { |
|
51 | + if (\is_string($tagName)) { |
|
52 | + $items[] = $this->fetchItemsByTagFromBackend($tagName); |
|
53 | + } else { |
|
54 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a a string'); |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + $items = \array_merge([], ...$items); |
|
59 | + |
|
60 | + switch ($strategy) { |
|
61 | + case TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL: |
|
62 | + foreach ($items as $key => $item) { |
|
63 | + if (\array_diff($tagNames, $item->getTags())) { |
|
64 | + unset($items[$key]); |
|
65 | + } |
|
66 | + } |
|
67 | + break; |
|
68 | + |
|
69 | + case TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY: |
|
70 | + foreach ($items as $key => $item) { |
|
71 | + if (\array_diff($tagNames, $item->getTags()) || \array_diff($item->getTags(), $tagNames)) { |
|
72 | + unset($items[$key]); |
|
73 | + } |
|
74 | + } |
|
75 | + break; |
|
76 | + } |
|
77 | + return $items; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @param string $tagName |
|
82 | + * @return array |
|
83 | + * @throws PhpfastcacheInvalidArgumentException |
|
84 | + */ |
|
85 | + protected function fetchItemsByTagFromBackend(string $tagName): array |
|
86 | + { |
|
87 | + if (\is_string($tagName)) { |
|
88 | + $driverResponse = $this->getItem($this->getTagKey($tagName)); |
|
89 | + if ($driverResponse->isHit()) { |
|
90 | + $tagsItems = (array)$driverResponse->get(); |
|
91 | + |
|
92 | + /** |
|
93 | + * getItems() may provides expired item(s) |
|
94 | + * themselves provided by a cache of item |
|
95 | + * keys based stored the tag item. |
|
96 | + * Therefore we pass a filter callback |
|
97 | + * to remove the expired Item(s) provided by |
|
98 | + * the item keys passed through getItems() |
|
99 | + * |
|
100 | + * #headache |
|
101 | + */ |
|
102 | + return \array_filter( |
|
103 | + $this->getItems(\array_unique(\array_keys($tagsItems))), |
|
104 | + static function (ExtendedCacheItemInterface $item) { |
|
105 | + return $item->isHit(); |
|
106 | + } |
|
107 | + ); |
|
108 | + } |
|
109 | + return []; |
|
110 | + } |
|
111 | + |
|
112 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @param string $key |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + protected function getTagKey(string $key): string |
|
120 | + { |
|
121 | + return self::DRIVER_TAGS_KEY_PREFIX . $key; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @inheritdoc |
|
126 | + */ |
|
127 | + public function deleteItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
128 | + { |
|
129 | + $return = null; |
|
130 | + |
|
131 | + foreach ($this->getItemsByTags($tagNames, $strategy) as $item) { |
|
132 | + $result = $this->deleteItem($item->getKey()); |
|
133 | + if ($return !== false) { |
|
134 | + $return = $result; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + return (bool)$return; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @inheritdoc |
|
143 | + */ |
|
144 | + public function deleteItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
145 | + { |
|
146 | + if (\is_string($tagName)) { |
|
147 | + $return = null; |
|
148 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
149 | + $result = $this->deleteItem($item->getKey()); |
|
150 | + if ($return !== false) { |
|
151 | + $return = $result; |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + return (bool)$return; |
|
156 | + } |
|
157 | + |
|
158 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @inheritdoc |
|
163 | + */ |
|
164 | + public function getItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
165 | + { |
|
166 | + $items = $this->fetchItemsByTagFromBackend($tagName); |
|
167 | + if ($strategy === TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY) { |
|
168 | + foreach ($items as $key => $item) { |
|
169 | + if (\array_diff($item->getTags(), $tagName)) { |
|
170 | + unset($items[$key]); |
|
171 | + } |
|
172 | + } |
|
173 | + } |
|
174 | + return $items; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @inheritdoc |
|
179 | + */ |
|
180 | + public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
181 | + { |
|
182 | + $return = null; |
|
183 | + foreach ($tagNames as $tagName) { |
|
184 | + $result = $this->incrementItemsByTag($tagName, $step, $strategy); |
|
185 | + if ($return !== false) { |
|
186 | + $return = $result; |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + return (bool)$return; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @inheritdoc |
|
195 | + */ |
|
196 | + public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
197 | + { |
|
198 | + if (\is_string($tagName) && \is_int($step)) { |
|
199 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
200 | + $item->increment($step); |
|
201 | + $this->saveDeferred($item); |
|
202 | + } |
|
203 | + |
|
204 | + return (bool)$this->commit(); |
|
205 | + } |
|
206 | + |
|
207 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * @inheritdoc |
|
212 | + */ |
|
213 | + public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
214 | + { |
|
215 | + $return = null; |
|
216 | + foreach ($tagNames as $tagName) { |
|
217 | + $result = $this->decrementItemsByTag($tagName, $step, $strategy); |
|
218 | + if ($return !== false) { |
|
219 | + $return = $result; |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + return (bool)$return; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @inheritdoc |
|
228 | + */ |
|
229 | + public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
230 | + { |
|
231 | + if (\is_string($tagName) && \is_int($step)) { |
|
232 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
233 | + $item->decrement($step); |
|
234 | + $this->saveDeferred($item); |
|
235 | + } |
|
236 | + |
|
237 | + return (bool)$this->commit(); |
|
238 | + } |
|
239 | + |
|
240 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string and $step an integer'); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @inheritdoc |
|
245 | + */ |
|
246 | + public function appendItemsByTags(array $tagNames, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
247 | + { |
|
248 | + $return = null; |
|
249 | + foreach ($tagNames as $tagName) { |
|
250 | + $result = $this->appendItemsByTag($tagName, $data, $strategy); |
|
251 | + if ($return !== false) { |
|
252 | + $return = $result; |
|
253 | + } |
|
254 | + } |
|
255 | + |
|
256 | + return (bool)$return; |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * @inheritdoc |
|
261 | + */ |
|
262 | + public function appendItemsByTag(string $tagName, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
263 | + { |
|
264 | + if (\is_string($tagName)) { |
|
265 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
266 | + $item->append($data); |
|
267 | + $this->saveDeferred($item); |
|
268 | + } |
|
269 | + |
|
270 | + return (bool)$this->commit(); |
|
271 | + } |
|
272 | + |
|
273 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * @inheritdoc |
|
278 | + */ |
|
279 | + public function prependItemsByTags(array $tagNames, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
280 | + { |
|
281 | + $return = null; |
|
282 | + foreach ($tagNames as $tagName) { |
|
283 | + $result = $this->prependItemsByTag($tagName, $data, $strategy); |
|
284 | + if ($return !== false) { |
|
285 | + $return = $result; |
|
286 | + } |
|
287 | + } |
|
288 | + |
|
289 | + return (bool)$return; |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * @inheritdoc |
|
294 | + */ |
|
295 | + public function prependItemsByTag(string $tagName, $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
296 | + { |
|
297 | + if (\is_string($tagName)) { |
|
298 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
299 | + $item->prepend($data); |
|
300 | + $this->saveDeferred($item); |
|
301 | + } |
|
302 | + |
|
303 | + return (bool)$this->commit(); |
|
304 | + } |
|
305 | + |
|
306 | + throw new PhpfastcacheInvalidArgumentException('$tagName must be a string'); |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * @param array $wrapper |
|
311 | + * @return mixed |
|
312 | + */ |
|
313 | + protected function driverUnwrapTags(array $wrapper) |
|
314 | + { |
|
315 | + return $wrapper[self::DRIVER_TAGS_WRAPPER_INDEX]; |
|
316 | + } |
|
317 | + |
|
318 | + /** |
|
319 | + * @param ExtendedCacheItemInterface $item |
|
320 | + * @throws PhpfastcacheInvalidArgumentException |
|
321 | + * @throws PhpfastcacheLogicException |
|
322 | + */ |
|
323 | + protected function cleanItemTags(ExtendedCacheItemInterface $item) |
|
324 | + { |
|
325 | + $this->driverWriteTags($item->removeTags($item->getTags())); |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * @param ExtendedCacheItemInterface $item |
|
330 | + * @return bool |
|
331 | + * @throws PhpfastcacheInvalidArgumentException |
|
332 | + * @throws PhpfastcacheLogicException |
|
333 | + */ |
|
334 | + protected function driverWriteTags(ExtendedCacheItemInterface $item): bool |
|
335 | + { |
|
336 | + /** |
|
337 | + * Do not attempt to write tags |
|
338 | + * on tags item, it can leads |
|
339 | + * to an infinite recursive calls |
|
340 | + */ |
|
341 | + if (\strpos($item->getKey(), self::DRIVER_TAGS_KEY_PREFIX) === 0) { |
|
342 | + throw new PhpfastcacheLogicException('Trying to set tag(s) to an Tag item index: ' . $item->getKey()); |
|
343 | + } |
|
344 | + |
|
345 | + if (!$item->getTags() && !$item->getRemovedTags()) { |
|
346 | + return true; |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * @var $tagsItems ExtendedCacheItemInterface[] |
|
351 | + */ |
|
352 | + $tagsItems = $this->getItems($this->getTagKeys($item->getTags())); |
|
353 | + |
|
354 | + foreach ($tagsItems as $tagsItem) { |
|
355 | + $data = $tagsItem->get(); |
|
356 | + $expTimestamp = $item->getExpirationDate()->getTimestamp(); |
|
357 | + |
|
358 | + /** |
|
359 | + * Using the key will |
|
360 | + * avoid to use array_unique |
|
361 | + * that has slow performances |
|
362 | + */ |
|
363 | + |
|
364 | + $tagsItem->set(\array_merge((array)$data, [$item->getKey() => $expTimestamp])) |
|
365 | + ->expiresAt($item->getExpirationDate()); |
|
366 | + |
|
367 | + $this->driverWrite($tagsItem); |
|
368 | + $tagsItem->setHit(true); |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Also update removed tags to |
|
373 | + * keep the index up to date |
|
374 | + */ |
|
375 | + $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
|
376 | + |
|
377 | + foreach ($tagsItems as $tagsItem) { |
|
378 | + $data = (array)$tagsItem->get(); |
|
379 | + |
|
380 | + unset($data[$item->getKey()]); |
|
381 | + $tagsItem->set($data); |
|
382 | + |
|
383 | + /** |
|
384 | + * Recalculate the expiration date |
|
385 | + * |
|
386 | + * If the $tagsItem does not have |
|
387 | + * any cache item references left |
|
388 | + * then remove it from tagsItems index |
|
389 | + */ |
|
390 | + if (\count($data)) { |
|
391 | + $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
392 | + $this->driverWrite($tagsItem); |
|
393 | + $tagsItem->setHit(true); |
|
394 | + } else { |
|
395 | + $this->deleteItem($tagsItem->getKey()); |
|
396 | + } |
|
397 | + } |
|
398 | + |
|
399 | + return true; |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * @param array $keys |
|
404 | + * @return array |
|
405 | + */ |
|
406 | + protected function getTagKeys(array $keys): array |
|
407 | + { |
|
408 | + return \array_map( |
|
409 | + function (string $key) { |
|
410 | + return $this->getTagKey($key); |
|
411 | + }, |
|
412 | + $keys |
|
413 | + ); |
|
414 | + } |
|
415 | 415 | } |
@@ -26,256 +26,256 @@ |
||
26 | 26 | */ |
27 | 27 | interface TaggableCacheItemPoolInterface |
28 | 28 | { |
29 | - public const DRIVER_TAGS_KEY_PREFIX = '_TAG_'; |
|
29 | + public const DRIVER_TAGS_KEY_PREFIX = '_TAG_'; |
|
30 | 30 | |
31 | - public const DRIVER_TAGS_WRAPPER_INDEX = 'g'; |
|
31 | + public const DRIVER_TAGS_WRAPPER_INDEX = 'g'; |
|
32 | 32 | |
33 | - public const TAG_STRATEGY_ONE = 1; |
|
33 | + public const TAG_STRATEGY_ONE = 1; |
|
34 | 34 | |
35 | - public const TAG_STRATEGY_ALL = 2; |
|
35 | + public const TAG_STRATEGY_ALL = 2; |
|
36 | 36 | |
37 | - public const TAG_STRATEGY_ONLY = 4; |
|
37 | + public const TAG_STRATEGY_ONLY = 4; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Returns a traversable set of cache items by a tag name. |
|
41 | - * |
|
42 | - * @param string $tagName |
|
43 | - * An indexed array of keys of items to retrieve. |
|
44 | - * |
|
45 | - * @param int $strategy |
|
46 | - * |
|
47 | - * @return ExtendedCacheItemInterface[] |
|
48 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
49 | - * each item. A Cache item will be returned for each key, even if that |
|
50 | - * key is not found. However, if no keys are specified then an empty |
|
51 | - * traversable MUST be returned instead. |
|
52 | - * @throws InvalidArgumentException |
|
53 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
54 | - * MUST be thrown. |
|
55 | - * |
|
56 | - */ |
|
57 | - public function getItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
39 | + /** |
|
40 | + * Returns a traversable set of cache items by a tag name. |
|
41 | + * |
|
42 | + * @param string $tagName |
|
43 | + * An indexed array of keys of items to retrieve. |
|
44 | + * |
|
45 | + * @param int $strategy |
|
46 | + * |
|
47 | + * @return ExtendedCacheItemInterface[] |
|
48 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
49 | + * each item. A Cache item will be returned for each key, even if that |
|
50 | + * key is not found. However, if no keys are specified then an empty |
|
51 | + * traversable MUST be returned instead. |
|
52 | + * @throws InvalidArgumentException |
|
53 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
54 | + * MUST be thrown. |
|
55 | + * |
|
56 | + */ |
|
57 | + public function getItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
58 | 58 | |
59 | - /** |
|
60 | - * Returns a traversable set of cache items by one of multiple tag names. |
|
61 | - * |
|
62 | - * @param string[] $tagNames |
|
63 | - * An indexed array of keys of items to retrieve. |
|
64 | - * |
|
65 | - * @param int $strategy |
|
66 | - * |
|
67 | - * @return ExtendedCacheItemInterface[] |
|
68 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
69 | - * each item. A Cache item will be returned for each key, even if that |
|
70 | - * key is not found. However, if no keys are specified then an empty |
|
71 | - * traversable MUST be returned instead. |
|
72 | - * @throws InvalidArgumentException |
|
73 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
74 | - * MUST be thrown. |
|
75 | - * |
|
76 | - */ |
|
77 | - public function getItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
59 | + /** |
|
60 | + * Returns a traversable set of cache items by one of multiple tag names. |
|
61 | + * |
|
62 | + * @param string[] $tagNames |
|
63 | + * An indexed array of keys of items to retrieve. |
|
64 | + * |
|
65 | + * @param int $strategy |
|
66 | + * |
|
67 | + * @return ExtendedCacheItemInterface[] |
|
68 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
69 | + * each item. A Cache item will be returned for each key, even if that |
|
70 | + * key is not found. However, if no keys are specified then an empty |
|
71 | + * traversable MUST be returned instead. |
|
72 | + * @throws InvalidArgumentException |
|
73 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
74 | + * MUST be thrown. |
|
75 | + * |
|
76 | + */ |
|
77 | + public function getItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
78 | 78 | |
79 | - /** |
|
80 | - * Returns A json string that represents an array of items by tags-based. |
|
81 | - * |
|
82 | - * @param string[] $tagNames |
|
83 | - * An indexed array of keys of items to retrieve. |
|
84 | - * @param int $option \json_encode() options |
|
85 | - * @param int $depth \json_encode() depth |
|
86 | - * @param int $strategy |
|
87 | - * |
|
88 | - * @return string |
|
89 | - * @throws InvalidArgumentException |
|
90 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
91 | - * MUST be thrown. |
|
92 | - * |
|
93 | - */ |
|
94 | - public function getItemsByTagsAsJsonString(array $tagNames, int $option = 0, int $depth = 512, int $strategy = self::TAG_STRATEGY_ONE): string; |
|
79 | + /** |
|
80 | + * Returns A json string that represents an array of items by tags-based. |
|
81 | + * |
|
82 | + * @param string[] $tagNames |
|
83 | + * An indexed array of keys of items to retrieve. |
|
84 | + * @param int $option \json_encode() options |
|
85 | + * @param int $depth \json_encode() depth |
|
86 | + * @param int $strategy |
|
87 | + * |
|
88 | + * @return string |
|
89 | + * @throws InvalidArgumentException |
|
90 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
91 | + * MUST be thrown. |
|
92 | + * |
|
93 | + */ |
|
94 | + public function getItemsByTagsAsJsonString(array $tagNames, int $option = 0, int $depth = 512, int $strategy = self::TAG_STRATEGY_ONE): string; |
|
95 | 95 | |
96 | - /** |
|
97 | - * Removes the item from the pool by tag. |
|
98 | - * |
|
99 | - * @param string $tagName |
|
100 | - * The tag for which to delete |
|
101 | - * |
|
102 | - * @param int $strategy |
|
103 | - * |
|
104 | - * @return bool |
|
105 | - * True if the item was successfully removed. False if there was an error. |
|
106 | - * @throws InvalidArgumentException |
|
107 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
108 | - * MUST be thrown. |
|
109 | - * |
|
110 | - */ |
|
111 | - public function deleteItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
96 | + /** |
|
97 | + * Removes the item from the pool by tag. |
|
98 | + * |
|
99 | + * @param string $tagName |
|
100 | + * The tag for which to delete |
|
101 | + * |
|
102 | + * @param int $strategy |
|
103 | + * |
|
104 | + * @return bool |
|
105 | + * True if the item was successfully removed. False if there was an error. |
|
106 | + * @throws InvalidArgumentException |
|
107 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
108 | + * MUST be thrown. |
|
109 | + * |
|
110 | + */ |
|
111 | + public function deleteItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
112 | 112 | |
113 | - /** |
|
114 | - * Removes the item from the pool by one of multiple tag names. |
|
115 | - * |
|
116 | - * @param string[] $tagNames |
|
117 | - * The tag for which to delete |
|
118 | - * |
|
119 | - * @param int $strategy |
|
120 | - * |
|
121 | - * @return bool |
|
122 | - * True if the items were successfully removed. False if there was an error. |
|
123 | - * @throws InvalidArgumentException |
|
124 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
125 | - * MUST be thrown. |
|
126 | - * |
|
127 | - */ |
|
128 | - public function deleteItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
113 | + /** |
|
114 | + * Removes the item from the pool by one of multiple tag names. |
|
115 | + * |
|
116 | + * @param string[] $tagNames |
|
117 | + * The tag for which to delete |
|
118 | + * |
|
119 | + * @param int $strategy |
|
120 | + * |
|
121 | + * @return bool |
|
122 | + * True if the items were successfully removed. False if there was an error. |
|
123 | + * @throws InvalidArgumentException |
|
124 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
125 | + * MUST be thrown. |
|
126 | + * |
|
127 | + */ |
|
128 | + public function deleteItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
129 | 129 | |
130 | - /** |
|
131 | - * Increment the items from the pool by tag. |
|
132 | - * |
|
133 | - * @param string $tagName |
|
134 | - * The tag for which to increment |
|
135 | - * |
|
136 | - * @param int $step |
|
137 | - * |
|
138 | - * @param int $strategy |
|
139 | - * |
|
140 | - * @return bool |
|
141 | - * True if the item was successfully incremented. False if there was an error. |
|
142 | - * @throws InvalidArgumentException |
|
143 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
144 | - * MUST be thrown. |
|
145 | - * |
|
146 | - */ |
|
147 | - public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
130 | + /** |
|
131 | + * Increment the items from the pool by tag. |
|
132 | + * |
|
133 | + * @param string $tagName |
|
134 | + * The tag for which to increment |
|
135 | + * |
|
136 | + * @param int $step |
|
137 | + * |
|
138 | + * @param int $strategy |
|
139 | + * |
|
140 | + * @return bool |
|
141 | + * True if the item was successfully incremented. False if there was an error. |
|
142 | + * @throws InvalidArgumentException |
|
143 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
144 | + * MUST be thrown. |
|
145 | + * |
|
146 | + */ |
|
147 | + public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
148 | 148 | |
149 | - /** |
|
150 | - * Increment the items from the pool by one of multiple tag names. |
|
151 | - * |
|
152 | - * @param string[] $tagNames |
|
153 | - * The tag for which to increment |
|
154 | - * |
|
155 | - * @param int $step |
|
156 | - * |
|
157 | - * @param int $strategy |
|
158 | - * |
|
159 | - * @return bool |
|
160 | - * True if the items were successfully incremented. False if there was an error. |
|
161 | - * @throws InvalidArgumentException |
|
162 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
163 | - * MUST be thrown. |
|
164 | - * |
|
165 | - */ |
|
166 | - public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
149 | + /** |
|
150 | + * Increment the items from the pool by one of multiple tag names. |
|
151 | + * |
|
152 | + * @param string[] $tagNames |
|
153 | + * The tag for which to increment |
|
154 | + * |
|
155 | + * @param int $step |
|
156 | + * |
|
157 | + * @param int $strategy |
|
158 | + * |
|
159 | + * @return bool |
|
160 | + * True if the items were successfully incremented. False if there was an error. |
|
161 | + * @throws InvalidArgumentException |
|
162 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
163 | + * MUST be thrown. |
|
164 | + * |
|
165 | + */ |
|
166 | + public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
167 | 167 | |
168 | - /** |
|
169 | - * Decrement the items from the pool by tag. |
|
170 | - * |
|
171 | - * @param string $tagName |
|
172 | - * The tag for which to decrement |
|
173 | - * |
|
174 | - * @param int $step |
|
175 | - * |
|
176 | - * @param int $strategy |
|
177 | - * |
|
178 | - * @return bool |
|
179 | - * True if the item was successfully decremented. False if there was an error. |
|
180 | - * @throws InvalidArgumentException |
|
181 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
182 | - * MUST be thrown. |
|
183 | - * |
|
184 | - */ |
|
185 | - public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
168 | + /** |
|
169 | + * Decrement the items from the pool by tag. |
|
170 | + * |
|
171 | + * @param string $tagName |
|
172 | + * The tag for which to decrement |
|
173 | + * |
|
174 | + * @param int $step |
|
175 | + * |
|
176 | + * @param int $strategy |
|
177 | + * |
|
178 | + * @return bool |
|
179 | + * True if the item was successfully decremented. False if there was an error. |
|
180 | + * @throws InvalidArgumentException |
|
181 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
182 | + * MUST be thrown. |
|
183 | + * |
|
184 | + */ |
|
185 | + public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
186 | 186 | |
187 | - /** |
|
188 | - * Decrement the items from the pool by one of multiple tag names. |
|
189 | - * |
|
190 | - * @param string[] $tagNames |
|
191 | - * The tag for which to decrement |
|
192 | - * |
|
193 | - * @param int $step |
|
194 | - * |
|
195 | - * @param int $strategy |
|
196 | - * |
|
197 | - * @return bool |
|
198 | - * True if the item was successfully decremented. False if there was an error. |
|
199 | - * @throws InvalidArgumentException |
|
200 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
201 | - * MUST be thrown. |
|
202 | - * |
|
203 | - */ |
|
204 | - public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
187 | + /** |
|
188 | + * Decrement the items from the pool by one of multiple tag names. |
|
189 | + * |
|
190 | + * @param string[] $tagNames |
|
191 | + * The tag for which to decrement |
|
192 | + * |
|
193 | + * @param int $step |
|
194 | + * |
|
195 | + * @param int $strategy |
|
196 | + * |
|
197 | + * @return bool |
|
198 | + * True if the item was successfully decremented. False if there was an error. |
|
199 | + * @throws InvalidArgumentException |
|
200 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
201 | + * MUST be thrown. |
|
202 | + * |
|
203 | + */ |
|
204 | + public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
205 | 205 | |
206 | - /** |
|
207 | - * Decrement the items from the pool by tag. |
|
208 | - * |
|
209 | - * @param string $tagName |
|
210 | - * The tag for which to append |
|
211 | - * |
|
212 | - * @param array|string $data |
|
213 | - * |
|
214 | - * @param int $strategy |
|
215 | - * |
|
216 | - * @return bool |
|
217 | - * True if the item was successfully appended. False if there was an error. |
|
218 | - * @throws InvalidArgumentException |
|
219 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
220 | - * MUST be thrown. |
|
221 | - * |
|
222 | - */ |
|
223 | - public function appendItemsByTag(string $tagName, $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
206 | + /** |
|
207 | + * Decrement the items from the pool by tag. |
|
208 | + * |
|
209 | + * @param string $tagName |
|
210 | + * The tag for which to append |
|
211 | + * |
|
212 | + * @param array|string $data |
|
213 | + * |
|
214 | + * @param int $strategy |
|
215 | + * |
|
216 | + * @return bool |
|
217 | + * True if the item was successfully appended. False if there was an error. |
|
218 | + * @throws InvalidArgumentException |
|
219 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
220 | + * MUST be thrown. |
|
221 | + * |
|
222 | + */ |
|
223 | + public function appendItemsByTag(string $tagName, $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
224 | 224 | |
225 | - /** |
|
226 | - * Append the items from the pool by one of multiple tag names. |
|
227 | - * |
|
228 | - * @param string[] $tagNames |
|
229 | - * The tag for which to append |
|
230 | - * |
|
231 | - * @param array|string $data |
|
232 | - * |
|
233 | - * @param int $strategy |
|
234 | - * |
|
235 | - * @return bool |
|
236 | - * True if the items were successfully appended. False if there was an error. |
|
237 | - * @throws InvalidArgumentException |
|
238 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
239 | - * MUST be thrown. |
|
240 | - * |
|
241 | - */ |
|
242 | - public function appendItemsByTags(array $tagNames, $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
225 | + /** |
|
226 | + * Append the items from the pool by one of multiple tag names. |
|
227 | + * |
|
228 | + * @param string[] $tagNames |
|
229 | + * The tag for which to append |
|
230 | + * |
|
231 | + * @param array|string $data |
|
232 | + * |
|
233 | + * @param int $strategy |
|
234 | + * |
|
235 | + * @return bool |
|
236 | + * True if the items were successfully appended. False if there was an error. |
|
237 | + * @throws InvalidArgumentException |
|
238 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
239 | + * MUST be thrown. |
|
240 | + * |
|
241 | + */ |
|
242 | + public function appendItemsByTags(array $tagNames, $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
243 | 243 | |
244 | - /** |
|
245 | - * Prepend the items from the pool by tag. |
|
246 | - * |
|
247 | - * @param string $tagName |
|
248 | - * The tag for which to prepend |
|
249 | - * |
|
250 | - * @param array|string $data |
|
251 | - * |
|
252 | - * @param int $strategy |
|
253 | - * |
|
254 | - * @return bool |
|
255 | - * True if the item was successfully prepended. False if there was an error. |
|
256 | - * @throws InvalidArgumentException |
|
257 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
258 | - * MUST be thrown. |
|
259 | - * |
|
260 | - */ |
|
261 | - public function prependItemsByTag(string $tagName, $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
244 | + /** |
|
245 | + * Prepend the items from the pool by tag. |
|
246 | + * |
|
247 | + * @param string $tagName |
|
248 | + * The tag for which to prepend |
|
249 | + * |
|
250 | + * @param array|string $data |
|
251 | + * |
|
252 | + * @param int $strategy |
|
253 | + * |
|
254 | + * @return bool |
|
255 | + * True if the item was successfully prepended. False if there was an error. |
|
256 | + * @throws InvalidArgumentException |
|
257 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
258 | + * MUST be thrown. |
|
259 | + * |
|
260 | + */ |
|
261 | + public function prependItemsByTag(string $tagName, $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
262 | 262 | |
263 | - /** |
|
264 | - * Prepend the items from the pool by one of multiple tag names. |
|
265 | - * |
|
266 | - * @param string[] $tagNames |
|
267 | - * The tag for which to prepend |
|
268 | - * |
|
269 | - * @param array|string $data |
|
270 | - * |
|
271 | - * @param int $strategy |
|
272 | - * |
|
273 | - * @return bool |
|
274 | - * True if the item was successfully prepended. False if there was an error. |
|
275 | - * @throws InvalidArgumentException |
|
276 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
277 | - * MUST be thrown. |
|
278 | - * |
|
279 | - */ |
|
280 | - public function prependItemsByTags(array $tagNames, $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
263 | + /** |
|
264 | + * Prepend the items from the pool by one of multiple tag names. |
|
265 | + * |
|
266 | + * @param string[] $tagNames |
|
267 | + * The tag for which to prepend |
|
268 | + * |
|
269 | + * @param array|string $data |
|
270 | + * |
|
271 | + * @param int $strategy |
|
272 | + * |
|
273 | + * @return bool |
|
274 | + * True if the item was successfully prepended. False if there was an error. |
|
275 | + * @throws InvalidArgumentException |
|
276 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
277 | + * MUST be thrown. |
|
278 | + * |
|
279 | + */ |
|
280 | + public function prependItemsByTags(array $tagNames, $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
281 | 281 | } |
@@ -37,228 +37,228 @@ discard block |
||
37 | 37 | */ |
38 | 38 | trait IOHelperTrait |
39 | 39 | { |
40 | - /** |
|
41 | - * @var array |
|
42 | - */ |
|
43 | - public $tmp = []; |
|
44 | - |
|
45 | - /** |
|
46 | - * Provide a generic getStats() method |
|
47 | - * for files-based drivers |
|
48 | - * @return DriverStatistic |
|
49 | - * @throws PhpfastcacheIOException |
|
50 | - */ |
|
51 | - public function getStats(): DriverStatistic |
|
52 | - { |
|
53 | - $stat = new DriverStatistic(); |
|
54 | - $path = $this->getFilePath(false); |
|
55 | - |
|
56 | - if (!is_dir($path)) { |
|
57 | - throw new PhpfastcacheIOException("Can't read PATH:" . $path); |
|
58 | - } |
|
59 | - $stat->setRawData( |
|
60 | - [ |
|
61 | - 'tmp' => $this->tmp, |
|
62 | - ] |
|
63 | - ) |
|
64 | - ->setSize(Directory::dirSize($path)) |
|
65 | - ->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path)); |
|
66 | - |
|
67 | - if($this->getConfig()->isUseStaticItemCaching()){ |
|
68 | - $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
|
69 | - }else{ |
|
70 | - $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
|
71 | - } |
|
72 | - |
|
73 | - return $stat; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @param $keyword |
|
78 | - * @param bool $skip |
|
79 | - * @return string |
|
80 | - * @throws PhpfastcacheIOException |
|
81 | - */ |
|
82 | - protected function getFilePath($keyword, $skip = false): string |
|
83 | - { |
|
84 | - $path = $this->getPath(); |
|
85 | - |
|
86 | - if ($keyword === false) { |
|
87 | - return $path; |
|
88 | - } |
|
89 | - |
|
90 | - $filename = $this->encodeFilename($keyword); |
|
91 | - $folder = \substr($filename, 0, 2) . DIRECTORY_SEPARATOR . \substr($filename, 2, 2); |
|
92 | - $path = \rtrim($path, '/\\') . DIRECTORY_SEPARATOR . $folder; |
|
93 | - |
|
94 | - /** |
|
95 | - * Skip Create Sub Folders; |
|
96 | - */ |
|
97 | - if (!$skip && !\is_dir($path) && @!\mkdir($path, $this->getDefaultChmod(), true) && !\is_dir($path)) { |
|
98 | - throw new PhpfastcacheIOException( |
|
99 | - 'Path "' . $path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
100 | - ); |
|
101 | - } |
|
102 | - |
|
103 | - return $path . \DIRECTORY_SEPARATOR . $filename . '.' . $this->getConfig()->getCacheFileExtension(); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @param bool $readonly |
|
108 | - * @return string |
|
109 | - * @throws PhpfastcacheIOException |
|
110 | - */ |
|
111 | - public function getPath($readonly = false): string |
|
112 | - { |
|
113 | - /** |
|
114 | - * Get the base system temporary directory |
|
115 | - */ |
|
116 | - $tmp_dir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
|
117 | - |
|
118 | - /** |
|
119 | - * Calculate the security key |
|
120 | - */ |
|
121 | - { |
|
122 | - $securityKey = $this->getConfig()->getSecurityKey(); |
|
123 | - if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
|
124 | - if (isset($_SERVER['HTTP_HOST'])) { |
|
125 | - $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $_SERVER['HTTP_HOST']))); |
|
126 | - } else { |
|
127 | - $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - if ($securityKey !== '') { |
|
132 | - $securityKey .= '/'; |
|
133 | - } |
|
134 | - |
|
135 | - $securityKey = static::cleanFileName($securityKey); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Extends the temporary directory |
|
140 | - * with the security key and the driver name |
|
141 | - */ |
|
142 | - $tmp_dir = \rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR; |
|
143 | - |
|
144 | - if (empty($this->getConfig()->getPath())) { |
|
145 | - $path = $tmp_dir; |
|
146 | - } else { |
|
147 | - $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
|
148 | - } |
|
149 | - |
|
150 | - $path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
|
151 | - $full_path = Directory::getAbsolutePath($path . $path_suffix); |
|
152 | - $full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix); |
|
153 | - $full_path_hash = $this->getConfig()->getDefaultFileNameHashFunction()($full_path); |
|
154 | - |
|
155 | - /** |
|
156 | - * In readonly mode we only attempt |
|
157 | - * to verify if the directory exists |
|
158 | - * or not, if it does not then we |
|
159 | - * return the temp dir |
|
160 | - */ |
|
161 | - if ($readonly === true) { |
|
162 | - if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
|
163 | - return $full_path_tmp; |
|
164 | - } |
|
165 | - return $full_path; |
|
166 | - } |
|
167 | - |
|
168 | - if (!isset($this->tmp[$full_path_hash]) || (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
|
169 | - if (!@\file_exists($full_path)) { |
|
170 | - if (@mkdir($full_path, $this->getDefaultChmod(), true) === false && !\is_dir($full_path)) { |
|
171 | - throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
|
172 | - } |
|
173 | - } else { |
|
174 | - if (!@\is_writable($full_path)) { |
|
175 | - if (!@\chmod($full_path, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
|
176 | - /** |
|
177 | - * Switch back to tmp dir |
|
178 | - * again if the path is not writable |
|
179 | - */ |
|
180 | - $full_path = $full_path_tmp; |
|
181 | - if (!@\file_exists($full_path)) { |
|
182 | - if (@\mkdir($full_path, $this->getDefaultChmod(), true) && !\is_dir($full_path)) { |
|
183 | - throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
|
184 | - } |
|
185 | - } |
|
186 | - } |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * In case there is no directory |
|
192 | - * writable including the temporary |
|
193 | - * one, we must throw an exception |
|
194 | - */ |
|
195 | - if (!@\file_exists($full_path) || !@\is_writable($full_path)) { |
|
196 | - throw new PhpfastcacheIOException( |
|
197 | - 'Path "' . $full_path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
198 | - ); |
|
199 | - } |
|
200 | - |
|
201 | - $this->tmp[$full_path_hash] = $full_path; |
|
202 | - $this->htaccessGen($full_path, $this->getConfig()->isValidOption('htaccess') ? $this->getConfig()->getHtaccess() : false); |
|
203 | - } |
|
204 | - |
|
205 | - return realpath($full_path); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * @param $filename |
|
210 | - * @return string |
|
211 | - */ |
|
212 | - protected static function cleanFileName($filename): string |
|
213 | - { |
|
214 | - $regex = [ |
|
215 | - '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
|
216 | - '/\.$/', |
|
217 | - '/^\./', |
|
218 | - ]; |
|
219 | - $replace = ['-', '', '']; |
|
220 | - |
|
221 | - return \trim(\preg_replace($regex, $replace, \trim($filename)), '-'); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * @return int |
|
226 | - */ |
|
227 | - protected function getDefaultChmod(): int |
|
228 | - { |
|
229 | - if (!$this->getConfig()->getDefaultChmod()) { |
|
230 | - return 0777; |
|
231 | - } |
|
232 | - |
|
233 | - return $this->getConfig()->getDefaultChmod(); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * @param $path |
|
238 | - * @param bool $create |
|
239 | - * @throws PhpfastcacheIOException |
|
240 | - */ |
|
241 | - protected function htaccessGen($path, $create = true) |
|
242 | - { |
|
243 | - if ($create === true) { |
|
244 | - if (!\is_writable($path)) { |
|
245 | - try { |
|
246 | - if (!\chmod($path, 0777)) { |
|
247 | - throw new PhpfastcacheIOException('Chmod failed on : ' . $path); |
|
248 | - } |
|
249 | - } catch (PhpfastcacheIOException $e) { |
|
250 | - throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!', 0, $e); |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - if (!\file_exists($path . '/.htaccess')) { |
|
255 | - $file = @\fopen($path . '/.htaccess', 'w+b'); |
|
256 | - if (!$file) { |
|
257 | - throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!'); |
|
258 | - } |
|
259 | - \fwrite( |
|
260 | - $file, |
|
261 | - <<<HTACCESS |
|
40 | + /** |
|
41 | + * @var array |
|
42 | + */ |
|
43 | + public $tmp = []; |
|
44 | + |
|
45 | + /** |
|
46 | + * Provide a generic getStats() method |
|
47 | + * for files-based drivers |
|
48 | + * @return DriverStatistic |
|
49 | + * @throws PhpfastcacheIOException |
|
50 | + */ |
|
51 | + public function getStats(): DriverStatistic |
|
52 | + { |
|
53 | + $stat = new DriverStatistic(); |
|
54 | + $path = $this->getFilePath(false); |
|
55 | + |
|
56 | + if (!is_dir($path)) { |
|
57 | + throw new PhpfastcacheIOException("Can't read PATH:" . $path); |
|
58 | + } |
|
59 | + $stat->setRawData( |
|
60 | + [ |
|
61 | + 'tmp' => $this->tmp, |
|
62 | + ] |
|
63 | + ) |
|
64 | + ->setSize(Directory::dirSize($path)) |
|
65 | + ->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path)); |
|
66 | + |
|
67 | + if($this->getConfig()->isUseStaticItemCaching()){ |
|
68 | + $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
|
69 | + }else{ |
|
70 | + $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
|
71 | + } |
|
72 | + |
|
73 | + return $stat; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @param $keyword |
|
78 | + * @param bool $skip |
|
79 | + * @return string |
|
80 | + * @throws PhpfastcacheIOException |
|
81 | + */ |
|
82 | + protected function getFilePath($keyword, $skip = false): string |
|
83 | + { |
|
84 | + $path = $this->getPath(); |
|
85 | + |
|
86 | + if ($keyword === false) { |
|
87 | + return $path; |
|
88 | + } |
|
89 | + |
|
90 | + $filename = $this->encodeFilename($keyword); |
|
91 | + $folder = \substr($filename, 0, 2) . DIRECTORY_SEPARATOR . \substr($filename, 2, 2); |
|
92 | + $path = \rtrim($path, '/\\') . DIRECTORY_SEPARATOR . $folder; |
|
93 | + |
|
94 | + /** |
|
95 | + * Skip Create Sub Folders; |
|
96 | + */ |
|
97 | + if (!$skip && !\is_dir($path) && @!\mkdir($path, $this->getDefaultChmod(), true) && !\is_dir($path)) { |
|
98 | + throw new PhpfastcacheIOException( |
|
99 | + 'Path "' . $path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
100 | + ); |
|
101 | + } |
|
102 | + |
|
103 | + return $path . \DIRECTORY_SEPARATOR . $filename . '.' . $this->getConfig()->getCacheFileExtension(); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @param bool $readonly |
|
108 | + * @return string |
|
109 | + * @throws PhpfastcacheIOException |
|
110 | + */ |
|
111 | + public function getPath($readonly = false): string |
|
112 | + { |
|
113 | + /** |
|
114 | + * Get the base system temporary directory |
|
115 | + */ |
|
116 | + $tmp_dir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
|
117 | + |
|
118 | + /** |
|
119 | + * Calculate the security key |
|
120 | + */ |
|
121 | + { |
|
122 | + $securityKey = $this->getConfig()->getSecurityKey(); |
|
123 | + if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
|
124 | + if (isset($_SERVER['HTTP_HOST'])) { |
|
125 | + $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $_SERVER['HTTP_HOST']))); |
|
126 | + } else { |
|
127 | + $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + if ($securityKey !== '') { |
|
132 | + $securityKey .= '/'; |
|
133 | + } |
|
134 | + |
|
135 | + $securityKey = static::cleanFileName($securityKey); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Extends the temporary directory |
|
140 | + * with the security key and the driver name |
|
141 | + */ |
|
142 | + $tmp_dir = \rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR; |
|
143 | + |
|
144 | + if (empty($this->getConfig()->getPath())) { |
|
145 | + $path = $tmp_dir; |
|
146 | + } else { |
|
147 | + $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
|
148 | + } |
|
149 | + |
|
150 | + $path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
|
151 | + $full_path = Directory::getAbsolutePath($path . $path_suffix); |
|
152 | + $full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix); |
|
153 | + $full_path_hash = $this->getConfig()->getDefaultFileNameHashFunction()($full_path); |
|
154 | + |
|
155 | + /** |
|
156 | + * In readonly mode we only attempt |
|
157 | + * to verify if the directory exists |
|
158 | + * or not, if it does not then we |
|
159 | + * return the temp dir |
|
160 | + */ |
|
161 | + if ($readonly === true) { |
|
162 | + if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
|
163 | + return $full_path_tmp; |
|
164 | + } |
|
165 | + return $full_path; |
|
166 | + } |
|
167 | + |
|
168 | + if (!isset($this->tmp[$full_path_hash]) || (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
|
169 | + if (!@\file_exists($full_path)) { |
|
170 | + if (@mkdir($full_path, $this->getDefaultChmod(), true) === false && !\is_dir($full_path)) { |
|
171 | + throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
|
172 | + } |
|
173 | + } else { |
|
174 | + if (!@\is_writable($full_path)) { |
|
175 | + if (!@\chmod($full_path, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
|
176 | + /** |
|
177 | + * Switch back to tmp dir |
|
178 | + * again if the path is not writable |
|
179 | + */ |
|
180 | + $full_path = $full_path_tmp; |
|
181 | + if (!@\file_exists($full_path)) { |
|
182 | + if (@\mkdir($full_path, $this->getDefaultChmod(), true) && !\is_dir($full_path)) { |
|
183 | + throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
|
184 | + } |
|
185 | + } |
|
186 | + } |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * In case there is no directory |
|
192 | + * writable including the temporary |
|
193 | + * one, we must throw an exception |
|
194 | + */ |
|
195 | + if (!@\file_exists($full_path) || !@\is_writable($full_path)) { |
|
196 | + throw new PhpfastcacheIOException( |
|
197 | + 'Path "' . $full_path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
198 | + ); |
|
199 | + } |
|
200 | + |
|
201 | + $this->tmp[$full_path_hash] = $full_path; |
|
202 | + $this->htaccessGen($full_path, $this->getConfig()->isValidOption('htaccess') ? $this->getConfig()->getHtaccess() : false); |
|
203 | + } |
|
204 | + |
|
205 | + return realpath($full_path); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * @param $filename |
|
210 | + * @return string |
|
211 | + */ |
|
212 | + protected static function cleanFileName($filename): string |
|
213 | + { |
|
214 | + $regex = [ |
|
215 | + '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
|
216 | + '/\.$/', |
|
217 | + '/^\./', |
|
218 | + ]; |
|
219 | + $replace = ['-', '', '']; |
|
220 | + |
|
221 | + return \trim(\preg_replace($regex, $replace, \trim($filename)), '-'); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * @return int |
|
226 | + */ |
|
227 | + protected function getDefaultChmod(): int |
|
228 | + { |
|
229 | + if (!$this->getConfig()->getDefaultChmod()) { |
|
230 | + return 0777; |
|
231 | + } |
|
232 | + |
|
233 | + return $this->getConfig()->getDefaultChmod(); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * @param $path |
|
238 | + * @param bool $create |
|
239 | + * @throws PhpfastcacheIOException |
|
240 | + */ |
|
241 | + protected function htaccessGen($path, $create = true) |
|
242 | + { |
|
243 | + if ($create === true) { |
|
244 | + if (!\is_writable($path)) { |
|
245 | + try { |
|
246 | + if (!\chmod($path, 0777)) { |
|
247 | + throw new PhpfastcacheIOException('Chmod failed on : ' . $path); |
|
248 | + } |
|
249 | + } catch (PhpfastcacheIOException $e) { |
|
250 | + throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!', 0, $e); |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + if (!\file_exists($path . '/.htaccess')) { |
|
255 | + $file = @\fopen($path . '/.htaccess', 'w+b'); |
|
256 | + if (!$file) { |
|
257 | + throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!'); |
|
258 | + } |
|
259 | + \fwrite( |
|
260 | + $file, |
|
261 | + <<<HTACCESS |
|
262 | 262 | ### This .htaccess is auto-generated by PhpFastCache ### |
263 | 263 | <IfModule mod_authz_host> |
264 | 264 | Require all denied |
@@ -268,97 +268,97 @@ discard block |
||
268 | 268 | Deny from all |
269 | 269 | </IfModule> |
270 | 270 | HTACCESS |
271 | - ); |
|
272 | - \fclose($file); |
|
273 | - } |
|
274 | - } |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * @param $keyword |
|
279 | - * @return string |
|
280 | - */ |
|
281 | - protected function encodeFilename($keyword): string |
|
282 | - { |
|
283 | - return $this->getConfig()->getDefaultFileNameHashFunction()($keyword); |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * @param $file |
|
288 | - * @return string |
|
289 | - * @throws PhpfastcacheIOException |
|
290 | - */ |
|
291 | - protected function readFile($file): string |
|
292 | - { |
|
293 | - if (!\is_readable($file)) { |
|
294 | - throw new PhpfastcacheIOException("Cannot read file located at: {$file}"); |
|
295 | - } |
|
296 | - if (\function_exists('file_get_contents')) { |
|
297 | - return (string)\file_get_contents($file); |
|
298 | - } |
|
299 | - |
|
300 | - $string = ''; |
|
301 | - |
|
302 | - $file_handle = @\fopen($file, 'rb'); |
|
303 | - while (!\feof($file_handle)) { |
|
304 | - $line = \fgets($file_handle); |
|
305 | - $string .= $line; |
|
306 | - } |
|
307 | - \fclose($file_handle); |
|
308 | - |
|
309 | - return $string; |
|
310 | - } |
|
311 | - |
|
312 | - /******************** |
|
271 | + ); |
|
272 | + \fclose($file); |
|
273 | + } |
|
274 | + } |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * @param $keyword |
|
279 | + * @return string |
|
280 | + */ |
|
281 | + protected function encodeFilename($keyword): string |
|
282 | + { |
|
283 | + return $this->getConfig()->getDefaultFileNameHashFunction()($keyword); |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * @param $file |
|
288 | + * @return string |
|
289 | + * @throws PhpfastcacheIOException |
|
290 | + */ |
|
291 | + protected function readFile($file): string |
|
292 | + { |
|
293 | + if (!\is_readable($file)) { |
|
294 | + throw new PhpfastcacheIOException("Cannot read file located at: {$file}"); |
|
295 | + } |
|
296 | + if (\function_exists('file_get_contents')) { |
|
297 | + return (string)\file_get_contents($file); |
|
298 | + } |
|
299 | + |
|
300 | + $string = ''; |
|
301 | + |
|
302 | + $file_handle = @\fopen($file, 'rb'); |
|
303 | + while (!\feof($file_handle)) { |
|
304 | + $line = \fgets($file_handle); |
|
305 | + $string .= $line; |
|
306 | + } |
|
307 | + \fclose($file_handle); |
|
308 | + |
|
309 | + return $string; |
|
310 | + } |
|
311 | + |
|
312 | + /******************** |
|
313 | 313 | * |
314 | 314 | * PSR-6 Extended Methods |
315 | 315 | * |
316 | 316 | *******************/ |
317 | 317 | |
318 | - /** |
|
319 | - * @param string $file |
|
320 | - * @param string $data |
|
321 | - * @param bool $secureFileManipulation |
|
322 | - * @return bool |
|
323 | - * @throws PhpfastcacheIOException |
|
324 | - */ |
|
325 | - protected function writefile(string $file, string $data, bool $secureFileManipulation = false): bool |
|
326 | - { |
|
327 | - /** |
|
328 | - * @eventName CacheWriteFileOnDisk |
|
329 | - * @param ExtendedCacheItemPoolInterface $this |
|
330 | - * @param string $file |
|
331 | - * @param bool $secureFileManipulation |
|
332 | - * |
|
333 | - */ |
|
334 | - $this->eventManager->dispatch('CacheWriteFileOnDisk', $this, $file, $secureFileManipulation); |
|
335 | - |
|
336 | - if ($secureFileManipulation) { |
|
337 | - $tmpFilename = Directory::getAbsolutePath( |
|
338 | - dirname($file) . \DIRECTORY_SEPARATOR . 'tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()( |
|
339 | - \bin2hex(\random_bytes(16)) |
|
340 | - ) |
|
341 | - ) . '.' . $this->getConfig()->getCacheFileExtension() . \random_int(1000, 9999); |
|
342 | - |
|
343 | - $handle = \fopen($tmpFilename, 'w+b'); |
|
344 | - if (\is_resource($handle)) { |
|
345 | - \flock($handle, \LOCK_EX); |
|
346 | - $octetWritten = fwrite($handle, $data); |
|
347 | - \flock($handle, \LOCK_UN); |
|
348 | - \fclose($handle); |
|
349 | - } |
|
350 | - |
|
351 | - if (!\rename($tmpFilename, $file)) { |
|
352 | - throw new PhpfastcacheIOException(\sprintf('Failed to rename %s to %s', $tmpFilename, $file)); |
|
353 | - } |
|
354 | - } else { |
|
355 | - $handle = \fopen($file, 'w+b'); |
|
356 | - if (\is_resource($handle)) { |
|
357 | - $octetWritten = \fwrite($handle, $data); |
|
358 | - \fclose($handle); |
|
359 | - } |
|
360 | - } |
|
361 | - |
|
362 | - return (bool)($octetWritten ?? false); |
|
363 | - } |
|
318 | + /** |
|
319 | + * @param string $file |
|
320 | + * @param string $data |
|
321 | + * @param bool $secureFileManipulation |
|
322 | + * @return bool |
|
323 | + * @throws PhpfastcacheIOException |
|
324 | + */ |
|
325 | + protected function writefile(string $file, string $data, bool $secureFileManipulation = false): bool |
|
326 | + { |
|
327 | + /** |
|
328 | + * @eventName CacheWriteFileOnDisk |
|
329 | + * @param ExtendedCacheItemPoolInterface $this |
|
330 | + * @param string $file |
|
331 | + * @param bool $secureFileManipulation |
|
332 | + * |
|
333 | + */ |
|
334 | + $this->eventManager->dispatch('CacheWriteFileOnDisk', $this, $file, $secureFileManipulation); |
|
335 | + |
|
336 | + if ($secureFileManipulation) { |
|
337 | + $tmpFilename = Directory::getAbsolutePath( |
|
338 | + dirname($file) . \DIRECTORY_SEPARATOR . 'tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()( |
|
339 | + \bin2hex(\random_bytes(16)) |
|
340 | + ) |
|
341 | + ) . '.' . $this->getConfig()->getCacheFileExtension() . \random_int(1000, 9999); |
|
342 | + |
|
343 | + $handle = \fopen($tmpFilename, 'w+b'); |
|
344 | + if (\is_resource($handle)) { |
|
345 | + \flock($handle, \LOCK_EX); |
|
346 | + $octetWritten = fwrite($handle, $data); |
|
347 | + \flock($handle, \LOCK_UN); |
|
348 | + \fclose($handle); |
|
349 | + } |
|
350 | + |
|
351 | + if (!\rename($tmpFilename, $file)) { |
|
352 | + throw new PhpfastcacheIOException(\sprintf('Failed to rename %s to %s', $tmpFilename, $file)); |
|
353 | + } |
|
354 | + } else { |
|
355 | + $handle = \fopen($file, 'w+b'); |
|
356 | + if (\is_resource($handle)) { |
|
357 | + $octetWritten = \fwrite($handle, $data); |
|
358 | + \fclose($handle); |
|
359 | + } |
|
360 | + } |
|
361 | + |
|
362 | + return (bool)($octetWritten ?? false); |
|
363 | + } |
|
364 | 364 | } |
@@ -41,171 +41,171 @@ |
||
41 | 41 | */ |
42 | 42 | interface ExtendedCacheItemPoolInterface extends CacheItemPoolInterface, EventManagerDispatcherInterface, ClassNamespaceResolverInterface, TaggableCacheItemPoolInterface |
43 | 43 | { |
44 | - public const DRIVER_CHECK_FAILURE = '%s is not installed or is misconfigured, cannot continue. |
|
44 | + public const DRIVER_CHECK_FAILURE = '%s is not installed or is misconfigured, cannot continue. |
|
45 | 45 | Also, please verify the suggested dependencies in composer because as of the V6, 3rd party libraries are no longer required.'; |
46 | 46 | |
47 | - public const DRIVER_CONNECT_FAILURE = '%s failed to connect with the following error message: "%s" line %d in %s'; |
|
48 | - |
|
49 | - public const DRIVER_KEY_WRAPPER_INDEX = 'k'; |
|
50 | - |
|
51 | - public const DRIVER_DATA_WRAPPER_INDEX = 'd'; |
|
52 | - |
|
53 | - /** |
|
54 | - * Expiration date Index |
|
55 | - */ |
|
56 | - public const DRIVER_EDATE_WRAPPER_INDEX = 'e'; |
|
57 | - |
|
58 | - /** |
|
59 | - * Creation date Index |
|
60 | - */ |
|
61 | - public const DRIVER_CDATE_WRAPPER_INDEX = 'c'; |
|
62 | - |
|
63 | - /** |
|
64 | - * Modification date Index |
|
65 | - */ |
|
66 | - public const DRIVER_MDATE_WRAPPER_INDEX = 'm'; |
|
67 | - |
|
68 | - /** |
|
69 | - * Return the config class name |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - public static function getConfigClass(): string; |
|
73 | - |
|
74 | - /** |
|
75 | - * @return ConfigurationOption |
|
76 | - */ |
|
77 | - public function getConfig(): ConfigurationOption; |
|
78 | - |
|
79 | - /** |
|
80 | - * @return ConfigurationOption |
|
81 | - */ |
|
82 | - public function getDefaultConfig(): ConfigurationOption; |
|
83 | - |
|
84 | - /** |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - public function getDriverName(): string; |
|
88 | - |
|
89 | - /** |
|
90 | - * @return mixed |
|
91 | - */ |
|
92 | - public function getInstanceId(): string; |
|
93 | - |
|
94 | - /** |
|
95 | - * [phpFastCache phpDoc Override] |
|
96 | - * Returns a Cache Item representing the specified key. |
|
97 | - * |
|
98 | - * This method must always return a CacheItemInterface object, even in case of |
|
99 | - * a cache miss. It MUST NOT return null. |
|
100 | - * |
|
101 | - * @param string $key |
|
102 | - * The key for which to return the corresponding Cache Item. |
|
103 | - * |
|
104 | - * @return ExtendedCacheItemInterface |
|
105 | - * The corresponding Cache Item. |
|
106 | - * @throws PhpfastcacheInvalidArgumentException |
|
107 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
108 | - * MUST be thrown. |
|
109 | - * |
|
110 | - */ |
|
111 | - public function getItem($key); |
|
112 | - |
|
113 | - /** |
|
114 | - * [phpFastCache phpDoc Override] |
|
115 | - * Returns a traversable set of cache items. |
|
116 | - * |
|
117 | - * @param array $keys |
|
118 | - * An indexed array of keys of items to retrieve. |
|
119 | - * |
|
120 | - * @return ExtendedCacheItemInterface[] |
|
121 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
122 | - * each item. A Cache item will be returned for each key, even if that |
|
123 | - * key is not found. However, if no keys are specified then an empty |
|
124 | - * traversable MUST be returned instead. |
|
125 | - * @throws InvalidArgumentException |
|
126 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
127 | - * MUST be thrown. |
|
128 | - * |
|
129 | - */ |
|
130 | - public function getItems(array $keys = []); |
|
131 | - |
|
132 | - /** |
|
133 | - * Returns A json string that represents an array of items. |
|
134 | - * |
|
135 | - * @param array $keys |
|
136 | - * An indexed array of keys of items to retrieve. |
|
137 | - * @param int $option \json_encode() options |
|
138 | - * @param int $depth \json_encode() depth |
|
139 | - * |
|
140 | - * @return string |
|
141 | - * @throws InvalidArgumentException |
|
142 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
143 | - * MUST be thrown. |
|
144 | - * |
|
145 | - */ |
|
146 | - public function getItemsAsJsonString(array $keys = [], int $option = 0, int $depth = 512): string; |
|
147 | - |
|
148 | - /** |
|
149 | - * @param CacheItemInterface $item |
|
150 | - * @return mixed |
|
151 | - */ |
|
152 | - public function setItem(CacheItemInterface $item); |
|
153 | - |
|
154 | - /** |
|
155 | - * @return DriverStatistic |
|
156 | - */ |
|
157 | - public function getStats(): DriverStatistic; |
|
158 | - |
|
159 | - /** |
|
160 | - * Get a quick help guide |
|
161 | - * about the current driver |
|
162 | - * |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - public function getHelp(): string; |
|
166 | - |
|
167 | - /** |
|
168 | - * @param CacheItemInterface $item |
|
169 | - * @return void |
|
170 | - */ |
|
171 | - public function detachItem(CacheItemInterface $item); |
|
172 | - |
|
173 | - /** |
|
174 | - * @return void |
|
175 | - */ |
|
176 | - public function detachAllItems(); |
|
177 | - |
|
178 | - /** |
|
179 | - * @param CacheItemInterface $item |
|
180 | - * @return void |
|
181 | - * @throws PhpfastcacheLogicException |
|
182 | - */ |
|
183 | - public function attachItem(CacheItemInterface $item); |
|
184 | - |
|
185 | - /** |
|
186 | - * Returns true if the item exists, is attached and the Spl Hash matches |
|
187 | - * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
188 | - * Returns null if the item does not exists |
|
189 | - * |
|
190 | - * @param CacheItemInterface $item |
|
191 | - * @return bool|null |
|
192 | - * @throws PhpfastcacheLogicException |
|
193 | - */ |
|
194 | - public function isAttached(CacheItemInterface $item); |
|
195 | - |
|
196 | - /** |
|
197 | - * Save multiple items, possible uses: |
|
198 | - * saveMultiple([$item1, $item2, $item3]); |
|
199 | - * saveMultiple($item1, $item2, $item3); |
|
200 | - * |
|
201 | - * @param ExtendedCacheItemInterface[] $items |
|
202 | - * @return bool |
|
203 | - */ |
|
204 | - public function saveMultiple(...$items): bool; |
|
205 | - |
|
206 | - |
|
207 | - /** |
|
208 | - * @return DriverIO |
|
209 | - */ |
|
210 | - public function getIO(): DriverIO; |
|
47 | + public const DRIVER_CONNECT_FAILURE = '%s failed to connect with the following error message: "%s" line %d in %s'; |
|
48 | + |
|
49 | + public const DRIVER_KEY_WRAPPER_INDEX = 'k'; |
|
50 | + |
|
51 | + public const DRIVER_DATA_WRAPPER_INDEX = 'd'; |
|
52 | + |
|
53 | + /** |
|
54 | + * Expiration date Index |
|
55 | + */ |
|
56 | + public const DRIVER_EDATE_WRAPPER_INDEX = 'e'; |
|
57 | + |
|
58 | + /** |
|
59 | + * Creation date Index |
|
60 | + */ |
|
61 | + public const DRIVER_CDATE_WRAPPER_INDEX = 'c'; |
|
62 | + |
|
63 | + /** |
|
64 | + * Modification date Index |
|
65 | + */ |
|
66 | + public const DRIVER_MDATE_WRAPPER_INDEX = 'm'; |
|
67 | + |
|
68 | + /** |
|
69 | + * Return the config class name |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + public static function getConfigClass(): string; |
|
73 | + |
|
74 | + /** |
|
75 | + * @return ConfigurationOption |
|
76 | + */ |
|
77 | + public function getConfig(): ConfigurationOption; |
|
78 | + |
|
79 | + /** |
|
80 | + * @return ConfigurationOption |
|
81 | + */ |
|
82 | + public function getDefaultConfig(): ConfigurationOption; |
|
83 | + |
|
84 | + /** |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + public function getDriverName(): string; |
|
88 | + |
|
89 | + /** |
|
90 | + * @return mixed |
|
91 | + */ |
|
92 | + public function getInstanceId(): string; |
|
93 | + |
|
94 | + /** |
|
95 | + * [phpFastCache phpDoc Override] |
|
96 | + * Returns a Cache Item representing the specified key. |
|
97 | + * |
|
98 | + * This method must always return a CacheItemInterface object, even in case of |
|
99 | + * a cache miss. It MUST NOT return null. |
|
100 | + * |
|
101 | + * @param string $key |
|
102 | + * The key for which to return the corresponding Cache Item. |
|
103 | + * |
|
104 | + * @return ExtendedCacheItemInterface |
|
105 | + * The corresponding Cache Item. |
|
106 | + * @throws PhpfastcacheInvalidArgumentException |
|
107 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
108 | + * MUST be thrown. |
|
109 | + * |
|
110 | + */ |
|
111 | + public function getItem($key); |
|
112 | + |
|
113 | + /** |
|
114 | + * [phpFastCache phpDoc Override] |
|
115 | + * Returns a traversable set of cache items. |
|
116 | + * |
|
117 | + * @param array $keys |
|
118 | + * An indexed array of keys of items to retrieve. |
|
119 | + * |
|
120 | + * @return ExtendedCacheItemInterface[] |
|
121 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
122 | + * each item. A Cache item will be returned for each key, even if that |
|
123 | + * key is not found. However, if no keys are specified then an empty |
|
124 | + * traversable MUST be returned instead. |
|
125 | + * @throws InvalidArgumentException |
|
126 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
127 | + * MUST be thrown. |
|
128 | + * |
|
129 | + */ |
|
130 | + public function getItems(array $keys = []); |
|
131 | + |
|
132 | + /** |
|
133 | + * Returns A json string that represents an array of items. |
|
134 | + * |
|
135 | + * @param array $keys |
|
136 | + * An indexed array of keys of items to retrieve. |
|
137 | + * @param int $option \json_encode() options |
|
138 | + * @param int $depth \json_encode() depth |
|
139 | + * |
|
140 | + * @return string |
|
141 | + * @throws InvalidArgumentException |
|
142 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
143 | + * MUST be thrown. |
|
144 | + * |
|
145 | + */ |
|
146 | + public function getItemsAsJsonString(array $keys = [], int $option = 0, int $depth = 512): string; |
|
147 | + |
|
148 | + /** |
|
149 | + * @param CacheItemInterface $item |
|
150 | + * @return mixed |
|
151 | + */ |
|
152 | + public function setItem(CacheItemInterface $item); |
|
153 | + |
|
154 | + /** |
|
155 | + * @return DriverStatistic |
|
156 | + */ |
|
157 | + public function getStats(): DriverStatistic; |
|
158 | + |
|
159 | + /** |
|
160 | + * Get a quick help guide |
|
161 | + * about the current driver |
|
162 | + * |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + public function getHelp(): string; |
|
166 | + |
|
167 | + /** |
|
168 | + * @param CacheItemInterface $item |
|
169 | + * @return void |
|
170 | + */ |
|
171 | + public function detachItem(CacheItemInterface $item); |
|
172 | + |
|
173 | + /** |
|
174 | + * @return void |
|
175 | + */ |
|
176 | + public function detachAllItems(); |
|
177 | + |
|
178 | + /** |
|
179 | + * @param CacheItemInterface $item |
|
180 | + * @return void |
|
181 | + * @throws PhpfastcacheLogicException |
|
182 | + */ |
|
183 | + public function attachItem(CacheItemInterface $item); |
|
184 | + |
|
185 | + /** |
|
186 | + * Returns true if the item exists, is attached and the Spl Hash matches |
|
187 | + * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
188 | + * Returns null if the item does not exists |
|
189 | + * |
|
190 | + * @param CacheItemInterface $item |
|
191 | + * @return bool|null |
|
192 | + * @throws PhpfastcacheLogicException |
|
193 | + */ |
|
194 | + public function isAttached(CacheItemInterface $item); |
|
195 | + |
|
196 | + /** |
|
197 | + * Save multiple items, possible uses: |
|
198 | + * saveMultiple([$item1, $item2, $item3]); |
|
199 | + * saveMultiple($item1, $item2, $item3); |
|
200 | + * |
|
201 | + * @param ExtendedCacheItemInterface[] $items |
|
202 | + * @return bool |
|
203 | + */ |
|
204 | + public function saveMultiple(...$items): bool; |
|
205 | + |
|
206 | + |
|
207 | + /** |
|
208 | + * @return DriverIO |
|
209 | + */ |
|
210 | + public function getIO(): DriverIO; |
|
211 | 211 | } |
@@ -31,218 +31,218 @@ |
||
31 | 31 | */ |
32 | 32 | trait DriverBaseTrait |
33 | 33 | { |
34 | - use ExtendedCacheItemPoolTrait; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var ConfigurationOption the options |
|
38 | - */ |
|
39 | - protected $config; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var bool |
|
43 | - */ |
|
44 | - protected $fallback = false; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var object Instance of driver service |
|
48 | - */ |
|
49 | - protected $instance; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $driverName; |
|
55 | - |
|
56 | - /** |
|
57 | - * @internal This variable is read-access only |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $instanceId; |
|
61 | - |
|
62 | - /** |
|
63 | - * Driver constructor. |
|
64 | - * @param ConfigurationOption $config |
|
65 | - * @param string $instanceId |
|
66 | - * @throws PhpfastcacheDriverCheckException |
|
67 | - * @throws PhpfastcacheDriverConnectException |
|
68 | - */ |
|
69 | - public function __construct(ConfigurationOption $config, $instanceId) |
|
70 | - { |
|
71 | - $this->setConfig($config); |
|
72 | - $this->instanceId = $instanceId; |
|
73 | - $this->IO = new DriverIO(); |
|
74 | - |
|
75 | - if (!$this->driverCheck()) { |
|
76 | - throw new PhpfastcacheDriverCheckException(\sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
|
77 | - } |
|
78 | - |
|
79 | - try { |
|
80 | - $this->driverConnect(); |
|
81 | - } catch (Exception $e) { |
|
82 | - throw new PhpfastcacheDriverConnectException( |
|
83 | - sprintf( |
|
84 | - self::DRIVER_CONNECT_FAILURE, |
|
85 | - $this->getDriverName(), |
|
86 | - $e->getMessage(), |
|
87 | - $e->getLine() ?: 'unknown line', |
|
88 | - $e->getFile() ?: 'unknown file' |
|
89 | - ) |
|
90 | - ); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return string |
|
96 | - */ |
|
97 | - public function getDriverName(): string |
|
98 | - { |
|
99 | - if (!$this->driverName) { |
|
100 | - $this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1)); |
|
101 | - } |
|
102 | - return $this->driverName; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @return ConfigurationOption |
|
107 | - */ |
|
108 | - public function getDefaultConfig(): ConfigurationOption |
|
109 | - { |
|
110 | - $className = self::getConfigClass(); |
|
111 | - return new $className; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public static function getConfigClass(): string |
|
118 | - { |
|
119 | - $localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config'; |
|
120 | - if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) { |
|
121 | - return $localConfigClass; |
|
122 | - } |
|
123 | - return ConfigurationOption::class; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param ExtendedCacheItemInterface $item |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function driverPreWrap(ExtendedCacheItemInterface $item): array |
|
131 | - { |
|
132 | - $wrap = [ |
|
133 | - self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key |
|
134 | - self::DRIVER_DATA_WRAPPER_INDEX => $item->get(), |
|
135 | - self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
136 | - self::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(), |
|
137 | - ]; |
|
138 | - |
|
139 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
140 | - $wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime(); |
|
141 | - /** |
|
142 | - * If the creation date exists |
|
143 | - * reuse it else set a new Date |
|
144 | - */ |
|
145 | - $wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate() ?: new DateTime(); |
|
146 | - } else { |
|
147 | - $wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = null; |
|
148 | - $wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = null; |
|
149 | - } |
|
150 | - |
|
151 | - return $wrap; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @return ConfigurationOption |
|
156 | - */ |
|
157 | - public function getConfig(): ConfigurationOption |
|
158 | - { |
|
159 | - return $this->config; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @param ConfigurationOption $config |
|
164 | - */ |
|
165 | - public function setConfig(ConfigurationOption $config) |
|
166 | - { |
|
167 | - $this->config = $config; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @param array $wrapper |
|
172 | - * @return mixed |
|
173 | - */ |
|
174 | - public function driverUnwrapData(array $wrapper) |
|
175 | - { |
|
176 | - return $wrapper[self::DRIVER_DATA_WRAPPER_INDEX]; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * @param array $wrapper |
|
182 | - * @return DateTime |
|
183 | - */ |
|
184 | - public function driverUnwrapEdate(array $wrapper) |
|
185 | - { |
|
186 | - return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX]; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @param array $wrapper |
|
191 | - * @return DateTime |
|
192 | - */ |
|
193 | - public function driverUnwrapCdate(array $wrapper) |
|
194 | - { |
|
195 | - return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX]; |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * @param array $wrapper |
|
200 | - * @return DateTime |
|
201 | - */ |
|
202 | - public function driverUnwrapMdate(array $wrapper) |
|
203 | - { |
|
204 | - return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX]; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @return string |
|
209 | - */ |
|
210 | - public function getInstanceId(): string |
|
211 | - { |
|
212 | - return $this->instanceId; |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * Encode data types such as object/array |
|
218 | - * for driver that does not support |
|
219 | - * non-scalar value |
|
220 | - * @param $data |
|
221 | - * @return string |
|
222 | - */ |
|
223 | - protected function encode($data): string |
|
224 | - { |
|
225 | - return \serialize($data); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Decode data types such as object/array |
|
230 | - * for driver that does not support |
|
231 | - * non-scalar value |
|
232 | - * @param string|null $value |
|
233 | - * @return mixed |
|
234 | - */ |
|
235 | - protected function decode($value) |
|
236 | - { |
|
237 | - return \unserialize((string)$value, ['allowed_classes' => true]); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Check if phpModule or CGI |
|
242 | - * @return bool |
|
243 | - */ |
|
244 | - protected function isPHPModule(): bool |
|
245 | - { |
|
246 | - return (\PHP_SAPI === 'apache2handler' || \strpos(\PHP_SAPI, 'handler') !== false); |
|
247 | - } |
|
34 | + use ExtendedCacheItemPoolTrait; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var ConfigurationOption the options |
|
38 | + */ |
|
39 | + protected $config; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var bool |
|
43 | + */ |
|
44 | + protected $fallback = false; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var object Instance of driver service |
|
48 | + */ |
|
49 | + protected $instance; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $driverName; |
|
55 | + |
|
56 | + /** |
|
57 | + * @internal This variable is read-access only |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $instanceId; |
|
61 | + |
|
62 | + /** |
|
63 | + * Driver constructor. |
|
64 | + * @param ConfigurationOption $config |
|
65 | + * @param string $instanceId |
|
66 | + * @throws PhpfastcacheDriverCheckException |
|
67 | + * @throws PhpfastcacheDriverConnectException |
|
68 | + */ |
|
69 | + public function __construct(ConfigurationOption $config, $instanceId) |
|
70 | + { |
|
71 | + $this->setConfig($config); |
|
72 | + $this->instanceId = $instanceId; |
|
73 | + $this->IO = new DriverIO(); |
|
74 | + |
|
75 | + if (!$this->driverCheck()) { |
|
76 | + throw new PhpfastcacheDriverCheckException(\sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
|
77 | + } |
|
78 | + |
|
79 | + try { |
|
80 | + $this->driverConnect(); |
|
81 | + } catch (Exception $e) { |
|
82 | + throw new PhpfastcacheDriverConnectException( |
|
83 | + sprintf( |
|
84 | + self::DRIVER_CONNECT_FAILURE, |
|
85 | + $this->getDriverName(), |
|
86 | + $e->getMessage(), |
|
87 | + $e->getLine() ?: 'unknown line', |
|
88 | + $e->getFile() ?: 'unknown file' |
|
89 | + ) |
|
90 | + ); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return string |
|
96 | + */ |
|
97 | + public function getDriverName(): string |
|
98 | + { |
|
99 | + if (!$this->driverName) { |
|
100 | + $this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1)); |
|
101 | + } |
|
102 | + return $this->driverName; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @return ConfigurationOption |
|
107 | + */ |
|
108 | + public function getDefaultConfig(): ConfigurationOption |
|
109 | + { |
|
110 | + $className = self::getConfigClass(); |
|
111 | + return new $className; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public static function getConfigClass(): string |
|
118 | + { |
|
119 | + $localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config'; |
|
120 | + if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) { |
|
121 | + return $localConfigClass; |
|
122 | + } |
|
123 | + return ConfigurationOption::class; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param ExtendedCacheItemInterface $item |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function driverPreWrap(ExtendedCacheItemInterface $item): array |
|
131 | + { |
|
132 | + $wrap = [ |
|
133 | + self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key |
|
134 | + self::DRIVER_DATA_WRAPPER_INDEX => $item->get(), |
|
135 | + self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
136 | + self::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(), |
|
137 | + ]; |
|
138 | + |
|
139 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
140 | + $wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime(); |
|
141 | + /** |
|
142 | + * If the creation date exists |
|
143 | + * reuse it else set a new Date |
|
144 | + */ |
|
145 | + $wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate() ?: new DateTime(); |
|
146 | + } else { |
|
147 | + $wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = null; |
|
148 | + $wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = null; |
|
149 | + } |
|
150 | + |
|
151 | + return $wrap; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @return ConfigurationOption |
|
156 | + */ |
|
157 | + public function getConfig(): ConfigurationOption |
|
158 | + { |
|
159 | + return $this->config; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @param ConfigurationOption $config |
|
164 | + */ |
|
165 | + public function setConfig(ConfigurationOption $config) |
|
166 | + { |
|
167 | + $this->config = $config; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @param array $wrapper |
|
172 | + * @return mixed |
|
173 | + */ |
|
174 | + public function driverUnwrapData(array $wrapper) |
|
175 | + { |
|
176 | + return $wrapper[self::DRIVER_DATA_WRAPPER_INDEX]; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * @param array $wrapper |
|
182 | + * @return DateTime |
|
183 | + */ |
|
184 | + public function driverUnwrapEdate(array $wrapper) |
|
185 | + { |
|
186 | + return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX]; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @param array $wrapper |
|
191 | + * @return DateTime |
|
192 | + */ |
|
193 | + public function driverUnwrapCdate(array $wrapper) |
|
194 | + { |
|
195 | + return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX]; |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * @param array $wrapper |
|
200 | + * @return DateTime |
|
201 | + */ |
|
202 | + public function driverUnwrapMdate(array $wrapper) |
|
203 | + { |
|
204 | + return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX]; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @return string |
|
209 | + */ |
|
210 | + public function getInstanceId(): string |
|
211 | + { |
|
212 | + return $this->instanceId; |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * Encode data types such as object/array |
|
218 | + * for driver that does not support |
|
219 | + * non-scalar value |
|
220 | + * @param $data |
|
221 | + * @return string |
|
222 | + */ |
|
223 | + protected function encode($data): string |
|
224 | + { |
|
225 | + return \serialize($data); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Decode data types such as object/array |
|
230 | + * for driver that does not support |
|
231 | + * non-scalar value |
|
232 | + * @param string|null $value |
|
233 | + * @return mixed |
|
234 | + */ |
|
235 | + protected function decode($value) |
|
236 | + { |
|
237 | + return \unserialize((string)$value, ['allowed_classes' => true]); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Check if phpModule or CGI |
|
242 | + * @return bool |
|
243 | + */ |
|
244 | + protected function isPHPModule(): bool |
|
245 | + { |
|
246 | + return (\PHP_SAPI === 'apache2handler' || \strpos(\PHP_SAPI, 'handler') !== false); |
|
247 | + } |
|
248 | 248 | } |
@@ -39,413 +39,413 @@ |
||
39 | 39 | */ |
40 | 40 | trait CacheItemPoolTrait |
41 | 41 | { |
42 | - use ClassNamespaceResolverTrait; |
|
43 | - use EventManagerDispatcherTrait; |
|
44 | - use TaggableCacheItemPoolTrait; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - protected static $unsupportedKeyChars = '{}()/\@:'; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var array |
|
53 | - */ |
|
54 | - protected $deferredList = []; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var ExtendedCacheItemInterface[] |
|
58 | - */ |
|
59 | - protected $itemInstances = []; |
|
60 | - |
|
61 | - /**CacheItemPoolTrait |
|
42 | + use ClassNamespaceResolverTrait; |
|
43 | + use EventManagerDispatcherTrait; |
|
44 | + use TaggableCacheItemPoolTrait; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + protected static $unsupportedKeyChars = '{}()/\@:'; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var array |
|
53 | + */ |
|
54 | + protected $deferredList = []; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var ExtendedCacheItemInterface[] |
|
58 | + */ |
|
59 | + protected $itemInstances = []; |
|
60 | + |
|
61 | + /**CacheItemPoolTrait |
|
62 | 62 | * @param CacheItemInterface $item |
63 | 63 | * @return $this |
64 | 64 | * @throws PhpfastcacheInvalidArgumentException |
65 | 65 | */ |
66 | - public function setItem(CacheItemInterface $item) |
|
67 | - { |
|
68 | - if ($this->getClassNamespace() . '\\Item' === \get_class($item)) { |
|
69 | - if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
70 | - throw new PhpfastcacheLogicException( |
|
71 | - 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
72 | - ); |
|
73 | - } |
|
74 | - |
|
75 | - $this->itemInstances[$item->getKey()] = $item; |
|
76 | - |
|
77 | - return $this; |
|
78 | - } |
|
79 | - throw new PhpfastcacheInvalidArgumentException( |
|
80 | - \sprintf( |
|
81 | - 'Invalid Item Class "%s" for this driver "%s".', |
|
82 | - get_class($item), |
|
83 | - get_class($this) |
|
84 | - ) |
|
85 | - ); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param array $keys |
|
90 | - * @return array |
|
91 | - * @throws PhpfastcacheCoreException |
|
92 | - * @throws PhpfastcacheInvalidArgumentException |
|
93 | - * @throws PhpfastcacheLogicException |
|
94 | - */ |
|
95 | - public function getItems(array $keys = []) |
|
96 | - { |
|
97 | - $collection = []; |
|
98 | - foreach ($keys as $key) { |
|
99 | - $collection[$key] = $this->getItem($key); |
|
100 | - } |
|
101 | - |
|
102 | - return $collection; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $key |
|
107 | - * @return ExtendedCacheItemInterface |
|
108 | - * @throws PhpfastcacheInvalidArgumentException |
|
109 | - * @throws PhpfastcacheLogicException |
|
110 | - * @throws PhpfastcacheCoreException |
|
111 | - */ |
|
112 | - public function getItem($key) |
|
113 | - { |
|
114 | - if (\is_string($key)) { |
|
115 | - $item = null; |
|
116 | - |
|
117 | - /** |
|
118 | - * Replace array_key_exists by isset |
|
119 | - * due to performance issue on huge |
|
120 | - * loop dispatching operations |
|
121 | - */ |
|
122 | - if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
|
123 | - if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
|
124 | - throw new PhpfastcacheInvalidArgumentException( |
|
125 | - 'Unsupported key character detected: "' . $matches[1] . '". Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers' |
|
126 | - ); |
|
127 | - } |
|
128 | - |
|
129 | - $cacheSlamsSpendSeconds = 0; |
|
130 | - $class = $this->getClassNamespace() . '\Item'; |
|
131 | - /** @var $item ExtendedCacheItemInterface */ |
|
132 | - $item = new $class($this, $key); |
|
133 | - $item->setEventManager($this->eventManager); |
|
134 | - |
|
135 | - getItemDriverRead: |
|
136 | - { |
|
137 | - $driverArray = $this->driverRead($item); |
|
138 | - |
|
139 | - if ($driverArray) { |
|
140 | - if (!\is_array($driverArray)) { |
|
141 | - throw new PhpfastcacheCoreException( |
|
142 | - sprintf( |
|
143 | - 'The driverRead method returned an unexpected variable type: %s', |
|
144 | - \gettype($driverArray) |
|
145 | - ) |
|
146 | - ); |
|
147 | - } |
|
148 | - $driverData = $this->driverUnwrapData($driverArray); |
|
149 | - |
|
150 | - if ($this->getConfig()['preventCacheSlams']) { |
|
151 | - while ($driverData instanceof ItemBatch) { |
|
152 | - if ($driverData->getItemDate()->getTimestamp() + $this->getConfig()->getCacheSlamsTimeout() < \time()) { |
|
153 | - /** |
|
154 | - * The timeout has been reached |
|
155 | - * Consider that the batch has |
|
156 | - * failed and serve an empty item |
|
157 | - * to avoid to get stuck with a |
|
158 | - * batch item stored in driver |
|
159 | - */ |
|
160 | - goto getItemDriverExpired; |
|
161 | - } |
|
162 | - /** |
|
163 | - * @eventName CacheGetItem |
|
164 | - * @param $this ExtendedCacheItemPoolInterface |
|
165 | - * @param $driverData ItemBatch |
|
166 | - * @param $cacheSlamsSpendSeconds int |
|
167 | - */ |
|
168 | - $this->eventManager->dispatch('CacheGetItemInSlamBatch', $this, $driverData, $cacheSlamsSpendSeconds); |
|
169 | - |
|
170 | - /** |
|
171 | - * Wait for a second before |
|
172 | - * attempting to get exit |
|
173 | - * the current batch process |
|
174 | - */ |
|
175 | - \sleep(1); |
|
176 | - $cacheSlamsSpendSeconds++; |
|
177 | - goto getItemDriverRead; |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - $item->set($driverData); |
|
182 | - $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
183 | - |
|
184 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
185 | - /** |
|
186 | - * If the itemDetailedDate has been |
|
187 | - * set after caching, we MUST inject |
|
188 | - * a new DateTime object on the fly |
|
189 | - */ |
|
190 | - $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
191 | - $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
192 | - } |
|
193 | - |
|
194 | - $item->setTags($this->driverUnwrapTags($driverArray)); |
|
195 | - |
|
196 | - getItemDriverExpired: |
|
197 | - if ($item->isExpired()) { |
|
198 | - /** |
|
199 | - * Using driverDelete() instead of delete() |
|
200 | - * to avoid infinite loop caused by |
|
201 | - * getItem() call in delete() method |
|
202 | - * As we MUST return an item in any |
|
203 | - * way, we do not de-register here |
|
204 | - */ |
|
205 | - $this->driverDelete($item); |
|
206 | - |
|
207 | - /** |
|
208 | - * Reset the Item |
|
209 | - */ |
|
210 | - $item->set(null) |
|
211 | - ->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])) |
|
212 | - ->setHit(false) |
|
213 | - ->setTags([]); |
|
214 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
215 | - /** |
|
216 | - * If the itemDetailedDate has been |
|
217 | - * set after caching, we MUST inject |
|
218 | - * a new DateTime object on the fly |
|
219 | - */ |
|
220 | - $item->setCreationDate(new DateTime()); |
|
221 | - $item->setModificationDate(new DateTime()); |
|
222 | - } |
|
223 | - } else { |
|
224 | - $item->setHit(true); |
|
225 | - } |
|
226 | - } else { |
|
227 | - $item->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])); |
|
228 | - } |
|
229 | - } |
|
230 | - }else{ |
|
231 | - $item = $this->itemInstances[$key]; |
|
232 | - } |
|
233 | - |
|
234 | - |
|
235 | - if($item !== null){ |
|
236 | - /** |
|
237 | - * @eventName CacheGetItem |
|
238 | - * @param $this ExtendedCacheItemPoolInterface |
|
239 | - * @param $this ExtendedCacheItemInterface |
|
240 | - */ |
|
241 | - $this->eventManager->dispatch('CacheGetItem', $this, $item); |
|
242 | - |
|
243 | - $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
244 | - |
|
245 | - return $item; |
|
246 | - } |
|
247 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('Item %s was not build due to an unknown error', \gettype($key))); |
|
248 | - } |
|
249 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('$key must be a string, got type "%s" instead.', \gettype($key))); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * @param string $key |
|
254 | - * @return bool |
|
255 | - * @throws PhpfastcacheInvalidArgumentException |
|
256 | - */ |
|
257 | - public function hasItem($key) |
|
258 | - { |
|
259 | - return $this->getItem($key)->isHit(); |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * @return bool |
|
264 | - */ |
|
265 | - public function clear() |
|
266 | - { |
|
267 | - /** |
|
268 | - * @eventName CacheClearItem |
|
269 | - * @param $this ExtendedCacheItemPoolInterface |
|
270 | - * @param $itemInstances ExtendedCacheItemInterface[] |
|
271 | - */ |
|
272 | - $this->eventManager->dispatch('CacheClearItem', $this, $this->itemInstances); |
|
273 | - |
|
274 | - $this->getIO()->incWriteHit(); |
|
275 | - // Faster than detachAllItems() |
|
276 | - $this->itemInstances = []; |
|
277 | - |
|
278 | - return $this->driverClear(); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @param array $keys |
|
283 | - * @return bool |
|
284 | - * @throws PhpfastcacheInvalidArgumentException |
|
285 | - */ |
|
286 | - public function deleteItems(array $keys) |
|
287 | - { |
|
288 | - $return = null; |
|
289 | - foreach ($keys as $key) { |
|
290 | - $result = $this->deleteItem($key); |
|
291 | - if ($result !== false) { |
|
292 | - $return = $result; |
|
293 | - } |
|
294 | - } |
|
295 | - |
|
296 | - return (bool)$return; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @param string $key |
|
301 | - * @return bool |
|
302 | - * @throws PhpfastcacheInvalidArgumentException |
|
303 | - */ |
|
304 | - public function deleteItem($key) |
|
305 | - { |
|
306 | - $item = $this->getItem($key); |
|
307 | - if ($item->isHit() && $this->driverDelete($item)) { |
|
308 | - $item->setHit(false); |
|
309 | - $this->getIO()->incWriteHit(); |
|
310 | - |
|
311 | - /** |
|
312 | - * @eventName CacheCommitItem |
|
313 | - * @param $this ExtendedCacheItemPoolInterface |
|
314 | - * @param $item ExtendedCacheItemInterface |
|
315 | - */ |
|
316 | - $this->eventManager->dispatch('CacheDeleteItem', $this, $item); |
|
317 | - |
|
318 | - /** |
|
319 | - * De-register the item instance |
|
320 | - * then collect gc cycles |
|
321 | - */ |
|
322 | - $this->deregisterItem($key); |
|
323 | - |
|
324 | - /** |
|
325 | - * Perform a tag cleanup to avoid memory leaks |
|
326 | - */ |
|
327 | - if (\strpos($key, self::DRIVER_TAGS_KEY_PREFIX) !== 0) { |
|
328 | - $this->cleanItemTags($item); |
|
329 | - } |
|
330 | - |
|
331 | - return true; |
|
332 | - } |
|
333 | - |
|
334 | - return false; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @param CacheItemInterface $item |
|
339 | - * @return CacheItemInterface |
|
340 | - * @throws RuntimeException |
|
341 | - */ |
|
342 | - public function saveDeferred(CacheItemInterface $item) |
|
343 | - { |
|
344 | - if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
|
345 | - $this->itemInstances[$item->getKey()] = $item; |
|
346 | - } else { |
|
347 | - if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
348 | - throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
349 | - } |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * @eventName CacheSaveDeferredItem |
|
354 | - * @param $this ExtendedCacheItemPoolInterface |
|
355 | - * @param $this ExtendedCacheItemInterface |
|
356 | - */ |
|
357 | - $this->eventManager->dispatch('CacheSaveDeferredItem', $this, $item); |
|
358 | - |
|
359 | - return $this->deferredList[$item->getKey()] = $item; |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * @return bool |
|
364 | - * @throws PhpfastcacheInvalidArgumentException |
|
365 | - */ |
|
366 | - public function commit() |
|
367 | - { |
|
368 | - /** |
|
369 | - * @eventName CacheCommitItem |
|
370 | - * @param $this ExtendedCacheItemPoolInterface |
|
371 | - * @param $deferredList ExtendedCacheItemInterface[] |
|
372 | - */ |
|
373 | - $this->eventManager->dispatch('CacheCommitItem', $this, $this->deferredList); |
|
374 | - |
|
375 | - $return = null; |
|
376 | - foreach ($this->deferredList as $key => $item) { |
|
377 | - $result = $this->save($item); |
|
378 | - if ($return !== false) { |
|
379 | - unset($this->deferredList[$key]); |
|
380 | - $return = $result; |
|
381 | - } |
|
382 | - } |
|
383 | - |
|
384 | - return (bool)$return; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * @param CacheItemInterface $item |
|
389 | - * @return bool |
|
390 | - * @throws PhpfastcacheInvalidArgumentException |
|
391 | - * @throws PhpfastcacheLogicException |
|
392 | - * @throws \ReflectionException |
|
393 | - */ |
|
394 | - public function save(CacheItemInterface $item) |
|
395 | - { |
|
396 | - /** |
|
397 | - * @var ExtendedCacheItemInterface $item |
|
398 | - * |
|
399 | - * Replace array_key_exists by isset |
|
400 | - * due to performance issue on huge |
|
401 | - * loop dispatching operations |
|
402 | - */ |
|
403 | - if (!isset($this->itemInstances[$item->getKey()])) { |
|
404 | - if($this->getConfig()->isUseStaticItemCaching()){ |
|
405 | - $this->itemInstances[$item->getKey()] = $item; |
|
406 | - } |
|
407 | - } else { |
|
408 | - if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
409 | - throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
410 | - } |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * @eventName CacheSaveItem |
|
415 | - * @param $this ExtendedCacheItemPoolInterface |
|
416 | - * @param $this ExtendedCacheItemInterface |
|
417 | - */ |
|
418 | - $this->eventManager->dispatch('CacheSaveItem', $this, $item); |
|
419 | - |
|
420 | - |
|
421 | - if ($this->getConfig()->isPreventCacheSlams()) { |
|
422 | - /** |
|
423 | - * @var $itemBatch ExtendedCacheItemInterface |
|
424 | - */ |
|
425 | - $class = new ReflectionClass((new ReflectionObject($this))->getNamespaceName() . '\Item'); |
|
426 | - $itemBatch = $class->newInstanceArgs([$this, $item->getKey()]); |
|
427 | - $itemBatch->setEventManager($this->eventManager) |
|
428 | - ->set(new ItemBatch($item->getKey(), new DateTime())) |
|
429 | - ->expiresAfter($this->getConfig()->getCacheSlamsTimeout()); |
|
430 | - |
|
431 | - /** |
|
432 | - * To avoid SPL mismatches |
|
433 | - * we have to re-attach the |
|
434 | - * original item to the pool |
|
435 | - */ |
|
436 | - $this->driverWrite($itemBatch); |
|
437 | - $this->detachItem($itemBatch); |
|
438 | - $this->attachItem($item); |
|
439 | - } |
|
440 | - |
|
441 | - |
|
442 | - if ($this->driverWrite($item) && $this->driverWriteTags($item)) { |
|
443 | - $item->setHit(true); |
|
444 | - $this->getIO()->incWriteHit(); |
|
445 | - |
|
446 | - return true; |
|
447 | - } |
|
448 | - |
|
449 | - return false; |
|
450 | - } |
|
66 | + public function setItem(CacheItemInterface $item) |
|
67 | + { |
|
68 | + if ($this->getClassNamespace() . '\\Item' === \get_class($item)) { |
|
69 | + if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
70 | + throw new PhpfastcacheLogicException( |
|
71 | + 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
72 | + ); |
|
73 | + } |
|
74 | + |
|
75 | + $this->itemInstances[$item->getKey()] = $item; |
|
76 | + |
|
77 | + return $this; |
|
78 | + } |
|
79 | + throw new PhpfastcacheInvalidArgumentException( |
|
80 | + \sprintf( |
|
81 | + 'Invalid Item Class "%s" for this driver "%s".', |
|
82 | + get_class($item), |
|
83 | + get_class($this) |
|
84 | + ) |
|
85 | + ); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param array $keys |
|
90 | + * @return array |
|
91 | + * @throws PhpfastcacheCoreException |
|
92 | + * @throws PhpfastcacheInvalidArgumentException |
|
93 | + * @throws PhpfastcacheLogicException |
|
94 | + */ |
|
95 | + public function getItems(array $keys = []) |
|
96 | + { |
|
97 | + $collection = []; |
|
98 | + foreach ($keys as $key) { |
|
99 | + $collection[$key] = $this->getItem($key); |
|
100 | + } |
|
101 | + |
|
102 | + return $collection; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $key |
|
107 | + * @return ExtendedCacheItemInterface |
|
108 | + * @throws PhpfastcacheInvalidArgumentException |
|
109 | + * @throws PhpfastcacheLogicException |
|
110 | + * @throws PhpfastcacheCoreException |
|
111 | + */ |
|
112 | + public function getItem($key) |
|
113 | + { |
|
114 | + if (\is_string($key)) { |
|
115 | + $item = null; |
|
116 | + |
|
117 | + /** |
|
118 | + * Replace array_key_exists by isset |
|
119 | + * due to performance issue on huge |
|
120 | + * loop dispatching operations |
|
121 | + */ |
|
122 | + if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
|
123 | + if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
|
124 | + throw new PhpfastcacheInvalidArgumentException( |
|
125 | + 'Unsupported key character detected: "' . $matches[1] . '". Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers' |
|
126 | + ); |
|
127 | + } |
|
128 | + |
|
129 | + $cacheSlamsSpendSeconds = 0; |
|
130 | + $class = $this->getClassNamespace() . '\Item'; |
|
131 | + /** @var $item ExtendedCacheItemInterface */ |
|
132 | + $item = new $class($this, $key); |
|
133 | + $item->setEventManager($this->eventManager); |
|
134 | + |
|
135 | + getItemDriverRead: |
|
136 | + { |
|
137 | + $driverArray = $this->driverRead($item); |
|
138 | + |
|
139 | + if ($driverArray) { |
|
140 | + if (!\is_array($driverArray)) { |
|
141 | + throw new PhpfastcacheCoreException( |
|
142 | + sprintf( |
|
143 | + 'The driverRead method returned an unexpected variable type: %s', |
|
144 | + \gettype($driverArray) |
|
145 | + ) |
|
146 | + ); |
|
147 | + } |
|
148 | + $driverData = $this->driverUnwrapData($driverArray); |
|
149 | + |
|
150 | + if ($this->getConfig()['preventCacheSlams']) { |
|
151 | + while ($driverData instanceof ItemBatch) { |
|
152 | + if ($driverData->getItemDate()->getTimestamp() + $this->getConfig()->getCacheSlamsTimeout() < \time()) { |
|
153 | + /** |
|
154 | + * The timeout has been reached |
|
155 | + * Consider that the batch has |
|
156 | + * failed and serve an empty item |
|
157 | + * to avoid to get stuck with a |
|
158 | + * batch item stored in driver |
|
159 | + */ |
|
160 | + goto getItemDriverExpired; |
|
161 | + } |
|
162 | + /** |
|
163 | + * @eventName CacheGetItem |
|
164 | + * @param $this ExtendedCacheItemPoolInterface |
|
165 | + * @param $driverData ItemBatch |
|
166 | + * @param $cacheSlamsSpendSeconds int |
|
167 | + */ |
|
168 | + $this->eventManager->dispatch('CacheGetItemInSlamBatch', $this, $driverData, $cacheSlamsSpendSeconds); |
|
169 | + |
|
170 | + /** |
|
171 | + * Wait for a second before |
|
172 | + * attempting to get exit |
|
173 | + * the current batch process |
|
174 | + */ |
|
175 | + \sleep(1); |
|
176 | + $cacheSlamsSpendSeconds++; |
|
177 | + goto getItemDriverRead; |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + $item->set($driverData); |
|
182 | + $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
183 | + |
|
184 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
185 | + /** |
|
186 | + * If the itemDetailedDate has been |
|
187 | + * set after caching, we MUST inject |
|
188 | + * a new DateTime object on the fly |
|
189 | + */ |
|
190 | + $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
191 | + $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
192 | + } |
|
193 | + |
|
194 | + $item->setTags($this->driverUnwrapTags($driverArray)); |
|
195 | + |
|
196 | + getItemDriverExpired: |
|
197 | + if ($item->isExpired()) { |
|
198 | + /** |
|
199 | + * Using driverDelete() instead of delete() |
|
200 | + * to avoid infinite loop caused by |
|
201 | + * getItem() call in delete() method |
|
202 | + * As we MUST return an item in any |
|
203 | + * way, we do not de-register here |
|
204 | + */ |
|
205 | + $this->driverDelete($item); |
|
206 | + |
|
207 | + /** |
|
208 | + * Reset the Item |
|
209 | + */ |
|
210 | + $item->set(null) |
|
211 | + ->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])) |
|
212 | + ->setHit(false) |
|
213 | + ->setTags([]); |
|
214 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
215 | + /** |
|
216 | + * If the itemDetailedDate has been |
|
217 | + * set after caching, we MUST inject |
|
218 | + * a new DateTime object on the fly |
|
219 | + */ |
|
220 | + $item->setCreationDate(new DateTime()); |
|
221 | + $item->setModificationDate(new DateTime()); |
|
222 | + } |
|
223 | + } else { |
|
224 | + $item->setHit(true); |
|
225 | + } |
|
226 | + } else { |
|
227 | + $item->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])); |
|
228 | + } |
|
229 | + } |
|
230 | + }else{ |
|
231 | + $item = $this->itemInstances[$key]; |
|
232 | + } |
|
233 | + |
|
234 | + |
|
235 | + if($item !== null){ |
|
236 | + /** |
|
237 | + * @eventName CacheGetItem |
|
238 | + * @param $this ExtendedCacheItemPoolInterface |
|
239 | + * @param $this ExtendedCacheItemInterface |
|
240 | + */ |
|
241 | + $this->eventManager->dispatch('CacheGetItem', $this, $item); |
|
242 | + |
|
243 | + $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
244 | + |
|
245 | + return $item; |
|
246 | + } |
|
247 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('Item %s was not build due to an unknown error', \gettype($key))); |
|
248 | + } |
|
249 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('$key must be a string, got type "%s" instead.', \gettype($key))); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * @param string $key |
|
254 | + * @return bool |
|
255 | + * @throws PhpfastcacheInvalidArgumentException |
|
256 | + */ |
|
257 | + public function hasItem($key) |
|
258 | + { |
|
259 | + return $this->getItem($key)->isHit(); |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * @return bool |
|
264 | + */ |
|
265 | + public function clear() |
|
266 | + { |
|
267 | + /** |
|
268 | + * @eventName CacheClearItem |
|
269 | + * @param $this ExtendedCacheItemPoolInterface |
|
270 | + * @param $itemInstances ExtendedCacheItemInterface[] |
|
271 | + */ |
|
272 | + $this->eventManager->dispatch('CacheClearItem', $this, $this->itemInstances); |
|
273 | + |
|
274 | + $this->getIO()->incWriteHit(); |
|
275 | + // Faster than detachAllItems() |
|
276 | + $this->itemInstances = []; |
|
277 | + |
|
278 | + return $this->driverClear(); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @param array $keys |
|
283 | + * @return bool |
|
284 | + * @throws PhpfastcacheInvalidArgumentException |
|
285 | + */ |
|
286 | + public function deleteItems(array $keys) |
|
287 | + { |
|
288 | + $return = null; |
|
289 | + foreach ($keys as $key) { |
|
290 | + $result = $this->deleteItem($key); |
|
291 | + if ($result !== false) { |
|
292 | + $return = $result; |
|
293 | + } |
|
294 | + } |
|
295 | + |
|
296 | + return (bool)$return; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @param string $key |
|
301 | + * @return bool |
|
302 | + * @throws PhpfastcacheInvalidArgumentException |
|
303 | + */ |
|
304 | + public function deleteItem($key) |
|
305 | + { |
|
306 | + $item = $this->getItem($key); |
|
307 | + if ($item->isHit() && $this->driverDelete($item)) { |
|
308 | + $item->setHit(false); |
|
309 | + $this->getIO()->incWriteHit(); |
|
310 | + |
|
311 | + /** |
|
312 | + * @eventName CacheCommitItem |
|
313 | + * @param $this ExtendedCacheItemPoolInterface |
|
314 | + * @param $item ExtendedCacheItemInterface |
|
315 | + */ |
|
316 | + $this->eventManager->dispatch('CacheDeleteItem', $this, $item); |
|
317 | + |
|
318 | + /** |
|
319 | + * De-register the item instance |
|
320 | + * then collect gc cycles |
|
321 | + */ |
|
322 | + $this->deregisterItem($key); |
|
323 | + |
|
324 | + /** |
|
325 | + * Perform a tag cleanup to avoid memory leaks |
|
326 | + */ |
|
327 | + if (\strpos($key, self::DRIVER_TAGS_KEY_PREFIX) !== 0) { |
|
328 | + $this->cleanItemTags($item); |
|
329 | + } |
|
330 | + |
|
331 | + return true; |
|
332 | + } |
|
333 | + |
|
334 | + return false; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @param CacheItemInterface $item |
|
339 | + * @return CacheItemInterface |
|
340 | + * @throws RuntimeException |
|
341 | + */ |
|
342 | + public function saveDeferred(CacheItemInterface $item) |
|
343 | + { |
|
344 | + if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
|
345 | + $this->itemInstances[$item->getKey()] = $item; |
|
346 | + } else { |
|
347 | + if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
348 | + throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
349 | + } |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * @eventName CacheSaveDeferredItem |
|
354 | + * @param $this ExtendedCacheItemPoolInterface |
|
355 | + * @param $this ExtendedCacheItemInterface |
|
356 | + */ |
|
357 | + $this->eventManager->dispatch('CacheSaveDeferredItem', $this, $item); |
|
358 | + |
|
359 | + return $this->deferredList[$item->getKey()] = $item; |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * @return bool |
|
364 | + * @throws PhpfastcacheInvalidArgumentException |
|
365 | + */ |
|
366 | + public function commit() |
|
367 | + { |
|
368 | + /** |
|
369 | + * @eventName CacheCommitItem |
|
370 | + * @param $this ExtendedCacheItemPoolInterface |
|
371 | + * @param $deferredList ExtendedCacheItemInterface[] |
|
372 | + */ |
|
373 | + $this->eventManager->dispatch('CacheCommitItem', $this, $this->deferredList); |
|
374 | + |
|
375 | + $return = null; |
|
376 | + foreach ($this->deferredList as $key => $item) { |
|
377 | + $result = $this->save($item); |
|
378 | + if ($return !== false) { |
|
379 | + unset($this->deferredList[$key]); |
|
380 | + $return = $result; |
|
381 | + } |
|
382 | + } |
|
383 | + |
|
384 | + return (bool)$return; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * @param CacheItemInterface $item |
|
389 | + * @return bool |
|
390 | + * @throws PhpfastcacheInvalidArgumentException |
|
391 | + * @throws PhpfastcacheLogicException |
|
392 | + * @throws \ReflectionException |
|
393 | + */ |
|
394 | + public function save(CacheItemInterface $item) |
|
395 | + { |
|
396 | + /** |
|
397 | + * @var ExtendedCacheItemInterface $item |
|
398 | + * |
|
399 | + * Replace array_key_exists by isset |
|
400 | + * due to performance issue on huge |
|
401 | + * loop dispatching operations |
|
402 | + */ |
|
403 | + if (!isset($this->itemInstances[$item->getKey()])) { |
|
404 | + if($this->getConfig()->isUseStaticItemCaching()){ |
|
405 | + $this->itemInstances[$item->getKey()] = $item; |
|
406 | + } |
|
407 | + } else { |
|
408 | + if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
409 | + throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
410 | + } |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * @eventName CacheSaveItem |
|
415 | + * @param $this ExtendedCacheItemPoolInterface |
|
416 | + * @param $this ExtendedCacheItemInterface |
|
417 | + */ |
|
418 | + $this->eventManager->dispatch('CacheSaveItem', $this, $item); |
|
419 | + |
|
420 | + |
|
421 | + if ($this->getConfig()->isPreventCacheSlams()) { |
|
422 | + /** |
|
423 | + * @var $itemBatch ExtendedCacheItemInterface |
|
424 | + */ |
|
425 | + $class = new ReflectionClass((new ReflectionObject($this))->getNamespaceName() . '\Item'); |
|
426 | + $itemBatch = $class->newInstanceArgs([$this, $item->getKey()]); |
|
427 | + $itemBatch->setEventManager($this->eventManager) |
|
428 | + ->set(new ItemBatch($item->getKey(), new DateTime())) |
|
429 | + ->expiresAfter($this->getConfig()->getCacheSlamsTimeout()); |
|
430 | + |
|
431 | + /** |
|
432 | + * To avoid SPL mismatches |
|
433 | + * we have to re-attach the |
|
434 | + * original item to the pool |
|
435 | + */ |
|
436 | + $this->driverWrite($itemBatch); |
|
437 | + $this->detachItem($itemBatch); |
|
438 | + $this->attachItem($item); |
|
439 | + } |
|
440 | + |
|
441 | + |
|
442 | + if ($this->driverWrite($item) && $this->driverWriteTags($item)) { |
|
443 | + $item->setHit(true); |
|
444 | + $this->getIO()->incWriteHit(); |
|
445 | + |
|
446 | + return true; |
|
447 | + } |
|
448 | + |
|
449 | + return false; |
|
450 | + } |
|
451 | 451 | } |