Skip to content

Commit a103043

Browse files
committed
refactor(redis): simplify Redis client initialization logic
- Remove redundant `redis_factory` parameter in favor of `factory` - Update documentation to reflect the change from `redis_factory` to `factory` - Adjust client initialization logic to prioritize `factory` over `client` - Add deprecation warning for callable `client` usage - Ensure backward compatibility for existing callable `client` usage - Improve error handling for invalid `factory` types - Update example code to use `factory` instead of `redis_factory`
1 parent aa579df commit a103043

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/redis_func_cache/cache.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ class RedisFuncCache(Generic[RedisClientTV]):
7878
pool = redis.ConnectionPool(...)
7979
factory = redis.from_pool(pool)
8080
81-
# supply a client instance and (optionally) a factory
82-
cache = RedisFuncCache(__name__, LruTPolicy(), client=redis.Redis(), redis_factory=factory)
81+
# supply a client instance by a factory
82+
cache = RedisFuncCache(__name__, LruTPolicy(), factory=factory)
8383
8484
@cache
8585
def function_to_cache(...):
@@ -161,7 +161,7 @@ def __init__(
161161
client: Optional Redis client instance to use.
162162
163163
This argument may be an already-created Redis client instance (for
164-
simple scripts/tests), or ``None`` when a ``redis_factory`` is supplied.
164+
simple scripts/tests), or ``None`` when a ``factory`` is supplied.
165165
166166
Examples of a client instance:
167167
@@ -275,21 +275,24 @@ def my_deserializer(data):
275275
# and treating it as a factory.
276276
self._redis_client_instance: Optional[RedisClientTV] = None
277277
self._redis_client_factory: Optional[Callable[[], RedisClientTV]] = None
278-
if client is not None: # pragma: no cover
278+
# explicit factory parameter overrides instance when present
279+
if factory is not None:
280+
if not callable(factory):
281+
raise TypeError("`factory` must be a callable")
282+
self._redis_client_factory = factory
283+
elif client is not None: # pragma: no cover
284+
# backward compatibility
279285
if callable(client):
280286
warn(
281287
"Passing a callable as `client` is deprecated; use `factory=` instead",
282288
DeprecationWarning,
283289
)
284-
# type: ignore[assignment]
285-
self._redis_client_factory = client # backward compatibility
290+
self._redis_client_factory = client # type: ignore[assignment]
286291
else:
287292
self._redis_client_instance = client
288-
# explicit redis_factory parameter overrides instance when present
289-
if factory is not None:
290-
self._redis_client_factory = factory
291-
if self._redis_client_factory is None and self._redis_client_instance is None:
293+
else:
292294
raise RuntimeError("Either `client` or `factory` must be provided.")
295+
# other arguments
293296
self.serializer = serializer
294297
self._mode: ContextVar[RedisFuncCache.Mode] = ContextVar("mode", default=RedisFuncCache.Mode())
295298
self._stats: ContextVar[Optional[RedisFuncCache.Stats]] = ContextVar("stats", default=None)

0 commit comments

Comments
 (0)