Skip to content

Commit 6576968

Browse files
committed
clean up call_with_cache
1 parent 3019ff9 commit 6576968

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

python/lib/sift_client/_internal/low_level_wrappers/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from abc import ABC
44
from typing import Any, Callable, TypeVar
55

6-
from sift_py.grpc.cache import ignore_cache, with_cache, with_force_refresh
6+
from sift_py.grpc.cache import with_cache, with_force_refresh
77

88
T = TypeVar("T")
99

@@ -56,7 +56,7 @@ async def _handle_pagination(
5656
return results
5757

5858
@staticmethod
59-
async def _call_with_cache(
59+
async def call_with_cache(
6060
stub_method: Callable[[Any, tuple[tuple[str, str], ...]], T],
6161
request: Any,
6262
*,
@@ -109,12 +109,12 @@ async def _call_with_cache(
109109
use_cache=False,
110110
)
111111
"""
112+
if not use_cache:
113+
return await stub_method(request)
112114

113115
if force_refresh:
114116
metadata = with_force_refresh(ttl=ttl)
115-
elif use_cache:
116-
metadata = with_cache(ttl=ttl)
117117
else:
118-
metadata = ignore_cache()
118+
metadata = with_cache(ttl=ttl)
119119

120120
return await stub_method(request, metadata=metadata)

python/lib/sift_client/transport/grpc_transport.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def __init__(self, config: GrpcConfig):
179179
self._stubs_async_map: dict[asyncio.AbstractEventLoop, dict[type[Any], Any]] = {}
180180

181181
# Initialize cache if caching is enabled
182-
self._cache = self._init_cache()
182+
self.cache = self._init_cache()
183183

184184
# default loop for sync API
185185
self._default_loop = asyncio.new_event_loop()
@@ -224,6 +224,10 @@ def _init_cache(self) -> GrpcCache | None:
224224
logger.warning(f"Failed to initialize cache: {e}")
225225
return None
226226

227+
@property
228+
def has_cache(self):
229+
return self.cache is not None
230+
227231
@property
228232
def default_loop(self) -> asyncio.AbstractEventLoop:
229233
"""Return the default event loop used for synchronous API operations.
@@ -246,7 +250,7 @@ def get_stub(self, stub_class: type[Any]) -> Any:
246250

247251
if loop not in self._channels_async:
248252
channel = use_sift_async_channel(
249-
self._config._to_sift_channel_config(), self._config.metadata, self._cache
253+
self._config._to_sift_channel_config(), self._config.metadata, self.cache
250254
)
251255
self._channels_async[loop] = channel
252256
self._stubs_async_map[loop] = {}
@@ -289,4 +293,4 @@ async def _create_async_channel(
289293
self, cfg: SiftChannelConfig, metadata: dict[str, str] | None
290294
) -> Any:
291295
"""Helper to create async channel on default loop."""
292-
return use_sift_async_channel(cfg, metadata, self._cache)
296+
return use_sift_async_channel(cfg, metadata, self.cache)

0 commit comments

Comments
 (0)