diff --git a/src/HttpCache/BufferedPurger.php b/src/HttpCache/BufferedPurger.php new file mode 100644 index 00000000000..54a1385a25a --- /dev/null +++ b/src/HttpCache/BufferedPurger.php @@ -0,0 +1,62 @@ + + * + * 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 */ + 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); + } +} diff --git a/src/Laravel/Eloquent/ApiPlatformEventProvider.php b/src/Laravel/Eloquent/ApiPlatformEventProvider.php index 5c1bf10b06e..83023ad770e 100644 --- a/src/Laravel/Eloquent/ApiPlatformEventProvider.php +++ b/src/Laravel/Eloquent/ApiPlatformEventProvider.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Laravel\Eloquent; +use ApiPlatform\HttpCache\BufferedPurger; use ApiPlatform\HttpCache\PurgerInterface; use ApiPlatform\HttpCache\SouinPurger; use ApiPlatform\HttpCache\VarnishPurger; @@ -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; @@ -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 @@ -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) { @@ -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(); }); } diff --git a/src/Symfony/Bundle/Resources/config/state/http_cache_purger.php b/src/Symfony/Bundle/Resources/config/state/http_cache_purger.php index 58c93cc959c..2105f01d934 100644 --- a/src/Symfony/Bundle/Resources/config/state/http_cache_purger.php +++ b/src/Symfony/Bundle/Resources/config/state/http_cache_purger.php @@ -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(); @@ -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]); }; diff --git a/src/Symfony/EventListener/HttpCachePurgeBufferListener.php b/src/Symfony/EventListener/HttpCachePurgeBufferListener.php new file mode 100644 index 00000000000..616657f4e99 --- /dev/null +++ b/src/Symfony/EventListener/HttpCachePurgeBufferListener.php @@ -0,0 +1,39 @@ + + * + * 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(); + } + } +} diff --git a/tests/Fixtures/NullPurger.php b/tests/Fixtures/NullPurger.php index 919a9351a74..40a58a28099 100644 --- a/tests/Fixtures/NullPurger.php +++ b/tests/Fixtures/NullPurger.php @@ -21,10 +21,12 @@ 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 @@ -32,9 +34,15 @@ 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 diff --git a/tests/Fixtures/app/config/config_common.yml b/tests/Fixtures/app/config/config_common.yml index 848e9d30bc4..fc1636d8748 100644 --- a/tests/Fixtures/app/config/config_common.yml +++ b/tests/Fixtures/app/config/config_common.yml @@ -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: diff --git a/tests/Functional/HttpCache/PurgeTagProviderTest.php b/tests/Functional/HttpCache/PurgeTagProviderTest.php index e8bfb685cb3..09e05e23b59 100644 --- a/tests/Functional/HttpCache/PurgeTagProviderTest.php +++ b/tests/Functional/HttpCache/PurgeTagProviderTest.php @@ -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()) {