Skip to content

Commit 260b6a4

Browse files
authored
Expand the example showing how to inject PurgeClientInterface (#3287)
1 parent 9d92854 commit 260b6a4

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

docs/infrastructure_and_maintenance/cache/http_cache/content_aware_cache.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,38 @@ In other words, HTTP Cache for `[Parent1]`, children of `[Parent1]` ( if any ),
326326
### Custom purging from code
327327

328328
While the system purges tags whenever API is used to change data, you may need to purge directly from code.
329-
For that you can use the built-in purge client:
329+
For that you can inject the built-in [`PurgeClientInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-HttpCache-PurgeClient-PurgeClientInterface.html) by using the `ibexa.http_cache.purge_client` service name:
330330

331-
``` php
331+
``` php hl_lines="12-13 19-21 23-25"
332332
use Ibexa\Contracts\HttpCache\Handler\ContentTagInterface;
333-
334-
/** @var \Ibexa\Contracts\HttpCache\PurgeClient\PurgeClientInterface $purgeClient */
335-
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location $location */
336-
337-
// Example for purging by Location ID:
338-
$purgeClient->purge([ContentTagInterface::LOCATION_PREFIX . $location->id]);
339-
340-
// Example for purging all cache for instance for full re-deploy cases, usually this triggers an expiry (soft purge):
341-
$purgeClient->purgeAll();
333+
use Ibexa\Contracts\HttpCache\PurgeClient\PurgeClientInterface;
334+
use Symfony\Component\Console\Attribute\AsCommand;
335+
use Symfony\Component\Console\Command\Command;
336+
use Symfony\Component\Console\Style\SymfonyStyle;
337+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
338+
339+
#[AsCommand(name: 'app:purge-cache')]
340+
class MyCustomCacheCommand
341+
{
342+
public function __construct(
343+
#[Autowire(service: 'ibexa.http_cache.purge_client')]
344+
private readonly PurgeClientInterface $purgeClient
345+
) {
346+
}
347+
348+
public function __invoke(SymfonyStyle $io): int
349+
{
350+
// Example for purging by Location ID:
351+
$locationId = 2;
352+
$this->purgeClient->purge([ContentTagInterface::LOCATION_PREFIX . $locationId]);
353+
354+
// Example for purging all cache for instance for full re-deploy cases
355+
// Usually this triggers an expiry (soft purge):
356+
$this->purgeClient->purgeAll();
357+
358+
return Command::SUCCESS;
359+
}
360+
}
342361
```
343362

344363
### Purging from command line

0 commit comments

Comments
 (0)