Skip to content

Commit 6b60a53

Browse files
committed
9.2 (WIP):
- __Core__ - Internal: Implemented multiple keys delete (*if supported by the backend*) to improve the performances behind all `deleteItems()` calls. Currently only supported in some backends, but it may evolve in the future.
1 parent 5f3cae8 commit 6b60a53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+347
-151
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local.properties
1818
.idea
1919
.gitignore
2020
.idea/
21+
.run/
2122

2223
# External tool builders
2324
.externalToolBuilders/

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Configuration methods`ConfigurationOption::isPreventCacheSlams()`, `ConfigurationOption::setPreventCacheSlams()`, `ConfigurationOption::getCacheSlamsTimeout()`, `ConfigurationOption::setCacheSlamsTimeout()` are deprecated. ([See changes](CHANGELOG_API.md)).
1313
- Fixed #907 // Internal "driver decode()" method will now throw an if the string data looks corrupted.
1414
- Internal: Implemented multiple keys fetch (*if supported by the backend*) to improve the performances behind all `getItems()` calls. Currently only supported in some backends, but it may evolve in the future.
15+
- Internal: Implemented multiple keys delete (*if supported by the backend*) to improve the performances behind all `deleteItems()` calls. Currently only supported in some backends, but it may evolve in the future.
1516
- __Misc__
1617
- Fixed multiple code typo & updated README.md
1718

docs/EVENTS.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ The order of execution of the events is always the following:
8989
- *ExtendedCacheItemPoolInterface::getItems()*
9090
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
9191
- *ExtendedCacheItemPoolInterface::getItemsAsJsonString()*
92+
9293
- onCacheGetItems(*Callable* **$callback**)
9394
- **Callback arguments**
9495
- *ExtendedCacheItemPoolInterface* **$itemPool**
@@ -102,16 +103,34 @@ The order of execution of the events is always the following:
102103
- *ExtendedCacheItemPoolInterface::getItems()*
103104
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
104105
- *ExtendedCacheItemPoolInterface::getItemsAsJsonString()*
106+
105107
- onCacheDeleteItem(*Callable* **$callback**)
106108
- **Callback arguments**
107109
- *ExtendedCacheItemPoolInterface* **$itemPool**
108110
- *ExtendedCacheItemInterface* **$item**
109111
- **Scope**
110112
- ItemPool
111113
- **Description**
112-
- Allow you to manipulate an item after being deleted. :exclamation: **Caution** The provided item is in pool detached-state.
114+
- Allow you to manipulate an item after being deleted (this event is not fired if `deleteItems()` is called). :exclamation: **Caution** The provided item is in pool detached-state.
115+
- **Risky Circular Methods**
116+
- *ExtendedCacheItemPoolInterface::deleteItem()*
117+
- *ExtendedCacheItemPoolInterface::deleteItems()*
118+
- *ExtendedCacheItemPoolInterface::getItem()*
119+
- *ExtendedCacheItemPoolInterface::getItems()*
120+
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
121+
- *ExtendedCacheItemPoolInterface::getItemsAsJsonString()*
122+
123+
- onCacheDeleteItems(*Callable* **$callback**)
124+
- **Callback arguments**
125+
- *ExtendedCacheItemPoolInterface* **$itemPool**
126+
- *ExtendedCacheItemInterface[]* **$items**
127+
- **Scope**
128+
- ItemPool
129+
- **Description**
130+
- Allow you to manipulate multiple items after being deleted. :exclamation: **Caution** The provided item is in pool detached-state.
113131
- **Risky Circular Methods**
114132
- *ExtendedCacheItemPoolInterface::deleteItem()*
133+
- *ExtendedCacheItemPoolInterface::deleteItems()*
115134
- *ExtendedCacheItemPoolInterface::getItem()*
116135
- *ExtendedCacheItemPoolInterface::getItems()*
117136
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
@@ -203,6 +222,7 @@ The order of execution of the events is always the following:
203222
- *ExtendedCacheItemPoolInterface::getItems()*
204223
- *ExtendedCacheItemPoolInterface::getItemsByTag()*
205224
- *ExtendedCacheItemPoolInterface::getItemsAsJsonString()*
225+
206226
- onCacheDriverChecked(*Callable* **$callback**)
207227
- **Callback arguments**
208228
- *ExtendedCacheItemPoolInterface* **$itemPool**

