Skip to content

Commit

Permalink
feat: Add prefix functionality for redis presistance
Browse files Browse the repository at this point in the history
  • Loading branch information
yaraslau-kavaliou committed Jun 16, 2024
1 parent 1118c19 commit c6771fc
Showing 1 changed file with 61 additions and 15 deletions.
76 changes: 61 additions & 15 deletions common/persistence/class.PhpRedisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class common_persistence_PhpRedisDriver implements common_persistence_AdvKvDrive
public const DEFAULT_TIMEOUT = 5; // in seconds
public const RETRY_DELAY = 500000; // Eq to 0.5s

private const DEFAULT_PREFIX_SEPARATOR = '::';

/**
* @var Redis
*/
Expand Down Expand Up @@ -142,6 +144,50 @@ protected function callWithRetry($method, array $params, $attempt = 1)
return $result;
}

protected function getPrefix(array $params): ?string
{
$prefix = null;

if (!empty($this->params['prefix'])) {
$prefix = $this->params['prefix'];
}

return $prefix;
}

/**
* @param string|int $key
* @return string|int
*/
protected function prefixKey($key)
{
$prefix = $this->getPrefix($this->params);

if ($prefix === null) {
return $key;
}

return $prefix . ($this->params['prefixSeparator'] ?? self::DEFAULT_PREFIX_SEPARATOR) . $key;
}

protected function prefixKeys(array $keys, bool $keyValueMode = false): array
{
if ($this->getPrefix($this->params) !== null) {
$prefixedKeys = [];
foreach (array_values($keys) as $i => $element) {
if ($keyValueMode) {
$prefixedKeys[] = $i % 2 == 0 ? $this->prefixKey($element) : $element;
} else {
$prefixedKeys[] = $this->prefixKey($element);
}
}

return $prefixedKeys;
}

return $keys;
}

/**
* (non-PHPdoc)
* @see common_persistence_KvDriver::set()
Expand All @@ -155,57 +201,57 @@ public function set($key, $value, $ttl = null, $nx = false)
if ($nx) {
$options[] = 'nx';
}
return $this->callWithRetry('set', [$key, $value, $options]);
return $this->callWithRetry('set', [$this->prefixKey($key), $value, $options]);
}

public function get($key)
{
return $this->callWithRetry('get', [$key]);
return $this->callWithRetry('get', [$this->prefixKey($key)]);
}

public function exists($key)
{
return (bool)$this->callWithRetry('exists', [$key]);
return (bool)$this->callWithRetry('exists', [$this->prefixKey($key)]);
}

public function del($key)
{
return $this->callWithRetry('del', [$key]);
return $this->callWithRetry('del', [$this->prefixKey($key)]);
}

//O(N) where N is the number of fields being set.
public function hmSet($key, $fields)
{
return $this->callWithRetry('hmSet', [$key, $fields]);
return $this->callWithRetry('hmSet', [$this->prefixKey($key), $fields]);
}

//Time complexity: O(1)
public function hExists($key, $field)
{
return (bool)$this->callWithRetry('hExists', [$key, $field]);
return (bool)$this->callWithRetry('hExists', [$this->prefixKey($key), $field]);
}

//Time complexity: O(1)
public function hSet($key, $field, $value)
{
return $this->callWithRetry('hSet', [$key, $field, $value]);
return $this->callWithRetry('hSet', [$this->prefixKey($key), $field, $value]);
}

//Time complexity: O(1)
public function hGet($key, $field)
{
return $this->callWithRetry('hGet', [$key, $field]);
return $this->callWithRetry('hGet', [$this->prefixKey($key), $field]);
}

public function hDel($key, $field): bool
{
return (bool)$this->callWithRetry('hDel', [$key, $field]);
return (bool)$this->callWithRetry('hDel', [$this->prefixKey($key), $field]);
}

//Time complexity: O(N) where N is the size of the hash.
public function hGetAll($key)
{
return $this->callWithRetry('hGetAll', [$key]);
return $this->callWithRetry('hGetAll', [$this->prefixKey($key)]);
}

//Time complexity: O(N)
Expand All @@ -217,13 +263,13 @@ public function keys($pattern)
//Time complexity: O(1)
public function incr($key)
{
return $this->callWithRetry('incr', [$key]);
return $this->callWithRetry('incr', [$this->prefixKey($key)]);
}

//Time complexity: O(1)
public function decr($key)
{
return $this->callWithRetry('decr', [$key]);
return $this->callWithRetry('decr', [$this->prefixKey($key)]);
}

/**
Expand Down Expand Up @@ -257,23 +303,23 @@ public function scan(int &$iterator = null, string $pattern = null, int $count =
*/
public function mGet(array $keys)
{
return $this->callWithRetry('mGet', [$keys]);
return $this->callWithRetry('mGet', [$this->prefixKeys($keys)]);
}

/**
* @return bool|mixed
*/
public function mDel(array $keys)
{
return $this->callWithRetry('del', [$keys]);
return $this->callWithRetry('del', [$this->prefixKeys($keys)]);
}

/**
* @return bool|mixed
*/
public function mSet(array $keyValues)
{
return $this->callWithRetry('mSet', [$keyValues]);
return $this->callWithRetry('mSet', [$this->prefixKeys($keyValues, true)]);
}

/**
Expand Down

0 comments on commit c6771fc

Please sign in to comment.