Cache Prefix Configuration Analyzer
| Analyzer ID | Category | Severity | Time To Fix |
|---|---|---|---|
cache-prefix-configuration | ✅ Reliability | High | 5 minutes |
What This Checks
- Detects when shared cache drivers (
redis,memcached,dynamodb,database) are in use without a configured prefix - Verifies both the global
cache.prefixand store-specific prefixes to ensure they’re non-empty - Flags prefixes that are too generic (e.g.,
laravel_cache,app,cache) or identical to other tenants - Provides location hints (line numbers in
config/cache.php) for quick remediation
Why It Matters
- Multi-tenant collisions: On shared Redis/Memcached clusters, generic prefixes cause one project to overwrite another’s entries
- Production stability: Cache collisions lead to inconsistent sessions, stale feature flags, and corrupted cached data
- Best practices: Laravel recommends slugging
APP_NAMEand appending_cache; forgetting this leaves you exposed when deploying to shared environments (e.g., Vapor, multi-app Redis)
How to Fix
Quick Fix (1 minute)
- Edit
config/cache.php:
php
'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'),- For store-specific needs (e.g.,
redisonly):
php
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
'prefix' => env('CACHE_PREFIX', 'myapp_cache'),
],- Set
CACHE_PREFIX=myapp_production_cachein.env.
Proper Fix (5 minutes)
- Slug your
APP_NAME: EnsureAPP_NAMEis unique per environment (DEV vs PROD) to make the default slug unique - Override per store: For multi-tenant Redis clusters, set
stores.redis.prefixto a tenant-specific slug - Automate: Add an environment-specific fallback so ephemeral environments (review apps) auto-generate unique prefixes (
Str::uuid()) on boot - Test: Write integration tests ensuring different
.envconfigurations yield different cache keys (e.g., store and retrieve a known key) - Monitor: Consider namespacing keys (e.g.,
myapp:prod:) for easier debugging in Redis CLI
References
Related Analyzers
- Cache Status Analyzer - Validates cache connectivity and functionality
- Queue Timeout Configuration Analyzer - Ensures queue timeout and retry_after values are properly configured
- Composer Validation Analyzer - Ensures composer.json is valid and follows best practices