Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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: 18 additions & 8 deletions redisvl/extensions/cache/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import asyncio
from collections.abc import Mapping
from typing import Any, cast
from typing import Any, TypedDict

from redis import Redis # For backwards compatibility in type checking
from redis.cluster import RedisCluster
Expand All @@ -15,6 +15,18 @@
from redisvl.types import AsyncRedisClient, SyncRedisClient


class _CacheConnectionKwargs(TypedDict):
"""The connection parameters a cache keeps for building its clients.

Annotated so each value keeps its own type. Without it the dict literal
widens to the union of all three, and every read needs a cast.
"""

redis_client: SyncRedisClient | None
redis_url: str
connection_kwargs: dict[str, Any]


class BaseCache:
"""Base abstract cache interface for all RedisVL caches.

Expand Down Expand Up @@ -51,7 +63,7 @@ def __init__(
self._ttl: int | None = None
self.set_ttl(ttl)

self.redis_kwargs = {
self.redis_kwargs: _CacheConnectionKwargs = {
"redis_client": redis_client,
"redis_url": redis_url,
"connection_kwargs": connection_kwargs,
Expand Down Expand Up @@ -121,8 +133,8 @@ def _get_redis_client(self) -> SyncRedisClient:
"""
if self._redis_client is None:
# Create new Redis client
url = cast(str | None, self.redis_kwargs["redis_url"])
kwargs = cast(dict[str, Any], self.redis_kwargs["connection_kwargs"])
url = self.redis_kwargs["redis_url"]
kwargs = self.redis_kwargs["connection_kwargs"]
self._redis_client = RedisConnectionFactory.get_redis_connection(
redis_url=url,
**kwargs,
Expand All @@ -146,10 +158,8 @@ async def _get_async_redis_client(self) -> AsyncRedisClient:
if provided and isinstance(provided, (Redis, RedisCluster)):
client = RedisConnectionFactory.sync_to_async_redis(provided)
else:
url = cast(str | None, self.redis_kwargs["redis_url"])
kwargs = cast(
dict[str, Any], self.redis_kwargs["connection_kwargs"]
)
url = self.redis_kwargs["redis_url"]
kwargs = self.redis_kwargs["connection_kwargs"]
client = await RedisConnectionFactory._get_aredis_connection(
redis_url=url, **kwargs
)
Expand Down
5 changes: 1 addition & 4 deletions redisvl/extensions/router/semantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ def from_existing(
overwrite = kwargs.pop("overwrite", False)
if not create_index and overwrite:
raise ValueError(CREATE_INDEX_OVERWRITE_CONFLICT)
init_kwargs, connection_kwargs = _split_from_existing_kwargs(
dict(kwargs),
nested_connection_keys=("connection_kwargs",),
)
init_kwargs, connection_kwargs = _split_from_existing_kwargs(dict(kwargs))
lib_name = init_kwargs.get("lib_name")
index_kwargs: dict[str, Any] = {}
created_redis_client = False
Expand Down
Loading
Loading