Laravel 5.4-PHP Artisan cache:clear在使用'文件'缓存驱动程序时不会清除缓存文件 [英] Laravel 5.4 - php artisan cache:clear does not clear cache files when using 'file' cache driver

查看:420
本文介绍了Laravel 5.4-PHP Artisan cache:clear在使用'文件'缓存驱动程序时不会清除缓存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel 5.4应用程序.在.env中将CACHE_DRIVER设置为file,将QUEUE_DRIVER设置为sync.

当我运行php artisan cache:clear时,它说Cache cleared successfully,但是我的storage/framework/cache目录中仍然有236K的文件.

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

现在,当我运行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

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

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

解决方案

更新2020年1月

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

这对我有用:

# 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的特权来写入具有特权的缓存文件,从而限制了对所有者的写入. /p>

如果像我一样,您为每个用户使用不同的用户

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

您最终得到的文件属于不同的用户,并且即使其他用户属于必需的组,也不能被其他用户写入或删除(例如www-data).

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

旁注

对我来说,这也导致主管程序和管理员之间的Cache::remember()问题. PHP进程,由于无法由不同用户写入缓存的文件,因此出现put_file_contents错误.

原始答案

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

这回答了您有关Artisan为什么不将其删除的问题cache:clear&运行rm是很容易的解决方法;尽管它不能解决为什么文件被写为写保护的问题.

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

Laravel 5.4 app. CACHE_DRIVER is set to file and QUEUE_DRIVER is set to 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.

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

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

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?

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.

解决方案

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.

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

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.

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

  • PHP process
  • Artisan CLI
  • Artisan via supervisor (for jobs)

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

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.

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.

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.

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.

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 5.4-PHP Artisan cache:clear在使用'文件'缓存驱动程序时不会清除缓存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