Source code for langchain.storage.redis
from typing import Any, Iterator, List, Optional, Sequence, Tuple, cast
from langchain.schema import BaseStore
from langchain.utilities.redis import get_client
[docs]class RedisStore(BaseStore[str, bytes]):
    """BaseStore implementation using Redis as the underlying store.
    Examples:
        Create a RedisStore instance and perform operations on it:
        .. code-block:: python
            # Instantiate the RedisStore with a Redis connection
            from langchain.storage import RedisStore
            from langchain.utilities.redis import get_client
            client = get_client('redis://localhost:6379')
            redis_store = RedisStore(client)
            # Set values for keys
            redis_store.mset([("key1", b"value1"), ("key2", b"value2")])
            # Get values for keys
            values = redis_store.mget(["key1", "key2"])
            # [b"value1", b"value2"]
            # Delete keys
            redis_store.mdelete(["key1"])
            # Iterate over keys
            for key in redis_store.yield_keys():
                print(key)
    """
[docs]    def __init__(
        self,
        *,
        client: Any = None,
        redis_url: Optional[str] = None,
        client_kwargs: Optional[dict] = None,
        ttl: Optional[int] = None,
        namespace: Optional[str] = None,
    ) -> None:
        """Initialize the RedisStore with a Redis connection.
        Must provide either a Redis client or a redis_url with optional client_kwargs.
        Args:
            client: A Redis connection instance
            redis_url: redis url
            client_kwargs: Keyword arguments to pass to the Redis client
            ttl: time to expire keys in seconds if provided,