lib/Phpfastcache/Cluster/ClusterPoolTrait.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
5050
}
5151

5252
/**
53-
* @param ExtendedCacheItemInterface $item
53+
* @param string $key
54+
* @param string $encodedKey
5455
* @return bool
5556
*/
56-
protected function driverDelete(ExtendedCacheItemInterface $item): bool
57+
protected function driverDelete(string $key, string $encodedKey): bool
5758
{
5859
return true;
5960
}

lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php

+51-10
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public function getItems(array $keys = []): iterable
163163
} else {
164164
$item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl()));
165165
}
166+
$item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss();
166167
}
167168
}
168169
} else {
@@ -316,15 +317,39 @@ public function clear(): bool
316317
*/
317318
public function deleteItems(array $keys): bool
318319
{
319-
$return = true;
320-
foreach ($keys as $key) {
321-
$result = $this->deleteItem($key);
322-
if ($result !== true) {
323-
$return = false;
320+
if (count($keys) > 1) {
321+
$return = true;
322+
try {
323+
$items = $this->getItems($keys);
324+
$return = $this->driverDeleteMultiple($keys);
325+
foreach ($items as $item) {
326+
$item->setHit(false);
327+
328+
if (!\str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) {
329+
$this->cleanItemTags($item);
330+
}
331+
}
332+
$this->getIO()->incWriteHit();
333+
$this->eventManager->dispatch(Event::CACHE_DELETE_ITEMS, $this, $items);
334+
$this->deregisterItems($keys);
335+
} catch (PhpfastcacheUnsupportedMethodException) {
336+
foreach ($keys as $key) {
337+
$result = $this->deleteItem($key);
338+
if ($result !== true) {
339+
$return = false;
340+
}
341+
}
324342
}
343+
344+
return $return;
345+
}
346+
347+
$index = array_key_first($keys);
348+
if ($index !== null) {
349+
return $this->deleteItem($keys[$index]);
325350
}
326351

327-
return $return;
352+
return false;
328353
}
329354

330355
/**
@@ -338,7 +363,8 @@ public function deleteItems(array $keys): bool
338363
public function deleteItem(string $key): bool
339364
{
340365
$item = $this->getItem($key);
341-
if ($item->isHit() && $this->driverDelete($item)) {
366+
if ($item->isHit()) {
367+
$result = $this->driverDelete($item->getKey(), $item->getEncodedKey());
342368
$item->setHit(false);
343369
$this->getIO()->incWriteHit();
344370

@@ -357,7 +383,7 @@ public function deleteItem(string $key): bool
357383
$this->cleanItemTags($item);
358384
}
359385

360-
return true;
386+
return $result;
361387
}
362388

363389
return false;
@@ -397,8 +423,8 @@ public function commit(): bool
397423
$return = true;
398424
foreach ($this->deferredList as $key => $item) {
399425
$result = $this->save($item);
426+
unset($this->deferredList[$key]);
400427
if ($result !== true) {
401-
unset($this->deferredList[$key]);
402428
$return = $result;
403429
}
404430
}
@@ -494,6 +520,21 @@ protected function deregisterItem(string $itemKey): static
494520
return $this;
495521
}
496522

523+
/**
524+
* @param string[] $itemKeys
525+
* @internal This method de-register multiple items from $this->itemInstances
526+
*/
527+
protected function deregisterItems(array $itemKeys): static
528+
{
529+
$this->itemInstances = array_diff_key($this->itemInstances, array_flip($itemKeys));
530+
531+
if (\gc_enabled()) {
532+
\gc_collect_cycles();
533+
}
534+
535+
return $this;
536+
}
537+
497538
/**
498539
* @throws PhpfastcacheLogicException
499540
*/
@@ -547,7 +588,7 @@ protected function handleExpiredCacheItem(ExtendedCacheItemInterface $item): voi
547588
* As we MUST return an item in any
548589
* way, we do not de-register here
549590
*/
550-
$this->driverDelete($item);
591+
$this->driverDelete($item->getKey(), $item->getEncodedKey());
551592

552593
/**
553594
* Reset the Item

lib/Phpfastcache/Core/Pool/DriverBaseTrait.php

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface
8989
throw new PhpfastcacheDriverConnectException(
9090
sprintf(
9191
ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE,
92-
$e::class,
9392
$this->getDriverName(),
9493
$e->getMessage(),
9594
$e->getLine() ?: 'unknown line',

lib/Phpfastcache/Core/Pool/DriverPoolAbstractTrait.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,36 @@ protected function driverReadAllKeys(string $pattern = ''): iterable
7272
abstract protected function driverWrite(ExtendedCacheItemInterface $item): bool;
7373

7474
/**
75-
* @param ExtendedCacheItemInterface $item
75+
* @param ExtendedCacheItemInterface ...$item
76+
* @return bool
77+
* @throws PhpfastcacheUnsupportedMethodException
78+
*/
79+
protected function driverWriteMultiple(ExtendedCacheItemInterface ...$item): bool
80+
{
81+
/**
82+
* @todo Implement bulk writes to be for v10:
83+
* For methods commit() and saveMultiple()
84+
*/
85+
throw new PhpfastcacheUnsupportedMethodException();
86+
}
87+
88+
89+
/**
90+
* @param string $key
91+
* @param string $encodedKey
7692
* @return bool
7793
*/
78-
abstract protected function driverDelete(ExtendedCacheItemInterface $item): bool;
94+
abstract protected function driverDelete(string $key, string $encodedKey): bool;
95+
96+
/**
97+
* @param string[] $keys
98+
* @return bool
99+
* @throws PhpfastcacheUnsupportedMethodException
100+
*/
101+
protected function driverDeleteMultiple(array $keys): bool
102+
{
103+
throw new PhpfastcacheUnsupportedMethodException();
104+
}
79105

80106
/**
81107
* @return bool

lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface ExtendedCacheItemPoolInterface extends CacheItemPoolInterface, EventMa
3939
public const DRIVER_CHECK_FAILURE = '%s is not installed or is misconfigured, cannot continue.
4040
Also, please verify the suggested dependencies in composer because as of the V6, 3rd party libraries are no longer required.';
4141

42-
public const DRIVER_CONNECT_FAILURE = '(%s) %s failed to connect with the following error message: "%s" line %d in %s';
42+
public const DRIVER_CONNECT_FAILURE = '%s failed to connect with the following error message: "%s" line %d in %s';
4343

4444
public const DRIVER_KEY_WRAPPER_INDEX = 'k';
4545

lib/Phpfastcache/Core/Pool/TaggableCacheItemPoolTrait.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ protected function driverUnwrapTags(array $wrapper): array
368368
*/
369369
protected function cleanItemTags(ExtendedCacheItemInterface $item): void
370370
{
371-
$this->driverWriteTags($item->removeTags($item->getTags()));
371+
if (!empty($item->getTags()) || !empty($item->getRemovedTags())) {
372+
$this->driverWriteTags($item->removeTags($item->getTags()));
373+
}
372374
}
373375

374376
/**

lib/Phpfastcache/Drivers/Apcu/Driver.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,13 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
9999
}
100100

101101
/**
102-
* @param ExtendedCacheItemInterface $item
102+
* @param string $key
103+
* @param string $encodedKey
103104
* @return bool
104-
* @throws PhpfastcacheInvalidArgumentException
105105
*/
106-
protected function driverDelete(ExtendedCacheItemInterface $item): bool
106+
protected function driverDelete(string $key, string $encodedKey): bool
107107
{
108-
109-
return (bool)apcu_delete($item->getKey());
108+
return (bool)apcu_delete($key);
110109
}
111110

112111
/**

lib/Phpfastcache/Drivers/Arangodb/Driver.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,18 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
170170
}
171171

172172
/**
173-
* @param ExtendedCacheItemInterface $item
173+
* @param string $key
174+
* @param string $encodedKey
174175
* @return bool
175176
*/
176-
protected function driverDelete(ExtendedCacheItemInterface $item): bool
177+
protected function driverDelete(string $key, string $encodedKey): bool
177178
{
178179
$options = [
179180
'returnOld' => false
180181
];
181182

182183
try {
183-
$this->documentHandler->removeById($this->getConfig()->getCollection(), $item->getEncodedKey(), null, $options);
184+
$this->documentHandler->removeById($this->getConfig()->getCollection(), $encodedKey, null, $options);
184185
return true;
185186
} catch (ArangoException) {
186187
return false;

lib/Phpfastcache/Drivers/Cassandra/Driver.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,18 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
204204
}
205205

206206
/**
207-
* @param ExtendedCacheItemInterface $item
207+
* @param string $key
208+
* @param string $encodedKey
208209
* @return bool
209-
* @throws PhpfastcacheInvalidArgumentException
210210
*/
211-
protected function driverDelete(ExtendedCacheItemInterface $item): bool
211+
protected function driverDelete(string $key, string $encodedKey): bool
212212
{
213213

214214
try {
215215
$options = $this->getCompatibleExecutionOptionsArgument(
216216
[
217217
'arguments' => [
218-
'cache_id' => $item->getKey(),
218+
'cache_id' => $key,
219219
],
220220
]
221221
);

lib/Phpfastcache/Drivers/Couchbasev3/Driver.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
127127
}
128128

129129
/**
130-
* @param ExtendedCacheItemInterface $item
130+
* @param string $key
131+
* @param string $encodedKey
131132
* @return bool
132-
* @throws PhpfastcacheInvalidArgumentException
133133
*/
134-
protected function driverDelete(ExtendedCacheItemInterface $item): bool
134+
protected function driverDelete(string $key, string $encodedKey): bool
135135
{
136136

137137
try {
138-
$this->getCollection()->remove($item->getEncodedKey());
138+
$this->getCollection()->remove($encodedKey);
139139
return true;
140140
} catch (DocumentNotFoundException) {
141141
return true;

lib/Phpfastcache/Drivers/Couchdb/Driver.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ protected function createDatabase(): void
131131

132132
protected function getCouchDbItemKey(ExtendedCacheItemInterface $item): string
133133
{
134-
return 'pfc_' . $item->getEncodedKey();
134+
return $this->getCouchDbKey($item->getEncodedKey());
135+
}
136+
137+
protected function getCouchDbKey(string $encodedKey): string
138+
{
139+
return 'pfc_' . $encodedKey;
135140
}
136141

137142
/**
@@ -203,16 +208,16 @@ protected function getLatestDocumentRevision(string $docId): ?string
203208
}
204209

205210
/**
206-
* @param ExtendedCacheItemInterface $item
211+
* @param string $key
212+
* @param string $encodedKey
207213
* @return bool
208214
* @throws PhpfastcacheDriverException
209-
* @throws PhpfastcacheInvalidArgumentException
210215
*/
211-
protected function driverDelete(ExtendedCacheItemInterface $item): bool
216+
protected function driverDelete(string $key, string $encodedKey): bool
212217
{
213218

214219
try {
215-
$this->instance->deleteDocument($this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)));
220+
$this->instance->deleteDocument($this->getCouchDbKey($encodedKey), $this->getLatestDocumentRevision($this->getCouchDbKey($encodedKey)));
216221
} catch (CouchDBException $e) {
217222
throw new PhpfastcacheDriverException('Got error while trying to delete a document: ' . $e->getMessage(), 0, $e);
218223
}

lib/Phpfastcache/Drivers/Devnull/Driver.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,12 @@ protected function driverRead(CacheItemInterface $item): ?array
7474
}
7575

7676
/**
77-
* @param ExtendedCacheItemInterface $item
77+
* @param string $key
78+
* @param string $encodedKey
7879
* @return bool
79-
* @throws PhpfastcacheInvalidArgumentException
8080
*/
81-
protected function driverDelete(ExtendedCacheItemInterface $item): bool
81+
protected function driverDelete(string $key, string $encodedKey): bool
8282
{
83-
8483
return true;
8584
}
8685

0 commit comments

Comments
 (0)