Redis Manager API

Complete API reference for the RouteMQ Redis Manager class and Redis integration patterns.

RedisManager Class

The RedisManager class provides async Redis operations with connection pooling, error handling, and graceful fallback when Redis is unavailable. It implements the singleton pattern to ensure one Redis connection per application.

Import

from core.redis_manager import redis_manager

Global Instance

RouteMQ provides a pre-configured global Redis manager instance:

# Use the global instance (recommended)
from core.redis_manager import redis_manager

# Or create your own instance
from core.redis_manager import RedisManager
custom_redis = RedisManager()

Configuration

Configure Redis through environment variables:

Connection Management

initialize()

Initialize Redis connection pool and test connectivity.

Signature:

Returns: bool - True if connection successful, False otherwise

Example:

disconnect()

Close Redis connections and clean up resources.

Signature:

Example:

is_enabled()

Check if Redis is enabled and available.

Signature:

Returns: bool - True if Redis is enabled and connected

Example:

get_client()

Get the underlying Redis client instance for advanced operations.

Signature:

Returns: Redis client instance or None if not enabled

Example:

Basic Operations

get(key)

Get value by key.

Signature:

Parameters:

  • key (str): Redis key

Returns: str | None - Value as string or None if not found

Example:

set(key, value, ex=None, px=None, nx=False, xx=False)

Set key-value pair with optional expiration and conditions.

Signature:

Parameters:

  • key (str): Redis key

  • value (str | int | float): Value to set

  • ex (int, optional): Expire time in seconds

  • px (int, optional): Expire time in milliseconds

  • nx (bool): Only set if key doesn't exist

  • xx (bool): Only set if key exists

Returns: bool - True if successful

Example:

delete(*keys)

Delete one or more keys.

Signature:

Parameters:

  • keys (str): One or more keys to delete

Returns: int - Number of keys deleted

Example:

exists(key)

Check if key exists.

Signature:

Parameters:

  • key (str): Redis key to check

Returns: bool - True if key exists

Example:

expire(key, time)

Set expiration time for a key.

Signature:

Parameters:

  • key (str): Redis key

  • time (int): Expiration time in seconds

Returns: bool - True if successful

Example:

ttl(key)

Get time to live for a key.

Signature:

Parameters:

  • key (str): Redis key

Returns: int - TTL in seconds, -1 if no expiry, -2 if key doesn't exist

Example:

Numeric Operations

incr(key, amount=1)

Increment key value by amount.

Signature:

Parameters:

  • key (str): Redis key

  • amount (int): Amount to increment (default: 1)

Returns: int | None - New value or None if error

Example:

Hash Operations

hset(name, key=None, value=None, mapping=None)

Set hash field(s).

Signature:

Parameters:

  • name (str): Hash name

  • key (str): Field key (for single field)

  • value (str): Field value (for single field)

  • mapping (dict): Dictionary of field-value pairs

Returns: int - Number of fields added

Example:

hget(name, key)

Get hash field value.

Signature:

Parameters:

  • name (str): Hash name

  • key (str): Field key

Returns: str | None - Field value or None

Example:

JSON Operations

set_json(key, value, ex=None, px=None, nx=False, xx=False)

Serialize and store JSON data.

Signature:

Parameters:

  • key (str): Redis key

  • value (Any): Value to serialize as JSON

  • Other parameters same as set()

Returns: bool - True if successful

Example:

get_json(key)

Retrieve and deserialize JSON data.

Signature:

Parameters:

  • key (str): Redis key

Returns: Any | None - Deserialized value or None

Example:

Common Usage Patterns

Caching

Session Management

Rate Limiting

Distributed Locking

Pub/Sub Messaging

Error Handling

The Redis Manager handles errors gracefully:

Best Practices

1. Use JSON Methods for Complex Data

2. Set Appropriate Expiration Times

3. Use Descriptive Key Patterns

4. Handle Redis Unavailability

Last updated