Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Using the cache engine in Laravel for better performance

Sunday, July 19th, 2015 by Servage

cache-redisOne of the common problems with website performance is slow load times due to long response times from the server. The response can be slowed down for various reasons, and thankfully there are also multiple points where optimization can occur. Regardless of network issues on either end of the connection, you can always optimize load times with caching. This may even be a significant improvement, especially when caching database operations.

Laravel cache engine

In the Laravel framework caching is built into the core and is therefore available in the default installation. You can basically cache anything right from your PHP code. This is useful for caching both direct database results, but also processed content, which could be partial views or HTML.

Configuration

You must configure your cache handling before you can start using it. That involves editing the “app/config/cache.php” file to suit your environment. Laravel ships with built in support for Redis, Memcached, File and Database powered cache. At Servage we suggest you use the file-based cache, because it will keep the cache files local on the webserver.

Usage

// Store the "value" for "key" forever (until you remove it again)
Cache::forever('key', 'value');

// Store the "value" for "key" for so many $minutes
Cache::put( 'key', 'value', $minutes );

// Use "add" instead of put, to just add if not yet existing
Cache::add( 'key', 'value', $minutes );

// Or check manually if a key exists, and then do something
if ( Cache::has('key') )
{
  // Something ...
}

Retrieving data from the cache is super simple too:

// Get the cached value for "key"
$value = Cache::get( 'key' );

// Get the cached value for "key" - or a default value
$value = Cache::get( 'key', 'default' );

Another convenient utility is the ability to easily increment or decrement a value:

// Increment the counter by $amount (or omit it to be default 1)
Cache::increment('key', $amount);

// Similar for decrements
Cache::decrement('key', $amount);

Where to use it?

It is suggested that you implement caching for repetitive requests for the same information. Below are a few examples.

Your site displays a language selector at the top of the page. The available languages are based on a database table, where some PHP renders the HTML with the language selector elements. You could cache the database request for the languages or even the entire rendered HTML, because you expect the available languages not to change very often.

Your site shows products in a web shop. You could cache the product pages until changes are made. Thereby only product changes trigger database queries, and not every single visit to the site.

Your site provides users a backend where they can review and edit profile information. You could cache each user’s profile until changes are made. Thereby the profile page would only need to perform database queries after a change.

There are unlimited suitable use-cases for caching. They can almost all help your site performance improve.

Using the cache engine in Laravel for better performance, 4.8 out of 5 based on 5 ratings
Categories: Guides & Tutorials, Tips & Tricks

Keywords: , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.