A Complete Guide to Laravel Caching and Application Performance

Today speed is everything! Maybe that’s the reason why both businesses and web developers are looking around for ways to speed up your app. Fortunately, there are several ways by which a web app or web service could be optimized for speed. I am talking about none other than Laravel caching!

The following post emphasizes on Laravel caching and how it helps in speeding up your laravel application performance. Being one of the most used PHP frameworks, Laravel has a few tricks up its sleeves to make this happen. One of them is config caching. Obviously, this is not going to make tremendous improvement, but it is significant enough to be written about. So, what is config caching? Why is it important for your upcoming Laravel project?

What is Laravel Caching?

Caching is a technique used to cache data for a certain period of time. If you happen to work on a high traffic website, the skill turns out to be extremely useful. You see, high traffic websites tend to do heavy database lifting. To reduce access load on database servers it becomes important to cache the data that doesn’t change often.

Now let me explain by offering an example, imagine you have a blog website where you list the recent blogs. Your website has millions of users accessing per day. You do not write blogs every minute so why can’t we just cache the list of blogs that come from the database after a certain period of time.

If you cache the data for 5 minutes, it saves the db queries for 5 minutes which can reduce an amount of hits to your database and can speed up your website loading as well.

Overall, caching plays an extremely vital role when it comes to optimizing the performance of your web apps. In laymen terms, caching methods speeds up website and makes page load time faster than the conventional ones.

In most cases, when clients ask about better website speed, developers more often opt for advanced Laravel caching tools to speed up the apps. No doubt, Laravel cache plays an important role in making a web app’s performance skyrocket.

Technically speaking, Laravel, has an artisan command for this:

php artisan config:cache

. Define cache driver in .env file

Laravel supports following types of caching drivers: apc file redis array database memcached You can define settings for cache driver in .env file of laravel root project. CACHE_DRIVER=file

Understanding the working of Caching

Before we proceed any further, let us understand how caching works?

Every cache is identified with a unique id called a “cache_key” Using this cache key we check to see if cache already exists If data is not found for given cache key we generate the data and store it using cache key Now, next time we check to see if data exists using cache key we find data Stored data is stored for specified time and exercises after it passes that time

Laravel Cache

It may quite interest you to know that laravel provides an efficient and effective API for different caching backends. You can find the configuration for Laravel cache within the config/cache.php folder.

Inside the file, you can specify which cache driver you wish to use as a default one. Some of the popular Laravel caching backends are:

Memcached

Redis

Database

File

Array

Laravel Route Caching

Are you ready to enhance the performance of the website up to 100X faster pace? How much time could route registration take anyway? It seems like it would be practically negligible, so why cache it?

In config/app.php we see that AppProvidersRouteServiceProvider::class is listed as a default framework provider. AppProvidersRouteServiceProvider::class extends IlluminateFoundationSupportProvidersRouteServiceProvider which has its own boot function that looks like this:

public function boot()

{

$this->setRootControllerNamespace();

if ($this->app->routesAreCached()) {

$this->loadCachedRoutes();

} else {

$this->loadRoutes();

$this->app->booted(function () {

$this->app[‘router’]->getRoutes()->refreshNameLookups();

$this->app[‘router’]->getRoutes()->refreshActionLookups();

});

}

}

As soon as you set controller namespace, the boot method checks to see if the routes have been cached. Let’s explore what happens both with and without the route cache.

Read Full Post: [HERE]