-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Description
Laravel Version
12.35.0
PHP Version
PHP 8.4.5 (cli) (built: Mar 12 2025 12:17:53) (NTS Visual C++ 2022 x64)
Database Driver & Version
No response
Description
When using Redis as the cache driver, Laravel normally prepends the configured cache prefixes (from CACHE_PREFIX or config/cache.php) to all keys stored in Redis.
However, when running parallel tests (using Laravel’s built-in parallel testing feature), the same cache operations do not include these prefixes if process "token" is appended with the user cache key (i.e. appending "token" to "spatie/laravel-permission" cache key so that each process has its own key), resulting in keys being stored without the expected prefix.
This creates a conflict or data leakage risk — particularly in environments where multiple Laravel applications share a Redis instance and run test suites concurrently.
Normal runtime behavior:
Cache::put('example_key', 'foo');
Redis key stored:
myapp-database-my-app-cache-user-key
Parallel test behavior:
When the same code executes in a parallel test process (spawned via php artisan test --parallel):
Cache::put('example_key', 'foo');
🚫 Redis key stored:
key
— missing the expected Laravel cache prefix (myapp-database-my-app-cache-).
Prefixes are essential to namespace cache keys between environments or apps.
In CI environments (e.g., GitHub Actions, shared Redis server), multiple parallel test runners or multiple Laravel apps may use the same Redis instance, causing:
Cache key collisions
Test flakiness
Cross-application data leakage
🔍 Environment details
Cache driver: redis
Testing mode: Laravel Parallel Testing (--parallel)
Redis client: PhpRedis
OS : Windows 11 Pro
Redis 5.0.14.1 - 64 bit
✅ Expected behavior
Even in parallel testing, Laravel should apply the same Redis cache key prefixing logic as it does during normal runtime.
That means, regardless of whether the cache is accessed in the main process or a parallel test process, the Redis key should consistently include:
{myapp-database}-{my-app-cache}-user-key
🧠 Possible cause
It appears that the parallel testing process bootstraps a minimal application context, and the cache store may not inherit the configured prefix from config/cache.php or .env.
This causes RedisStore::buildKey() to skip or use an empty prefix.
💡 Suggested fix
Ensure that each parallel test process:
Loads and applies the same cache prefix configuration (config/cache.php), and
Initializes Redis cache stores with the correct prefix value from config.
This would maintain consistent cache behavior between normal runtime and parallel testing.
Steps To Reproduce
Use Redis as the cache driver (CACHE_DRIVER=redis).
Add to a test case:
test('redis prefix test', function () {
Cache::put('example_key', 'foo');
});
Run:
php artisan test
→ Redis key includes prefix ✅
Run:
php artisan test --parallel
→ Redis key missing prefix