Getting an Item from the Cache or Storing a Default Forever

Problem

You want to retrieve an item from the cache.

But, if it doesn't exist you want to store the value in the cache forever.

Solution

Use the Cache::rememberForever() value.

$value = Cache::rememberForever($key, function()
{
    // fetch value from db or some other logic
    return $value;
});

If the item exists in the cache, it's returned immediately. But if it doesn't exist then the function is executed and the return value of the function is cached forever. In the later case this value is also returned.

Discussion

An alias to Cache::rememberForever() is Cache::sear().

This method is similar to the Cache::remember() method except you don't specify the $minutes because if the value is stored, it's stored permanently.

See the [Retrieving an Item from the Cache or Storing a Default]] recipe.

comments powered by Disqus