HTTP caching with API Platform
What is API Platform
API Platform is a next-generation web framework designed to easily create API-first projects without compromising extensibility and flexibility.
Minimalistic setup
Caddyfile
By default API Platform uses FrankenPHP (that is built on top of the Caddy webserver) as reverse-proxy. So we can edit the provided Caddyfile api/frankenphp/Caddyfile
and configure our caddy instance with these minimal changes:
Dockerfile
You now have to update the Dockerfile api/Dockerfile
to build the FrankenPHP/Caddy instance with Souin (or the cache-handler):
And voilà, your API Platform project has now an HTTP cache in front of your application. But you would probably enable the automatic invalidation to be sure your responses are always up to date, especially to refresh the list of your entity when you create a new item or update one that is in this list.
To do that, you have to update the api/config/packages/api_platform.yml
file to enable the HTP cache invalidation:
You’re now ready to handle tons of requests that will be served by the HTTP cache.