Storing an Item in the Cache if it Doesn't Exist


You want to store an item in the cache.

But you only want to store it if the item doesn't already exist.


Use the Cache::add() method.

$result = Cache::add($key, $value, $minutes);
if ($result)
    echo "$key stored for $minutes";
    echo "$key wasn't stored, already in cache";

If the item was stored, true is returned. Otherwise false is returned.


A couple of notes about this method.

  1. If false is returned, you should call Cache::get() to retrieve the item.
  2. If the key exists in the cache and is null, the new value will always be stored and true returned.
comments powered by Disqus