Code Happy: Caching

← Back to Index

Please note that this chapter was written for VERSION 3 of the Laravel PHP Framework.

Laravel offers a very simple to use Caching class, allowing you to easily cache anything you need to, for however long you like. Confused? Let's take a look at it in action.

Setup

There are many ways to store your cached data you must set a driver stating which storage method you want to use in application/config/cache.php. The options are 'file', 'memcached', 'apc', 'redis', and 'database'. You may experience better performance with 'apc' or 'memcached' but I am going to use 'file' based caching for its simplicity to setup.

<?php

'driver' => 'file',

Setting values

You can use the put() or forever() methods to store data in the Cache.

The put() method allows you to store a value for a set period of time, let's have a look at a code sample..

<?php

$data = 'complicated_generated_data';
Cache::put('mydata', $data, 10);

Let's pretend that $data wasn't just an assigned string, but the result of a complicated algorithm that would take some time to process. We don't want to process this data all the time, so we keep it in the cache.

The first parameter to the method is the key, a string used to identify the cached data. The second parameter is the data itself, and the third is the amount of time in minutes to cache to the data.

The forever() method takes only the first two parameters, and acts exactly as the name implies. The data is cached forever.

Retrieving Values

To retrieve an item from the cache use the get() method and supply the key, for example..

<?php

$data = Cache::get('mydata');

By default, if the cached item does not exist or has expired already, the method will return NULL. However, you can pass an optional second parameter to provide an alternate default value.

<?php

$data = Cache::get('mydata', 'complicated data');

You can also pass a closure as the second parameter and the value returned by the closure will be used if the cache data does not exist. The closure will only be executed if the key does not exist.

A better way

You might find yourself using the has() method to check if a cached item exists, and falling into this familiar pattern..

<?php

if (! Cache::has('mydata'))
{
    $data = 'complicated_generated_data';
    Cache::put('mydata', $data, 10);   
}

return $data;

However, you can use the remember() method as a much more elegant solution. The remember() method will return the value if it exists in the cache, or it will store and return the value of the second parameter for a defined length of time, for example..

<?php

return Cache::remember('mydata', function () {
    return 'complicated_generated_data';
}, 10);

Of course the closure will not be executed unless the key does not exist or has expired.

Have fun using the Cache!

My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.

It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!