laravel 如何使用命令行清除laravel中的缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38256469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:27:08  来源:igfitidea点击:

How can I clear the cache in laravel using command line?

laravelcachinglaravel-5laravel-5.2artisan

提问by Jaylen

I am using Redisto cache queries/routes in Laravel 5.2.

我正在使用Redis在 Laravel 5.2 中缓存查询/路由。

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

我有 3 个环境在同一 Web 服务器上运行,即“生产”、“暂存”和“开发”。

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

对于每个环境,我设置了不同的缓存prefix值,以允许我将缓存链接到不同的环境。

In the config/cache.phpfile I changed the line

config/cache.php文件中我改变了这一行

'prefix' => 'laravel',

To

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

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

然后在我的.env文件中,我为每个环境添加了这样的前缀

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.

但是上面的代码将清除我所有环境的缓存。

I only want to clear the cache for "LaravelDev" only and leave alone "LaravelStaging" and "LaravelProduction"

我只想清除“LaravelDev”的缓存,而不要“LaravelStaging”和“LaravelProduction”

How can I clear the cache for a specific environment?

如何清除特定环境的缓存?

回答by Alexander Pekhota

Instead of using CACHE_PREFIX you can use different redis databases. For ex: production => 1, staging => 2, development => 3. Use this links if you want to be a little bit more familiar with it:

您可以使用不同的 redis 数据库,而不是使用 CACHE_PREFIX。例如:生产 => 1,分期 => 2,开发 => 3。如果您想更熟悉它,请使用此链接:

  1. What's the Point of Multiple Redis Databases?
  2. List All Redis Databases
  1. 多个Redis数据库有什么意义?
  2. 列出所有 Redis 数据库

So, in your .env file for each environment (production/stage/dev) you need to define different REDIS_CACHE_DB env value.

因此,在每个环境(生产/阶段/开发)的 .env 文件中,您需要定义不同的 REDIS_CACHE_DB 环境值。

Link to the line uses this variable https://github.com/laravel/laravel/blob/2a2522d8824c0852e30a7e3e07d46a543eb4b33d/config/database.php#L142. Examples of .env:

该行的链接使用此变量https://github.com/laravel/laravel/blob/2a2522d8824c0852e30a7e3e07d46a543eb4b33d/config/database.php#L142。.env 的例子:

.env.production

.env.生产

REDIS_CACHE_DB=1

.env.stage

.env.stage

REDIS_CACHE_DB=2

.env.development

.env.development

REDIS_CACHE_DB=3

Don't forget to clear config cache after changing env variables: https://laravel.com/docs/7.x/configuration#configuration-caching

更改环境变量后不要忘记清除配置缓存:https: //laravel.com/docs/7.x/configuration#configuration-caching

Hope this helps!

希望这可以帮助!

回答by Chris Cynarski

What the cache:clearartisan command does calls flushfunction on current connector. As caching engines varies with functionality I don't think it's possible to expire keys selectively keeping cache API universal. Engines like Redis provides such functionality, but memcached for example don't.

什么cache:clear工匠命令不调用flush当前连接器的功能。由于缓存引擎因功能而异,我认为不可能有选择地使密钥过期以保持缓存 API 的通用性。像 Redis 这样的引擎提供了这样的功能,但是 memcached 就没有。

If you're using Redis you can modify default connector to use 'SCAN / DEL' commands for flush()method. Still this is not going to be very effective.

如果您使用的是 Redis,您可以修改默认连接器以使用“SCAN/DEL”命令作为flush()方法。尽管如此,这不会很有效。

If your application uses cache correctly flushing it should not cause any problems as the cache will rebuild itself. You should never expect some data being in the cache as it might expire anyway.

如果您的应用程序正确使用缓存刷新,它应该不会导致任何问题,因为缓存会自行重建。您永远不应该期望缓存中有一些数据,因为它可能无论如何都会过期。