Laravel 5.4 - php artisan cache:clear does not cle

2019-03-18 16:28发布

问题:

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.

回答1:

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.



回答2:

You can try:

php artisan config:cache

It solve most of my problems.