How Cache Works
Cache is applied inBaseService for read operations:
list_all- Caches paginated list responsesget_one- Caches individual resource responses
create, update, destroy, destroybyid) automatically invalidate the cache by bumping a model-level cache version.
Cache Key Fingerprint
The cache key is built from multiple factors to ensure correct cache isolation:
This prevents cache pollution where one query’s results might incorrectly serve another query.
Enabling Cache
1. Configure Environment
Set these variables in your.env file:
Cache is disabled by default (
REST_CACHE_ENABLED=false). You must explicitly enable it.2. Select Cache Store
TheREST_CACHE_STORE variable accepts any Laravel cache driver:
3. Configure Store in Laravel
Ensure your selected cache store is properly configured inconfig/cache.php:
TTL Configuration
Default TTL
Set a baseline TTL for all cached responses:Method-Specific TTLs
Override TTL for specific operations:TTL Strategy Recommendations
List endpoints (
list_all) often have shorter TTLs than single-item reads (get_one) because lists change more frequently when new items are added.Per-Request Cache Control
Clients can control caching behavior on individual requests using query parameters.Disable Cache for One Request
Override TTL for One Request
Combined Example
Multi-Tenant and Locale Awareness
Vary by Headers
By default, cache keys vary by specific request headers to prevent cross-tenant or cross-locale data leaks:- Request with
Accept-Language: engets a different cache key thanAccept-Language: es - Request with
X-Tenant-Id: tenant-agets a different cache key thanX-Tenant-Id: tenant-b
Customizing Vary Headers
If your application uses different headers for tenant identification, update the configuration:Cache Invalidation
Automatic Version-Based Invalidation
The package uses a model-level cache versioning strategy:- Each model has a version key stored in cache (e.g.,
cache_version:products) - Every cache key includes the current model version
- On write operations (
create,update,destroy), the version is bumped - Existing cache keys become stale automatically without explicit deletion
Manual Cache Clearing
Clear all application cache:You rarely need to manually clear cache because version-based invalidation handles writes automatically.
Cache Without Tags Support
The package’s version-based invalidation strategy works with all cache stores, including those that don’t support tags (likefile, database, memcached).
This is more flexible than tag-based invalidation, which only works with Redis and a few other stores.
Example Configurations
High-Traffic Production (Redis)
Simple Staging (File)
Distributed Applications (Database)
Development (Disabled)
Monitoring Cache Performance
Track these metrics to optimize your cache configuration:Troubleshooting
Cache Not Working
Verify cache is enabled
Verify cache is enabled
Check that
REST_CACHE_ENABLED=true in .env and configuration is not cached with old values.Check cache store configuration
Check cache store configuration
Ensure the store specified in
REST_CACHE_STORE exists in config/cache.php and is properly configured.For Redis, verify connection:Verify cache permissions (file store)
Verify cache permissions (file store)
If using file cache, ensure
storage/framework/cache is writable:Cache Serving Stale Data
If cache persists after updates, check:- Version bump logic - Ensure write operations are using
BaseServicemethods - Custom write logic - If you bypass
BaseService, manually bump the cache version - TTL too long - Reduce TTL if data staleness is unacceptable
Related Topics
Environment Variables
Complete reference of cache-related environment variables
Validation Cache
Configure caching for validation queries