Esempio n. 1
0
 /**
  * Increment value of the key
  * @param string $key
  * @param mixed $by_value
  *                              if stored value is an array:
  *                              if $by_value is a value in array, new element will be pushed to the end of array,
  *                              if $by_value is a key=>value array, new key=>value pair will be added (or updated)
  * @param int $limit_keys_count - maximum count of elements (used only if stored value is array)
  * @param int $ttl              - set time to live for key
  * @return int|string|array new value of key
  */
 public function increment($key, $by_value = 1, $limit_keys_count = 0, $ttl = 259200)
 {
     if (empty($key)) {
         $this->ReportError('empty keys are not allowed', __LINE__);
         return false;
     }
     if (is_numeric($by_value) && !$this->redis->Exists($this->serialize_key_prefix . $key)) {
         if (!($key_exists = $this->redis->Exists($this->prefix . $key)) || is_numeric($this->redis->Get($this->prefix . $key))) {
             if ($by_value >= 0) {
                 $result = $this->redis->IncrBy($this->prefix . $key, $by_value);
             } else {
                 $result = $this->redis->DecrBy($this->prefix . $key, $by_value * -1);
             }
             if ($result !== false) {
                 if ($ttl > 0) {
                     $this->redis->Expire($this->prefix . $key, $ttl);
                 }
                 return $result;
             }
         }
     }
     if (!$this->acquire_key($key, $auto_unlocker)) {
         return false;
     }
     $value = $this->read($key);
     if ($value === null || $value === false) {
         return $this->save($key, $by_value, $ttl);
     }
     if (is_array($value)) {
         $value = $this->incrementArray($limit_keys_count, $value, $by_value);
     } elseif (is_numeric($value) && is_numeric($by_value)) {
         $value += $by_value;
     } else {
         $value .= $by_value;
     }
     if ($this->save($key, $value, $ttl)) {
         return $value;
     } else {
         return false;
     }
 }