berthott / laravel-api-cache
Laravel Helper for Caching API Responses
Installs: 1 045
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
Laravel-API-Cache
A helper for caching complete API Responses. Easily cache the complete response of you Laravel API.
Installation
$ composer require berthott/laravel-api-cache
Usage
- The package automatically caches all responses to GET requests when installed and enabled.
- The package assumes, that your routes are named according to Laravels Route::apiResource helper (
tablename.method
). - Responses are grouped via the table name (more specifically the first part of the route name, so custom route names are supported too).
- The package assumes, that your routes are named according to Laravels Route::apiResource helper (
- To automatically flush the cache corresponding to your model add the
FlushesApiCache
Trait to your model.- This will flush the cache for any model creation, update or deletion.
- To also flush dependent models override the Traits
cacheDependencies
method and return a list of related route names.
- A
FlushApiCache
event listener is available to be connected with custom model events.
Options
To change the default options use
$ php artisan vendor:publish --provider="berthott\ApiCache\ApiCacheServiceProvider" --tag="config"
enabled
: Enable the API caching. Default toenv('CACHE_API', false)
.ignoreRoutes
: An array of route names to be ignored from the cache. Defaults to[]
.lifetime
: The lifetime of the cache in days. Defaults to14
.key
: A key to add to each redis cache to be able to use a single redis server and avoid name clashes. Defaults toenv('CACHE_API_KEY', env('APP_NAME', 'laravel'))
.
Architecture
- The actual caching is done in
ApiCacheService
- A store-key is used to connect requests / cached responses to a specific model / entity
- An
ApiCacheMiddleware
intercepts all http requests and callsApiCacheService
- The
FlushesApiCache
trait observes creating / updating / deleting models and will clear the cache for the corresponding store
Compatibility
Tested with Laravel 10.x.
License
See License File. Copyright © 2023 Jan Bladt.