-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from Adi3g/46-redis-module
46 redis module
- Loading branch information
Showing
7 changed files
with
134 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from __future__ import annotations | ||
|
||
import click | ||
|
||
from nox.domains.redis_manager import RedisManager | ||
|
||
# Initialize Redis manager | ||
redis_manager = RedisManager() | ||
|
||
|
||
@click.group() | ||
def redis(): | ||
"""Redis management commands.""" | ||
pass | ||
|
||
|
||
@click.command() | ||
@click.option('--key', required=True, help='Key to set in Redis') | ||
@click.option('--value', required=True, help='Value to set for the key') | ||
def set_key(key, value): | ||
"""Set a key in Redis.""" | ||
result = redis_manager.set_key(key, value) | ||
click.echo(result) | ||
|
||
|
||
@click.command() | ||
@click.option('--key', required=True, help='Key to get from Redis') | ||
def get_key(key): | ||
"""Get a key from Redis.""" | ||
result = redis_manager.get_key(key) | ||
click.echo(result) | ||
|
||
|
||
@click.command() | ||
@click.option('--key', required=True, help='Key to delete from Redis') | ||
def delete_key(key): | ||
"""Delete a key from Redis.""" | ||
result = redis_manager.delete_key(key) | ||
click.echo(result) | ||
|
||
|
||
@click.command() | ||
@click.option('--pattern', default='*', help='Pattern to match keys (default: *)') | ||
def list_keys(pattern): | ||
"""List keys in Redis.""" | ||
result = redis_manager.list_keys(pattern) | ||
click.echo('\n'.join(result)) | ||
|
||
|
||
@click.command() | ||
def flush_db(): | ||
"""Flush the current Redis database.""" | ||
result = redis_manager.flush_database() | ||
click.echo(result) | ||
|
||
|
||
@click.command() | ||
def info(): | ||
"""Get Redis server information.""" | ||
result = redis_manager.info() | ||
click.echo(result) | ||
|
||
|
||
# Add commands to the redis group | ||
redis.add_command(set_key) | ||
redis.add_command(get_key) | ||
redis.add_command(delete_key) | ||
redis.add_command(list_keys) | ||
redis.add_command(flush_db) | ||
redis.add_command(info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import redis | ||
|
||
|
||
class RedisManager: | ||
def __init__(self, host: str = 'localhost', port: int = 6379, db: int = 0): | ||
"""Initialize the Redis manager.""" | ||
self.client = redis.Redis(host=host, port=port, db=db) | ||
|
||
def set_key(self, key: str, value: Any) -> str: | ||
"""Set a key in Redis.""" | ||
try: | ||
self.client.set(key, value) | ||
return f"Key '{key}' set successfully." | ||
except Exception as e: | ||
return f"Error setting key '{key}': {str(e)}" | ||
|
||
def get_key(self, key: str) -> str | None: | ||
"""Get a key from Redis.""" | ||
try: | ||
value = self.client.get(key) | ||
return value.decode() if value else 'Key not found.' | ||
except Exception as e: | ||
return f"Error getting key '{key}': {str(e)}" | ||
|
||
def delete_key(self, key: str) -> str: | ||
"""Delete a key from Redis.""" | ||
try: | ||
self.client.delete(key) | ||
return f"Key '{key}' deleted successfully." | ||
except Exception as e: | ||
return f"Error deleting key '{key}': {str(e)}" | ||
|
||
def list_keys(self, pattern: str = '*') -> list[str]: | ||
"""List keys in Redis matching a pattern.""" | ||
try: | ||
keys = self.client.keys(pattern) | ||
return [key.decode() for key in keys] | ||
except Exception as e: | ||
return [f"Error listing keys: {str(e)}"] | ||
|
||
def flush_database(self) -> str: | ||
"""Flush the current database.""" | ||
try: | ||
self.client.flushdb() | ||
return 'Database flushed successfully.' | ||
except Exception as e: | ||
return f"Error flushing database: {str(e)}" | ||
|
||
def info(self) -> str: | ||
"""Get Redis server info.""" | ||
try: | ||
info = self.client.info() | ||
return '\n'.join([f"{k}: {v}" for k, v in info.items()]) | ||
except Exception as e: | ||
return f"Error retrieving server info: {str(e)}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters