Storing an Item in the Cache

Problem

You want to store a value in the cache.

Solution

Use the Cache::put() method.

Cache::put($key, $value, $minutes);

Discussion

Cache storage and retrieval usually follows the pattern below.

// 1. Pull the item from cache
$value = Cache::get($key);

// 2. Check if it was not found
if ($value === null)
{
    // 3. Figure the value manually
    $value = 'just something easy';

    // 4. Store it for a number of minutes
    $minutes = 30;
    Cache::put($key, $value, $minutes);
}
comments powered by Disqus