add() public method

Add value to memory storage, only if this key does not exists (or false will be returned).
public add ( string $key, mixed $value, integer $ttl = 259200, array | string $tags = NULL ) : boolean
$key string
$value mixed
$ttl integer
$tags array | string
return boolean
Example #1
0
 public function store($task_object, $unique = false, $priority = 1)
 {
     $content = serialize($task_object);
     $priority = '[' . intval($priority) . ']';
     if ($unique) {
         $hash = '1' . md5($content);
         return $this->mem->add($hash, $content);
     } else {
         $key = $t = $priority . round(microtime(true) * 1000);
         $i = 0;
         while ($this->mem->add($key, $content) === false) {
             $i++;
             if ($i > 1000) {
                 trigger_error('Can not add key more than 1000 times', E_USER_NOTICE);
                 return false;
             }
             $key = $t . $i;
         }
         return true;
     }
 }