Problem
You want to store an item in the cache.
But you only want to store it if the item doesn't already exist.
Solution
Use the Cache::add() method.
$result = Cache::add($key, $value, $minutes);
if ($result)
{
echo "$key stored for $minutes";
}
else
{
echo "$key wasn't stored, already in cache";
}
If the item was stored, true is returned. Otherwise false is returned.
Discussion
A couple of notes about this method.
- If
falseis returned, you should callCache::get()to retrieve the item. - If the key exists in the cache and is
null, the new value will always be stored andtruereturned.
