Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/HttpCache/BufferedPurger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\HttpCache;

final class BufferedPurger implements PurgerInterface
{
private bool $buffering = false;

/** @var array<string, string> */
private array $tags = [];

public function __construct(private readonly PurgerInterface $decorated)
{
}

public function startBuffering(): void
{
$this->buffering = true;
}

public function purge(array $iris): void
{
if (!$this->buffering) {
$this->decorated->purge($iris);

return;
}

foreach ($iris as $iri) {
$this->tags[$iri] = $iri;
}
}

public function flush(): void
{
$this->buffering = false;

if (!$this->tags) {
return;
}

$tags = array_values($this->tags);
$this->tags = [];
$this->decorated->purge($tags);
}

public function getResponseHeaders(array $iris): array
{
return $this->decorated->getResponseHeaders($iris);
}
}
15 changes: 13 additions & 2 deletions src/Laravel/Eloquent/ApiPlatformEventProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Laravel\Eloquent;

use ApiPlatform\HttpCache\BufferedPurger;
use ApiPlatform\HttpCache\PurgerInterface;
use ApiPlatform\HttpCache\SouinPurger;
use ApiPlatform\HttpCache\VarnishPurger;
Expand All @@ -24,6 +25,7 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Symfony\Component\HttpClient\HttpClient;
Expand Down Expand Up @@ -74,7 +76,7 @@ public function register(): void
);
});

$this->app->singleton(PurgerInterface::class, static function (Application $app) {
$this->app->singleton(BufferedPurger::class, static function (Application $app) {
$purgerClass = Config::get(
'api-platform.http_cache.invalidation.purger',
SouinPurger::class
Expand All @@ -84,7 +86,11 @@ public function register(): void
throw new \InvalidArgumentException("Purger class '{$purgerClass}' configured in api-platform.php was not found.");
}

return $app->make($purgerClass);
return new BufferedPurger($app->make($purgerClass));
});

$this->app->singleton(PurgerInterface::class, static function (Application $app) {
return $app->make(BufferedPurger::class);
});

$this->app->singleton(PurgeHttpCacheListener::class, static function (Application $app) {
Expand All @@ -103,10 +109,15 @@ public function boot(): void
return;
}

Event::listen(RouteMatched::class, function (): void {
$this->app->make(BufferedPurger::class)->startBuffering();
});

Event::listen(RequestHandled::class, function (): void {
Event::forget('eloquent.saved: *');
Event::forget('eloquent.deleted: *');
$this->app->make(PurgeHttpCacheListener::class)->postFlush();
$this->app->make(BufferedPurger::class)->flush();
});
}

Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Bundle/Resources/config/state/http_cache_purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use ApiPlatform\HttpCache\BufferedPurger;
use ApiPlatform\HttpCache\State\AddTagsProcessor;
use ApiPlatform\Symfony\EventListener\HttpCachePurgeBufferListener;
use Symfony\Component\HttpKernel\KernelEvents;

return static function (ContainerConfigurator $container) {
$services = $container->services();
Expand All @@ -25,4 +28,14 @@
service('api_platform.iri_converter'),
service('api_platform.http_cache.purger')->nullOnInvalid(),
]);

$services->set('api_platform.http_cache.purger.buffered', BufferedPurger::class)
->decorate('api_platform.http_cache.purger')
->args([service('api_platform.http_cache.purger.buffered.inner')])
->tag('kernel.reset', ['method' => 'flush']);

$services->set('api_platform.http_cache.purger.buffer_listener', HttpCachePurgeBufferListener::class)
->args([service('api_platform.http_cache.purger.buffered')])
->tag('kernel.event_listener', ['event' => KernelEvents::REQUEST, 'method' => 'onKernelRequest', 'priority' => 2048])
->tag('kernel.event_listener', ['event' => KernelEvents::RESPONSE, 'method' => 'onKernelResponse', 'priority' => -1024]);
};
39 changes: 39 additions & 0 deletions src/Symfony/EventListener/HttpCachePurgeBufferListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Symfony\EventListener;

use ApiPlatform\HttpCache\BufferedPurger;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

final class HttpCachePurgeBufferListener
{
public function __construct(private readonly BufferedPurger $purger)
{
}

public function onKernelRequest(RequestEvent $event): void
{
if ($event->isMainRequest()) {
$this->purger->startBuffering();
}
}

public function onKernelResponse(ResponseEvent $event): void
{
if ($event->isMainRequest()) {
$this->purger->flush();
}
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/NullPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@
final class NullPurger implements PurgerInterface
{
private array $iris = [];
private int $purgeCount = 0;

public function purge(array $iris): void
{
$this->iris = $iris;
++$this->purgeCount;
}

public function getIris(): array
{
return $this->iris;
}

public function getPurgeCount(): int
{
return $this->purgeCount;
}

public function clear(): void
{
$this->iris = [];
$this->purgeCount = 0;
}

public function getResponseHeaders(array $iris): array
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/app/config/config_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ services:
class: ApiPlatform\Tests\Fixtures\NullPurger

test.api_platform.http_cache.purger:
alias: api_platform.http_cache.purger
alias: api_platform.http_cache.purger.buffered.inner
public: true

test.property_accessor:
Expand Down
30 changes: 30 additions & 0 deletions tests/Functional/HttpCache/PurgeTagProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ public function testSubresourceCollectionIsPurged(): void
$this->assertContains('/delivery_days/1/history', $this->purger()->getIris());
}

public function testSingleWriteTriggersSinglePurge(): void
{
$this->recreateSchema([Supplier::class, DeliveryDay::class]);

$manager = $this->getManager();
foreach (['First', 'Second'] as $name) {
$supplier = $this->isMongoDB() ? new SupplierDocument() : new Supplier();
$supplier->name = $name;
$manager->persist($supplier);
}
$manager->flush();

$client = self::createClient();
$client->request('POST', '/delivery_days', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['supplier' => '/suppliers/1', 'day' => 'monday'],
]);
$this->assertResponseStatusCodeSame(201);

$this->purger()->clear();
$client->request('PUT', '/delivery_days/1', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['supplier' => '/suppliers/2', 'day' => 'monday'],
]);
$this->assertResponseStatusCodeSame(200);
$this->assertSame(1, $this->purger()->getPurgeCount());
$this->assertContains('/suppliers/2', $this->purger()->getIris());
$this->assertContains('/suppliers/2/delivery_days', $this->purger()->getIris());
}

public function testMappedResourceUpdateReceivesEntityAndPreviousResource(): void
{
if ($this->isMongoDB()) {
Expand Down
Loading