Cache Prefix Configuration Analyzer
| Analyzer ID | Category | Severity | Time To Fix |
|---|---|---|---|
cache-prefix-configuration | ✅ Reliability | Medium | 5 minutes |
What This Checks
- Detects when the default cache store uses a shared driver (
redis,memcached,dynamodb,database) without a configured prefix - Resolves the prefix the way Laravel does: a store's own
prefixwins whenever it is set, even to an empty string, and otherwise the globalcache.prefixapplies - Counts the prefix the cache client adds to every key: the Redis connection prefix (
REDIS_PREFIX, a connection's ownprefixoroptions, or a?prefix=in its URL) and the MemcachedOPT_PREFIX_KEYoption - Flags key namespaces that are too generic (e.g.,
laravel_cache,laravel-database-,app,cache); one unique part, such as a unique Redis prefix, is enough to pass - When the default store uses the
failoverdriver, checks each shared store it lists, and reports each offending store by name - Grades a
databasestore Low, because its keys only collide when two applications share one database connection and cache table, and aredis,memcachedordynamodbstore Medium, because several applications commonly share one cache server - 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'),This only clears the finding when APP_NAME is set to something unique: with the default Laravel the prefix is laravel_cache, which is generic.
- 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.On Redis, a unique connection prefix is enough on its own, because the client puts it in front of every key. Set
REDIS_PREFIX=myapp_production_in.env, or set a uniqueAPP_NAME, which the defaultREDIS_PREFIXis built from.
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
ShieldCI Configuration
This analyzer is automatically skipped in CI environments ($runInCI = false).
Why skip in CI?
- CI environments do not run a shared cache server (Redis/Memcached/DynamoDB), so prefix collisions are not possible
- Prevents irrelevant failures in pipelines where no cache infrastructure is present
When to run this analyzer:
- ✅ Local development: Catches misconfigured prefixes before they reach a shared environment
- ✅ Staging/Production servers: Validates that the deployed prefix is unique across applications sharing the cache cluster
- ❌ CI/CD pipelines: Skipped automatically (no shared cache server runs in CI)
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