Framework: `php artisan cache:clear` ignores the cache prefix

Created on 8 Jul 2016  路  4Comments  路  Source: laravel/framework

I am using Redis to cache queries/routes in Laravel 5.2.

I have 3 environments running on the same web server i.e "production", "staging", and "development."

For each environment I set a different cache prefix value to allow me to link the cache to different environment.

In the config/cache.php file I changed the line

 'prefix' => 'laravel',

To

 'prefix' => ENV('CACHE_PREFIX', 'laravel'),

Then in my .env file, I added the prefix for each environment like this

For Dev
CACHE_PREFIX="LaravelDev"

For Staging
CACHE_PREFIX="LaravelStaging"

For Production
CACHE_PREFIX="LaravelProduction"

I know I can clear the cache from the command line like this

 php artisan cache:clear

But the code above will clear the cache for all of my environments. It ignores the current environment prefix

Is this an issue with the command or is there a different way to clear all the cache for a specific environment?

I logged into my Redis server an I existing the following command
KEYS *

which gave me a list of all keys cached. I can see the correct prefix begin assigned. So the issue is with clearing it.

Most helpful comment

You can use multiple redis databases though. By default, Laravel uses db 0, but redis by default gives you 16 databases, 0 through 15.

All 4 comments

This is the expected behaviour. The prefix is not a cache namespace. It's literally just something stuck on the front of keys, and nothing more. Clearing will kill everything.

You can use multiple redis databases though. By default, Laravel uses db 0, but redis by default gives you 16 databases, 0 through 15.

Never have your production, development and stage environment on a single server.

You can use multiple redis databases though. By default, Laravel uses db 0, but redis by default gives you 16 databases, 0 through 15.

This is still the best solution even in 2019 thanks @GrahamCampbell

Was this page helpful?
0 / 5 - 0 ratings