/** * Increment value of key * @param string $key * @param mixed $by_value * if stored value is array: * if $by_value is value in array, new element will be pushed to the end of array, * if $by_value is key=>value array, 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 * @return int|string|array new value of key */ public function increment($key, $by_value = 1, $limit_keys_count = 0, $ttl = 259200) { if (!$this->acquire_key($key, $auto_unlocker)) { return false; } $this->setKeyTTL($key, $this->ttl_to_expiration($ttl)); $value = $this->memcache->get($this->prefix . $key); if (empty($value)) { if ($this->save($key, $by_value, $ttl)) { return $by_value; } else { return false; } } if (is_array($value)) { $value = $this->incrementArray($limit_keys_count, $value, $by_value); } elseif (is_numeric($value) && is_numeric($by_value)) { if ($by_value > 0) { return $this->memcache->increment($this->prefix . $key, $by_value); } else { return $this->memcache->decrement($this->prefix . $key, $by_value * -1); } } else { $value .= $by_value; } if ($this->save($key, $value, $ttl)) { return $value; } else { return false; } }
/** * Increment value of key * @param string $key * @param mixed $by_value * if stored value is array: * if $by_value is value in array, new element will be pushed to the end of array, * if $by_value is key=>value array, 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 * @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 (!$this->acquire_key($key, $auto_unlocker)) { return false; } $value = $this->memcache->get($this->prefix . $key); if (empty($value)) { if ($this->save($key, $by_value, $ttl)) { return $by_value; } else { return false; } } if (is_array($value)) { $value = $this->incrementArray($limit_keys_count, $value, $by_value); } elseif (is_numeric($value) && is_numeric($by_value)) { if ($by_value > 0) { return $this->memcache->increment($this->prefix . $key, $by_value); } else { $value += $by_value; } } else { $value .= $by_value; } if ($this->save($key, $value, $ttl)) { return $value; } else { return false; } }