Redis caching utilities for ML Research Tools.
This module provides utilities for Redis caching, including a Redis cache manager class and functions for common caching operations.
- ml_research_tools.cache.redis.create_redis_client(config)[source]#
Create and return a Redis client based on configuration.
- Parameters:
config (
RedisConfig
) – Redis configuration from the Config object- Return type:
Optional
[Redis
]- Returns:
Redis client instance or None if disabled or connection failed
- ml_research_tools.cache.redis.generate_cache_key(args=None, kwargs=None, prefix='')[source]#
Generate a unique cache key based on input parameters.
- ml_research_tools.cache.redis.get_from_cache(redis_client, cache_key)[source]#
Retrieve data from Redis cache if available.
- ml_research_tools.cache.redis.save_to_cache(redis_client, cache_key, data, ttl)[source]#
Save data to Redis cache with the specified TTL.
- class ml_research_tools.cache.redis.RedisCache(config)[source]#
Bases:
object
Redis cache manager for ML Research Tools.
This class provides a simple interface for Redis caching operations, including serialization and deserialization of complex Python objects.
Example
from ml_research_tools.config import get_config from ml_research_tools.cache import RedisCache config = get_config() cache = RedisCache(config.redis) # Cache a Python object data = {"results": [1, 2, 3]} cache.set("my_key", data) # Get it back retrieved = cache.get("my_key")
Initialize Redis cache manager.
- Parameters:
config (
RedisConfig
) – Redis configuration from Config object
- __init__(config)[source]#
Initialize Redis cache manager.
- Parameters:
config (
RedisConfig
) – Redis configuration from Config object
- ml_research_tools.cache.redis.cached(prefix='', ttl=None, key_fn=None)[source]#
Decorator to cache function results in Redis.
- Parameters:
- Return type:
Callable
[[Callable
[...
,TypeVar
(R
)]],Callable
[...
,TypeVar
(R
)]]- Returns:
Decorator function
Example
from ml_research_tools.cache.redis import cached from ml_research_tools.config import get_config config = get_config() @cached(prefix="expensive_computation", ttl=3600) def expensive_computation(a, b, c): # ... some expensive calculation return result