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:| Factor | Purpose |
|---|---|
| Model class | Separate cache per resource type |
| Operation | Different keys for list_all vs get_one |
| Route/path | Different endpoints get different cache |
| Query parameters | Different filters/selects/relations get different cache |
| Authenticated user | User-specific data gets user-specific cache |
| Request headers | Multi-tenant and locale-aware caching |
| Model cache version | Automatic invalidation on writes |
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
| Data Type | Recommended TTL | Reason |
|---|---|---|
| Frequently updated | 30-60 seconds | Balance freshness and performance |
| Moderately updated | 5-15 minutes | Reduce database load significantly |
| Rarely updated | 30-60 minutes | Maximize cache hit rate |
| Static/reference data | 1-24 hours | Near-permanent caching |
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:| Metric | Target | Action if Below Target |
|---|---|---|
| Cache hit rate | > 80% | Increase TTL |
| Average response time | < 100ms | Enable caching or increase TTL |
| Cache memory usage | < 80% capacity | Reduce TTL or add more cache capacity |
| Database query count | Significant reduction | Cache is working well |
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