I am using laravel caching ( remember() method) on a website with this code:
$postedItems = Cache::remember('home_posted_items', $this->cacheTimes['postedItems'], function() { 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']); }
Džuris
source share