RedisStore#

class langchain_community.storage.redis.RedisStore(*, client: Any | None = None, redis_url: str | None = None, client_kwargs: dict | None = None, ttl: int | None = None, namespace: str | None = None)[source]#

BaseStore implementation using Redis as the underlying store.

Examples

Create a RedisStore instance and perform operations on it:

# Instantiate the RedisStore with a Redis connection
from langchain_community.storage import RedisStore
from langchain_community.utilities.redis import get_client

client = get_client('redis://localhost:6379')
redis_store = RedisStore(client=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)  # noqa: T201

Initialize the RedisStore with a Redis connection.

Must provide either a Redis client or a redis_url with optional client_kwargs.

Parameters:
  • client (Any) โ€“ A Redis connection instance

  • redis_url (str | None) โ€“ redis url

  • client_kwargs (dict | None) โ€“ Keyword arguments to pass to the Redis client

  • ttl (int | None) โ€“ time to expire keys in seconds if provided, if None keys will never expire

  • namespace (str | None) โ€“ if provided, all keys will be prefixed with this namespace

Methods

__init__(*[,ย client,ย redis_url,ย ...])

Initialize the RedisStore with a Redis connection.

amdelete(keys)

Async delete the given keys and their associated values.

amget(keys)

Async get the values associated with the given keys.

amset(key_value_pairs)

Async set the values for the given keys.

ayield_keys(*[,ย prefix])

Async get an iterator over keys that match the given prefix.

mdelete(keys)

Delete the given keys.

mget(keys)

Get the values associated with the given keys.

mset(key_value_pairs)

Set the given key-value pairs.

yield_keys(*[,ย prefix])

Yield keys in the store.

__init__(*, client: Any | None = None, redis_url: str | None = None, client_kwargs: dict | None = None, ttl: int | None = None, namespace: str | None = None) None[source]#

Initialize the RedisStore with a Redis connection.

Must provide either a Redis client or a redis_url with optional client_kwargs.

Parameters:
  • client (Any | None) โ€“ A Redis connection instance

  • redis_url (str | None) โ€“ redis url

  • client_kwargs (dict | None) โ€“ Keyword arguments to pass to the Redis client

  • ttl (int | None) โ€“ time to expire keys in seconds if provided, if None keys will never expire

  • namespace (str | None) โ€“ if provided, all keys will be prefixed with this namespace

Return type:

None

async amdelete(keys: Sequence[K]) None#

Async delete the given keys and their associated values.

Parameters:

keys (Sequence[K]) โ€“ A sequence of keys to delete.

Return type:

None

async amget(keys: Sequence[K]) List[V | None]#

Async get the values associated with the given keys.

Parameters:

keys (Sequence[K]) โ€“ A sequence of keys.

Returns:

A sequence of optional values associated with the keys. If a key is not found, the corresponding value will be None.

Return type:

List[V | None]

async amset(key_value_pairs: Sequence[Tuple[K, V]]) None#

Async set the values for the given keys.

Parameters:

key_value_pairs (Sequence[Tuple[K, V]]) โ€“ A sequence of key-value pairs.

Return type:

None

async ayield_keys(*, prefix: str | None = None) AsyncIterator[K] | AsyncIterator[str]#

Async get an iterator over keys that match the given prefix.

Parameters:

prefix (str) โ€“ The prefix to match.

Yields:

Iterator[K | str] โ€“ An iterator over keys that match the given prefix. This method is allowed to return an iterator over either K or str depending on what makes more sense for the given store.

Return type:

AsyncIterator[K] | AsyncIterator[str]

mdelete(keys: Sequence[str]) None[source]#

Delete the given keys.

Parameters:

keys (Sequence[str]) โ€“

Return type:

None

mget(keys: Sequence[str]) List[bytes | None][source]#

Get the values associated with the given keys.

Parameters:

keys (Sequence[str]) โ€“

Return type:

List[bytes | None]

mset(key_value_pairs: Sequence[Tuple[str, bytes]]) None[source]#

Set the given key-value pairs.

Parameters:

key_value_pairs (Sequence[Tuple[str, bytes]]) โ€“

Return type:

None

yield_keys(*, prefix: str | None = None) Iterator[str][source]#

Yield keys in the store.

Parameters:

prefix (str | None) โ€“

Return type:

Iterator[str]

Examples using RedisStore