Laravel 5.4 - php artisan cache:clear 使用“文件"缓存驱动程序时不清除缓存文件

2022-01-21 00:00:00 queue caching php laravel-5 laravel-artisan

Laravel 5.4 应用程序.CACHE_DRIVER 设置为 file 并且 QUEUE_DRIVER 设置为 .env 中的 sync.

Laravel 5.4 app. CACHE_DRIVER is set to file and QUEUE_DRIVER is set to sync in .env.

当我运行 php artisan cache:clear 它说 Cache clear successfully 但我的 storage/framework/cache 目录.

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.

对此感到沮丧,我还使用 rm -rf * 从该目录手动删除了 storage/framework/cache 下的所有文件/目录.

Frustrated by this, I also manually deleted all files/directories under storage/framework/cache using rm -rf * from that directory.

现在,当我运行 art queue:restart 我得到 [ErrorException] file_put_contents(/var/www/vhosts/my-app.com/releases/28/storage/framework/缓存/ee/2f/ee2f842aa7bb1f53ed
f3a2ed2c09a1807ffa6c90):打开流失败:没有这样的文件或目录

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

所以,我手上有两个问题.首先是:为什么Artisan没有删除所有缓存文件?如何安全地删除它们?第二个问题是:如何从中恢复,以便 php artisan queue:restart 不会对我出错?

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?

更新:在我看来,如果 QUEUE_DRIVER 设置为 sync,我可能没有理由重新启动队列工作程序,因此完全跳过该命令可以解决我的一半问题.仍然不确定如何正确删除那些 236K 的缓存文件.

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.

推荐答案

2020 年 1 月更新

似乎有一个简单的解决方案来解决所有这些问题.使用此答案 https://serverfault.com/a/96349 作为参考,您可以在父文件夹上设置 gid 位以便所有后续文件 &在 ./storage/* 下创建的文件夹可由正确组中的任何人写入,无论是谁创建的;从而克服组安全权限问题,如下所述.

Update Jan 2020

Seems there is an easy solution to all of this. Using this answer https://serverfault.com/a/96349 as 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.

这对我有用:

# 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

简答

使用 sudo:sudo rm -r ./storage/framework/cache

确保写入缓存的所有进程都使用同一个用户(而不仅仅是属于同一个组),因为事实证明 Laravel 写入缓存文件的权限类似于 0755 行,这限制了对所有者的写入.

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.

如果你像我一样为这些使用不同的用户:

If like me you use a different user for each of these:

  • PHP 进程
  • Artisan CLI
  • 通过主管的工匠(用于工作)

您最终会得到属于不同用户的文件,即使它们属于所需的组(例如 www-data),其他用户也无法写入或删除它们.

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).

希望有人能找到一种方法将 Larvel 中的新缓存文件权限设置为 0775 之类的东西.如果它只是从父级继承就好了.

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.

这对我来说也导致了主管进程与 Cache::remember() 之间的问题.PHP 进程导致我收到 put_file_contents 错误,因为不同用户无法写入缓存文件.

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.

我遇到了同样的问题,在我的情况下,文件没有被删除,因为它们受到了写保护.当我使用 rm -r ./storage/framework/cache 手动删除它们时,我收到警告 rm: descend into write-protected directory 'cache/c5'?.我不会为缓存中的每个文件都输入 yes,所以我运行了与 sudo & 相同的命令它运行顺利 sudo rm -r ./storage/framework/cache.

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.

这回答了您关于为什么 Artisan cache:clear & 没有删除它们的问题.运行 rm 是一个非常简单的解决方法;虽然它不能解决为什么文件被写为写保护的问题.

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.

删除缓存后,Laravel 再次将缓存创建为写保护.这意味着它可能是一个错误 &需要有人向 Laravel 开发人员提交错误报告.由于解决方法很简单,我将把它留给其他人去做.

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.

相关文章