RedisCache#
- class langchain_redis.cache.RedisCache(redis_url: str = 'redis://localhost:6379', ttl: int | None = None, prefix: str | None = 'redis', redis_client: Redis | None = None)[source]#
Redis cache implementation for LangChain.
This class provides a Redis-based caching mechanism for LangChain, allowing storage and retrieval of language model responses.
- ttl#
Time-to-live for cache entries in seconds. If None, entries don’t expire.
- Type:
Optional[int]
- prefix#
Prefix for all keys stored in Redis.
- Type:
Optional[str]
- Parameters:
redis_url (str) – The URL of the Redis instance to connect to. Defaults to “redis://localhost:6379”.
ttl (Optional[int]) – Time-to-live for cache entries in seconds. Defaults to None (no expiration).
prefix (Optional[str]) – Prefix for all keys stored in Redis. Defaults to “redis”.
redis (Optional[Redis]) – An existing Redis client instance. If provided, redis_url is ignored.
redis_client (Optional[Redis])
Example
from langchain_redis import RedisCache from langchain_core.globals import set_llm_cache # Create a Redis cache instance redis_cache = RedisCache(redis_url="redis://localhost:6379", ttl=3600) # Set it as the global LLM cache set_llm_cache(redis_cache) # Now, when you use an LLM, it will automatically use this cache
Note
This cache implementation uses Redis JSON capabilities to store structured data.
The cache key is created using MD5 hashes of the prompt and LLM string.
If TTL is set, cache entries will automatically expire after the specified duration.
The prefix can be used to namespace cache entries.
Methods
__init__
([redis_url, ttl, prefix, redis_client])aclear
(**kwargs)Async clear cache that can take additional keyword arguments.
alookup
(prompt, llm_string)Async look up based on prompt and llm_string.
aupdate
(prompt, llm_string, return_val)Async update cache based on prompt and llm_string.
clear
(**kwargs)Clear all entries in the Redis cache that match the cache prefix.
lookup
(prompt, llm_string)Look up the result of a previous language model call in the Redis cache.
update
(prompt, llm_string, return_val)Update the cache with a new result for a given prompt and language model.
- __init__(redis_url: str = 'redis://localhost:6379', ttl: int | None = None, prefix: str | None = 'redis', redis_client: Redis | None = None)[source]#
- Parameters:
redis_url (str)
ttl (int | None)
prefix (str | None)
redis_client (Redis | None)
- async aclear(**kwargs: Any) None #
Async clear cache that can take additional keyword arguments.
- Parameters:
kwargs (Any)
- Return type:
None
- async alookup(prompt: str, llm_string: str) Sequence[Generation] | None #
Async look up based on prompt and llm_string.
A cache implementation is expected to generate a key from the 2-tuple of prompt and llm_string (e.g., by concatenating them with a delimiter).
- Parameters:
prompt (str) – a string representation of the prompt. In the case of a Chat model, the prompt is a non-trivial serialization of the prompt into the language model.
llm_string (str) – A string representation of the LLM configuration. This is used to capture the invocation parameters of the LLM (e.g., model name, temperature, stop tokens, max tokens, etc.). These invocation parameters are serialized into a string representation.
- Returns:
On a cache miss, return None. On a cache hit, return the cached value. The cached value is a list of Generations (or subclasses).
- Return type:
Sequence[Generation] | None
- async aupdate(prompt: str, llm_string: str, return_val: Sequence[Generation]) None #
Async update cache based on prompt and llm_string.
The prompt and llm_string are used to generate a key for the cache. The key should match that of the look up method.
- Parameters:
prompt (str) – a string representation of the prompt. In the case of a Chat model, the prompt is a non-trivial serialization of the prompt into the language model.
llm_string (str) – A string representation of the LLM configuration. This is used to capture the invocation parameters of the LLM (e.g., model name, temperature, stop tokens, max tokens, etc.). These invocation parameters are serialized into a string representation.
return_val (Sequence[Generation]) – The value to be cached. The value is a list of Generations (or subclasses).
- Return type:
None
- clear(**kwargs: Any) None [source]#
Clear all entries in the Redis cache that match the cache prefix.
This method removes all cache entries that start with the specified prefix.
- Parameters:
**kwargs (Any) – Additional keyword arguments. Currently not used, but included for potential future extensions.
- Returns:
None
- Return type:
None
Example
cache = RedisCache( redis_url="redis://localhost:6379", prefix="my_cache" ) # Add some entries to the cache cache.update("prompt1", "llm1", [Generation(text="Result 1")]) cache.update("prompt2", "llm2", [Generation(text="Result 2")]) # Clear all entries cache.clear() # After this, all entries with keys starting with "my_cache:" # will be removed
Note
This method uses Redis SCAN to iterate over keys, which is safe for large datasets.
It deletes keys in batches of 100 to optimize performance.
Only keys that start with the specified prefix (default is “redis:”) will be deleted.
This operation is irreversible. Make sure you want to clear all cached data before calling this method.
If no keys match the prefix, the method will complete without any errors.
- lookup(prompt: str, llm_string: str) Sequence[Generation] | None [source]#
Look up the result of a previous language model call in the Redis cache.
This method checks if there’s a cached result for the given prompt and language model combination.
- Parameters:
prompt (str) – The input prompt for which to look up the cached result.
llm_string (str) – A string representation of the language model and its parameters.
- Returns:
- The cached result if found, or None if not
present in the cache.
The result is typically a list containing a single Generation object.
- Return type:
Optional[RETURN_VAL_TYPE]
Example
cache = RedisCache(redis_url="redis://localhost:6379") prompt = "What is the capital of France?" llm_string = "openai/gpt-3.5-turbo" result = cache.lookup(prompt, llm_string) if result: print("Cache hit:", result[0].text) else: print("Cache miss")
Note
The method uses an MD5 hash of the prompt and llm_string to create the cache key.
The cached value is stored as JSON and parsed back into a Generation object.
If the key exists but the value is None or cannot be parsed, None is returned.
This method is typically called internally by LangChain, but can be used directly for manual cache interactions.
- update(prompt: str, llm_string: str, return_val: Sequence[Generation]) None [source]#
Update the cache with a new result for a given prompt and language model.
This method stores a new result in the Redis cache for the specified prompt and language model combination.
- Parameters:
prompt (str) – The input prompt associated with the result.
llm_string (str) – A string representation of the language model and its parameters.
return_val (RETURN_VAL_TYPE) – The result to be cached, typically a list containing a single Generation object.
- Returns:
None
- Return type:
None
Example
from langchain_core.outputs import Generation cache = RedisCache(redis_url="redis://localhost:6379", ttl=3600) prompt = "What is the capital of France?" llm_string = "openai/gpt-3.5-turbo" result = [Generation(text="The capital of France is Paris.")] cache.update(prompt, llm_string, result)
Note
The method uses an MD5 hash of the prompt and llm_string to create the cache key.
The result is stored as JSON in Redis.
If a TTL (Time To Live) was specified when initializing the cache, it will be applied to this entry.
This method is typically called internally by LangChain after a language model generates a response, but it can be used directly for manual cache updates.
If the cache already contains an entry for this prompt and llm_string, it will be overwritten.
Examples using RedisCache