Skip to main content
The Rest Generic Class package implements a store-agnostic cache strategy using Laravel’s native cache system. This allows you to use Redis, database, file, Memcached, or any other Laravel-supported cache driver without code changes.

How Cache Works

Cache is applied in BaseService for read operations:
  • list_all - Caches paginated list responses
  • get_one - Caches individual resource responses
Write operations (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

The REST_CACHE_STORE variable accepts any Laravel cache driver:
The array store only persists cache for the current request lifecycle. Don’t use it in production.

3. Configure Store in Laravel

Ensure your selected cache store is properly configured in config/cache.php:
For database caching, run the migration:

TTL Configuration

Default TTL

Set a baseline TTL for all cached responses:

Method-Specific TTLs

Override TTL for specific operations:
Configuration structure:

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

This bypasses the cache entirely and always queries fresh data from the database.

Override TTL for One Request

This caches the response for 120 seconds, overriding the configured TTL.

Combined Example

Cache this specific query shape for 5 minutes.
Per-request cache control is useful for debugging and special cases, but shouldn’t be relied upon for normal operation. Configure appropriate default TTLs instead.

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:
How it works:
  • Request with Accept-Language: en gets a different cache key than Accept-Language: es
  • Request with X-Tenant-Id: tenant-a gets a different cache key than X-Tenant-Id: tenant-b

Customizing Vary Headers

If your application uses different headers for tenant identification, update the configuration:
Security critical: If your application is multi-tenant, ensure your tenant identification header is listed in cache.vary.headers. Otherwise, users may see cached data from other tenants.

Cache Invalidation

Automatic Version-Based Invalidation

The package uses a model-level cache versioning strategy:
  1. Each model has a version key stored in cache (e.g., cache_version:products)
  2. Every cache key includes the current model version
  3. On write operations (create, update, destroy), the version is bumped
  4. Existing cache keys become stale automatically without explicit deletion
Example flow:

Manual Cache Clearing

Clear all application cache:
Clear cache for a specific store:
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 (like file, 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)

Best for: APIs with heavy read traffic and moderate write traffic

Simple Staging (File)

Best for: Staging environments without Redis infrastructure

Distributed Applications (Database)

Best for: Multi-server setups sharing a database but without Redis

Development (Disabled)

Best for: Local development where you always want fresh data

Monitoring Cache Performance

Track these metrics to optimize your cache configuration:

Troubleshooting

Cache Not Working

Check that REST_CACHE_ENABLED=true in .env and configuration is not cached with old values.
Ensure the store specified in REST_CACHE_STORE exists in config/cache.php and is properly configured.For Redis, verify connection:
If using file cache, ensure storage/framework/cache is writable:

Cache Serving Stale Data

If cache persists after updates, check:
  1. Version bump logic - Ensure write operations are using BaseService methods
  2. Custom write logic - If you bypass BaseService, manually bump the cache version
  3. TTL too long - Reduce TTL if data staleness is unacceptable

Environment Variables

Complete reference of cache-related environment variables

Validation Cache

Configure caching for validation queries