Laravel sometimes doesn't read cache

I am using laravel caching ( remember() method) on a website with this code:

 $postedItems = Cache::remember('home_posted_items', $this->cacheTimes['postedItems'], function() { /* the stuff that prepares data */ return ['items' => $items, 'firstItemNumber' => $firstItem]; }); 

The problem is that sometimes (every few days, I would say) the cached file seems to be damaged, and as a result I have a downtime before the cache expires (if I did not clear it manually).

Here is the part of the error stack that may be relevant:

 [2017-02-04 22:01:34] production.ERROR: ErrorException: unserialize(): Error at offset 131059 of 131062 bytes in /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/FileStore.php:78 Stack trace: #0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'unserialize(): ...', '/home/path/to/...', 78, Array) #1 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/FileStore.php(78): unserialize('a:2:{s:7:"item...') #2 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/FileStore.php(47): Illuminate\Cache\FileStore->getPayload('home_posted_ite...') #3 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/Repository.php(98): Illuminate\Cache\FileStore->get('home_posted_ite...') #4 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/Repository.php(202): Illuminate\Cache\Repository->get('home_posted_ite...') #5 [internal function]: Illuminate\Cache\Repository->remember('home_posted_ite...', 1, Object(Closure)) #6 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php(318): call_user_func_array(Array, Array) #7 /home/path/to/app/bootstrap/cache/compiled.php(6089): Illuminate\Cache\CacheManager->__call('remember', Array) #8 /home/path/to/app/app/Http/Controllers/HomeController.php(197): Illuminate\Support\Facades\Facade::__callStatic('remember', Array) 

How to solve this problem?

From experience, I know that clearing the cache solves the problem. Thus, the problem seems to be related to file corruption. I think if I noticed that the “file is unreadable” and just clears the cache ( Cache::forget(...) ), it should solve the problem.

What would be the best way to notice such an error? It seems that all the file extraction logic is hidden inside the remember() method. Should I just expand it and use other methods, something like the following?

 if (!($postedItems = @Cache::get('home_posted_items')) { // prepare data $postedItems = ['items' => $items, 'firstItemNumber' => $firstItem]; Cache::put('home_posted_items', $postedItems, $this->cacheTimes['postedItems']); } 
+7
php caching laravel-5
source share
2 answers

IMHO may be a problem with the file driver. I think that if you have a good web server capable of handling concurrent requests, the problem is that the file driver is not able to handle the concurrency handle.

And this is due to the fact that usually the file systems themselves are not so good at processing different simultaneous processes of reading / writing to the same file.

In the end, I advise you to switch the driver to something more capable when handling concurrency, i.e. Memcached or Redis, but also Database should be enough.

You can find the same suggestion for the session here , look at the second message, and I think this might be relevant for the cache file driver too.

+1
source share

This doesn't seem to be a permission issue (you can see # 1 in the stack trace), somehow laravel overrides (corrupts) the cache files. You need to find out what content you have at the end of the file.

You also need to check what content you put in the cache, laravel uses a plugin to store files, which may have an error.

And the best way to debug is here. fooobar.com/questions/90060 / ...

0
source share