Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel and view caching in development -- can't see changes right away

Tags:

Some friends and I decided to start working on a project and we came across Laravel and thought it might be a good tool. We started using it locally to develop out some of our pages and noticed something strange.

When we update a view with different information, it would take almost 5 to 10 minutes before the views information would change. It's like Laravel is caching the view and put a TTL on it.

I know this isn't anything I am doing on my local web server because I have used other frameworks and I have never encountered this issue.

Upon searching the Internet, I can't find a great answer on how to disable this. I want to use Laravel, but find it worthless if it takes a while for my views to update each time I want to make a change. In fact, it sounds counter productive.

Is there any way to disable this? Why are my views taking forever to update right out of the box?

like image 762
Sethen Avatar asked Dec 14 '13 02:12

Sethen


People also ask

Does Laravel support caching explain in detail?

Yes, Laravel provides support for popular caching backends like Memcached and Redis. By default, Laravel is configured to use file cache driver, which is used to store the serialized or cached objects in the file system. For huge projects, it is suggested to use Memcached or Redis.

How do I enable caching in Laravel?

To enable Laravel caching services, first use the Illuminate\Contracts\Cache\Factory and Illuminate\Contracts\Cache\Repository, as they provide access to the Laravel caching services. The Factory contract gives access to all the cache drivers of your application.

Where does Laravel store cached data?

The cache configuration is located at app/config/cache. php . In this file you may specify which cache driver you would like used by default throughout your application. Laravel supports popular caching backends like Memcached and Redis out of the box.


2 Answers

The #laravel IRC channel is a God send. This had nothing to do with Laravel's behavior at all. This was actually something PHP 5.5 was doing.

The reason this was so baffling is because I upgraded my PHP version from 5.3 and never had this issue.

In your .ini file, you need to tweak your OPcache settings. For me, these settings began at line 1087 in the .ini file and looked something like this:

opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1 

Take particular note of the opcache.revalidate_freq=60. This is what is actually making your views cache. If this is not the desired behavior, set the value to 0 and your views will update every time you make a change. Yay!

EDIT AUGUST 21, 2014

As mentioned by Matt below, make sure to restart your web server to see your changes take effect after you have changed your .ini file.

like image 105
Sethen Avatar answered Oct 02 '22 15:10

Sethen


With newer versions of PHP, opcache doesn't work. This is what I use (in app/filters.php):

App::before(function($request) {     // Clear view cache in sandbox (only) with every request     if (App::environment() == 'sandbox') {         $cachedViewsDirectory=app('path.storage').'/views/';         $files = glob($cachedViewsDirectory.'*');         foreach($files as $file) {             if(is_file($file)) {                 @unlink($file);             }         }     } }); 
like image 30
Wendtly Avatar answered Oct 02 '22 17:10

Wendtly