Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Redis reset() method #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Ganesha/Storage/Adapter/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function loadStatus(string $service): int

public function reset(): void
{
// TODO: Implement reset() method.
$this->redis->reset();
}

/**
Expand Down
48 changes: 48 additions & 0 deletions src/Ganesha/Storage/Adapter/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ackintosh\Ganesha\Storage\Adapter;

use Ackintosh\Ganesha\Exception\StorageException;
use Ackintosh\Ganesha\Storage;
use Exception;

class RedisStore
Expand Down Expand Up @@ -186,4 +187,51 @@ public function get(string $key)
throw new StorageException($exception->getMessage(), $exception->getCode(), $exception);
}
}

/**
* Resets the storage.
*
* @return void
*
* @throws StorageException
*/
public function reset(): void
{
try {
$redisPrefix = $this->redis->getOption(\Redis::OPT_PREFIX);
$ganeshaPrefix = Storage\StorageKeys::KEY_PREFIX;
$prefix = $redisPrefix . $ganeshaPrefix;
$suffixes = [
Storage\StorageKeys::KEY_SUFFIX_SUCCESS,
Storage\StorageKeys::KEY_SUFFIX_FAILURE,
Storage\StorageKeys::KEY_SUFFIX_REJECTION,
Storage\StorageKeys::KEY_SUFFIX_LAST_FAILURE_TIME,
Storage\StorageKeys::KEY_SUFFIX_STATUS,
];
$suffixesRegex = implode('|', array_map('preg_quote', $suffixes));

$iterator = null;
do {
$keys = $this->redis->scan($iterator, $prefix . '*');
if ($keys !== false) {
$keysToDelete = array_filter(array_map(function ($key) use ($prefix, $redisPrefix, $suffixesRegex) {
return preg_match("/^{$prefix}(.*)(?:$suffixesRegex)$/", $key) ? substr($key, strlen($redisPrefix)) : null;
}, $keys));

if (!empty($keysToDelete)) {
$deleted = $this->redis->del($keysToDelete);
if ($deleted !== count($keysToDelete)) {
throw new StorageException(sprintf(
"Failed to delete all keys. Deleted %d out of %d keys.",
$deleted,
count($keysToDelete)
));
}
}
}
} while ($iterator > 0);
} catch (Exception $exception) {
throw new StorageException($exception->getMessage(), $exception->getCode(), $exception);
}
}
}