Jamm\Memory\Shm\SingleMemory::increment PHP Method

increment() public method

Increment value of key
public increment ( string $key, mixed $by_value = 1, integer $limit_keys_count, integer $ttl = 259200 ) : integer | string | array
$key string
$by_value mixed 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)
$limit_keys_count integer - maximum count of elements (used only if stored value is array)
$ttl integer
return integer | 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;
        }
        $key = (string) $key;
        $auto_unlocker = NULL;
        if (!$this->sem->get_access_write($auto_unlocker)) {
            return false;
        }
        $this->readmemory();
        if (!isset($this->mem[self::map_keys][$key])) {
            return $this->save($key, $by_value);
        }
        $value = $this->mem[self::map_keys][$key];
        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;
        }
        $ttl = intval($ttl);
        if ($ttl > 0) {
            $this->mem[self::map_key_ttl][$key] = time() + $ttl;
        }
        $this->mem[self::map_keys][$key] = $value;
        $this->refresh();
        return $value;
    }