/** * Select from storage via callback function * @param callback $fx ($value, $key) - should return true to select key(s) * @param bool $get_array * @return mixed */ public function select_fx($fx, $get_array = false) { $arr = array(); $prefix_length = strlen($this->prefix); $keys = $this->redis->Keys($this->prefix . '*'); foreach ($keys as $key) { $content = $this->redis->Get($key); if (empty($content)) { continue; } $value = unserialize($content); $index = substr($key, $prefix_length); if ($fx($value, $index) === true) { if (!$get_array) { return $value; } else { $arr[$index] = $value; } } } if (!$get_array || empty($arr)) { return false; } else { return $arr; } }
/** * 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; } }