Laravel 5.4 - php artisan cache:clear 在使用“文件”缓存驱动程序时不会清除缓存文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45555783/
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
Laravel 5.4 - php artisan cache:clear does not clear cache files when using 'file' cache driver
提问by fronzee
Laravel 5.4 app. CACHE_DRIVER
is set to file
and QUEUE_DRIVER
is set to sync
in .env
.
Laravel 5.4 应用程序。CACHE_DRIVER
设置为file
并且QUEUE_DRIVER
设置为sync
in .env
。
When I run php artisan cache:clear
It says Cache cleared successfully
yet I still have 236K of files in my storage/framework/cache
directory.
当我运行时,php artisan cache:clear
它说Cache cleared successfully
我的storage/framework/cache
目录中仍然有 236K 的文件。
Frustrated by this, I also manually deleted all files/directories under storage/framework/cache
using rm -rf *
from that directory.
对此感到沮丧,我还手动删除了该目录下storage/framework/cache
using下的所有文件/rm -rf *
目录。
Now, when I run art queue:restart
I get [ErrorException] file_put_contents(/var/www/vhosts/my-app.com/releases/28/storage/framework/cache/ee/2f/ee2f842aa7bb1f53ed
f3a2ed2c09a1807ffa6c90): failed to open stream: No such file or directory
现在,当我跑步时,art queue:restart
我得到[ErrorException] file_put_contents(/var/www/vhosts/my-app.com/releases/28/storage/framework/cache/ee/2f/ee2f842aa7bb1f53ed
f3a2ed2c09a1807ffa6c90): failed to open stream: No such file or directory
So, I have two problems on my hands. First is: why aren't all the cache files deleted by Artisan? How do I safely delete them? Second problem is: how do I recover from this so that php artisan queue:restart
doesn't error out on me?
所以,我手上有两个问题。首先是:为什么Artisan没有删除所有缓存文件?如何安全地删除它们?第二个问题是:我如何从中恢复,以免php artisan queue:restart
出错?
UPDATE: It occurred to me that I probably have no reason to restart a queue worker if QUEUE_DRIVER
is set to sync
, so skipping that command altogether resolves half my issue. Still not sure how to properly delete those 236K of cache files though.
更新:我突然想到,如果QUEUE_DRIVER
设置为sync
,我可能没有理由重新启动队列工作器,因此完全跳过该命令可以解决我的一半问题。仍然不确定如何正确删除那些 236K 的缓存文件。
回答by Precastic
Update Jan 2020
2020 年 1 月更新
Seems there is an easy solution to all of this. Using this answer https://serverfault.com/a/96349as a reference, you can set the gid bit on the parent folder so that all subsequent files & folders created under ./storage/*
are writable by anyone in the correct group regardless of who created them; thereby overcoming the group security permission issues as explained below.
似乎有一个简单的解决方案可以解决所有这些问题。使用此答案https://serverfault.com/a/96349作为参考,您可以在父文件夹上设置 gid 位,以便在其下创建的所有后续文件和文件夹./storage/*
都可由正确组中的任何人写入,而不管是谁创建的; 从而克服组安全权限问题,如下所述。
This works for me:
这对我有用:
# Assumes all required users belong to the www-data group
sudo chgrp -R www-data /path/to/storage
sudo chmod g+s /path/to/storage
Short answer
简答
Use sudo: sudo rm -r ./storage/framework/cache
使用须藤: sudo rm -r ./storage/framework/cache
Long answer
长答案
Make sure all processes writing to the cache use the same user (and not just belong to the same group) because it turns out that Laravel writes cache files with privileges something along the lines of 0755 which restricts writes to the owner.
确保所有写入缓存的进程都使用相同的用户(而不仅仅是属于同一个组),因为事实证明 Laravel 使用类似于 0755 的权限写入缓存文件,这限制了对所有者的写入。
If like me you use a different user for each of these:
如果像我一样,您对每个使用不同的用户:
- PHP process
- Artisan CLI
- Artisan via supervisor (for jobs)
- PHP进程
- 工匠 CLI
- 工匠通过主管(工作)
You end up with files that belong to different users and cannot be written to or deleted by the other users even if they belong to the required group (www-data as an example).
您最终会得到属于不同用户的文件,即使他们属于所需的组(例如 www-data),其他用户也无法写入或删除它们。
Hopefully someone can find a way to set new cache file privileges in Larvel to something like 0775. It would be nice if it just inherited from the parent.
希望有人能找到一种方法将 Larvel 中的新缓存文件权限设置为 0775 之类的东西。如果它只是从父级继承而来就好了。
Side note
边注
This for me was also causing a problem with Cache::remember()
between the supervisor process & the PHP process such that I was getting put_file_contents
errors because the cached files couldn't be written to by the different users.
这对我来说也导致了Cache::remember()
主管进程和 PHP 进程之间的问题,以至于我收到put_file_contents
错误,因为不同的用户无法写入缓存的文件。
Original answer
原答案
I was having the same problem and in my case the files weren't being deleted because they were write protected. When I went to delete them manually using rm -r ./storage/framework/cache
I got the warning rm: descend into write-protected directory 'cache/c5'?
. I wasn't going to type yes for every file in the cache so I ran the same command as sudo & it worked without a hitch sudo rm -r ./storage/framework/cache
.
我遇到了同样的问题,在我的情况下,文件没有被删除,因为它们被写保护。当我使用手动删除它们时,rm -r ./storage/framework/cache
我收到了警告rm: descend into write-protected directory 'cache/c5'?
。我不打算为缓存中的每个文件都输入 yes,所以我运行了与 sudo 相同的命令,它可以顺利运行sudo rm -r ./storage/framework/cache
。
This answers your question as to why they aren't being deleted by Artisan cache:clear
& running rm
is an easy enough work-around; although it doesn't solve the problem of why the files are being written as write-protected.
这回答了您的问题,即为什么他们没有被 Artisan 删除,而cache:clear
runningrm
是一个足够简单的解决方法;虽然它没有解决为什么文件被写为写保护的问题。
After deleting the cache Laravel again creates the cache as write-protected. This means it is probably a bug & requires someone to submit a bug report to the Laravel developers. Since the work-around is trivial I'll leave that for someone else to do.
删除缓存后,Laravel 再次将缓存创建为写保护。这意味着它可能是一个错误并且需要有人向 Laravel 开发人员提交错误报告。由于解决方法微不足道,我会将其留给其他人来做。
回答by Leonardo Cabré
You can try:
你可以试试:
php artisan config:cache
It solve most of my problems.
它解决了我的大部分问题。
回答by A.G.
I ran into similar issue recently. The cache files created through Cache
facade seems to persist even after running php artisan cache:clear
and as mentioned by @Precastic, is turned out to be a file permission issue.
我最近遇到了类似的问题。通过Cache
Facade创建的缓存文件即使在运行后似乎仍然存在php artisan cache:clear
,正如@Precastic 所提到的,结果证明是文件权限问题。
Instead of removing the files/folders manually, I just ran the same command with admin privilege like this
我没有手动删除文件/文件夹,而是像这样以管理员权限运行相同的命令
sudo php artisan cache:clear
and it worked for me. Hope this helps someone.
它对我有用。希望这可以帮助某人。
回答by hschmieder
You can also use Tinker:
您还可以使用 Tinker:
php artisan tinker
Cache::store("file")->flush()