Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 22 additions & 4 deletions libs/langchain/langchain/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,25 @@ def __init__(self, database_path: str = ".langchain.db"):
class RedisCache(BaseCache):
"""Cache that uses Redis as a backend."""

# TODO - implement a TTL policy in Redis

def __init__(self, redis_: Any):
"""Initialize by passing in Redis instance."""
def __init__(self, redis_: Any, ttl: int = 0):
"""
Initialize an instance of RedisCache.

This method initializes an object with Redis caching capabilities.
It takes a `redis_` parameter, which should be an instance of a Redis
client class, allowing the object to interact with a Redis
server for caching purposes.

Parameters:
redis_ (Any): An instance of a Redis client class
(e.g., redis.Redis) used for caching.
This allows the object to communicate with a
Redis server for caching operations.
ttl (int, optional): Time-to-live (TTL) for cached items in seconds.
If provided, it sets
the default time duration for how long cached items will remain valid.
Defaults to 0, indicating no automatic expiration.
"""
try:
from redis import Redis
except ImportError:
Expand All @@ -230,6 +245,7 @@ def __init__(self, redis_: Any):
if not isinstance(redis_, Redis):
raise ValueError("Please pass in Redis object.")
self.redis = redis_
self.ttl = ttl

def _key(self, prompt: str, llm_string: str) -> str:
"""Compute key from prompt and llm_string"""
Expand Down Expand Up @@ -267,6 +283,8 @@ def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> N
str(idx): generation.text for idx, generation in enumerate(return_val)
},
)
if self.ttl > 0:
self.redis.expire(key, self.ttl)

def clear(self, **kwargs: Any) -> None:
"""Clear cache. If `asynchronous` is True, flush asynchronously."""
Expand Down
12 changes: 12 additions & 0 deletions libs/langchain/tests/integration_tests/cache/test_redis_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
REDIS_TEST_URL = "redis://localhost:6379"


def test_redis_cache_ttl() -> None:
import time

import redis

langchain.llm_cache = RedisCache(redis_=redis.Redis.from_url(REDIS_TEST_URL), ttl=1)
langchain.llm_cache.update("foo", "bar", [Generation(text="fizz")])
key = langchain.llm_cache._key("foo", "bar")
time.sleep(1.1)
assert langchain.llm_cache.redis.hgetall(key) == {}


def test_redis_cache() -> None:
import redis

Expand Down