Search code examples
amazon-cloudfrontapi-platform.comhttp-caching

HTTP cache invalidation with API Platform and AWS CloudFront


I am trying to implement a HTTP cache invalidation with API Platform and AWS CloudFront and as I can read in API Platform documentation:

Support for reverse proxies other than Varnish can easily be added by implementing the ApiPlatform\Core\HttpCache\PurgerInterface

I have coded an implementation but now I can not make the built-in cache invalidation system -should be the event listener ApiPlatform\Core\Bridge\Doctrine\EventListener\PurgeHttpCacheListener- it just keep injecting the ApiPlatform\Core\HttpCache\VarnishPurger instead.

What I did basically, in config/services.yml -having autowire enabled:

    ApiPlatform\Core\HttpCache\PurgerInterface: '@App\ApiPlatform\HttpCache\CloudFrontPurger'

    ApiPlatform\Core\Bridge\Doctrine\EventListener\PurgeHttpCacheListener:
        arguments:
            $purger: '@App\ApiPlatform\HttpCache\CloudFrontPurger'

Any thoughts?


Solution

  • Alright! Found the issue. PurgeHttpCacheListener is using a service ID so it cannot be autowired according to the Symfony docs.

    From vendor/api-platform/core/src/Bridge/Symfony/Bundle/Resources/config/doctrine_orm_http_cache_purger.xml:

            <service id="api_platform.doctrine.listener.http_cache.purge" class="ApiPlatform\Core\Bridge\Doctrine\EventListener\PurgeHttpCacheListener">
                <argument type="service" id="api_platform.http_cache.purger" />
                <argument type="service" id="api_platform.iri_converter" />
                <argument type="service" id="api_platform.resource_class_resolver" />
    
                <tag name="doctrine.event_listener" event="preUpdate" />
                <tag name="doctrine.event_listener" event="onFlush" />
                <tag name="doctrine.event_listener" event="postFlush" />
            </service>
    

    The solution is simple. In your service.yml just inject it using its service ID as follow:

        api_platform.http_cache.purger:
            class: App\ApiPlatform\HttpCache\CloudFrontPurger