@@ -17,6 +17,9 @@ class BaseCache:
1717 including TTL management, connection handling, and basic cache operations.
1818 """
1919
20+ _redis_client : Optional [Redis ]
21+ _async_redis_client : Optional [AsyncRedis ]
22+
2023 def __init__ (
2124 self ,
2225 name : str ,
@@ -47,12 +50,15 @@ def __init__(
4750 "connection_kwargs" : connection_kwargs ,
4851 }
4952
53+ # Initialize Redis clients
54+ self ._async_redis_client = None
55+
5056 if redis_client :
5157 self ._owns_redis_client = False
5258 self ._redis_client = redis_client
5359 else :
5460 self ._owns_redis_client = True
55- self ._redis_client = None
61+ self ._redis_client = None # type: ignore
5662
5763 def _get_prefix (self ) -> str :
5864 """Get the key prefix for Redis keys.
@@ -102,9 +108,10 @@ def _get_redis_client(self) -> Redis:
102108 Redis: A Redis client instance.
103109 """
104110 if self ._redis_client is None :
105- self ._redis_client = Redis .from_url (
106- self .redis_kwargs ["redis_url" ], ** self .redis_kwargs ["connection_kwargs" ]
107- )
111+ # Create new Redis client
112+ url = self .redis_kwargs ["redis_url" ]
113+ kwargs = self .redis_kwargs ["connection_kwargs" ]
114+ self ._redis_client = Redis .from_url (url , ** kwargs ) # type: ignore
108115 return self ._redis_client
109116
110117 async def _get_async_redis_client (self ) -> AsyncRedis :
@@ -114,9 +121,10 @@ async def _get_async_redis_client(self) -> AsyncRedis:
114121 AsyncRedis: An async Redis client instance.
115122 """
116123 if not hasattr (self , "_async_redis_client" ) or self ._async_redis_client is None :
117- self ._async_redis_client = AsyncRedis .from_url (
118- self .redis_kwargs ["redis_url" ], ** self .redis_kwargs ["connection_kwargs" ]
119- )
124+ # Create new async Redis client
125+ url = self .redis_kwargs ["redis_url" ]
126+ kwargs = self .redis_kwargs ["connection_kwargs" ]
127+ self ._async_redis_client = AsyncRedis .from_url (url , ** kwargs ) # type: ignore
120128 return self ._async_redis_client
121129
122130 def expire (self , key : str , ttl : Optional [int ] = None ) -> None :
@@ -161,25 +169,29 @@ def clear(self) -> None:
161169 prefix = self ._get_prefix ()
162170
163171 # Scan for all keys with our prefix
164- cursor = "0"
165- while cursor != 0 :
166- cursor , keys = client .scan (cursor = cursor , match = f"{ prefix } *" , count = 100 )
172+ cursor = 0 # Start with cursor 0
173+ while True :
174+ cursor_int , keys = client .scan (cursor = cursor , match = f"{ prefix } *" , count = 100 ) # type: ignore
167175 if keys :
168176 client .delete (* keys )
177+ if cursor_int == 0 : # Redis returns 0 when scan is complete
178+ break
179+ cursor = cursor_int # Update cursor for next iteration
169180
170181 async def aclear (self ) -> None :
171182 """Async clear the cache of all keys."""
172183 client = await self ._get_async_redis_client ()
173184 prefix = self ._get_prefix ()
174185
175186 # Scan for all keys with our prefix
176- cursor = "0"
177- while cursor != 0 :
178- cursor , keys = await client .scan (
179- cursor = cursor , match = f"{ prefix } *" , count = 100
180- )
187+ cursor = 0 # Start with cursor 0
188+ while True :
189+ cursor_int , keys = await client .scan (cursor = cursor , match = f"{ prefix } *" , count = 100 ) # type: ignore
181190 if keys :
182191 await client .delete (* keys )
192+ if cursor_int == 0 : # Redis returns 0 when scan is complete
193+ break
194+ cursor = cursor_int # Update cursor for next iteration
183195
184196 def disconnect (self ) -> None :
185197 """Disconnect from Redis."""
@@ -188,11 +200,12 @@ def disconnect(self) -> None:
188200
189201 if self ._redis_client :
190202 self ._redis_client .close ()
191- self ._redis_client = None
203+ self ._redis_client = None # type: ignore
192204
193205 if hasattr (self , "_async_redis_client" ) and self ._async_redis_client :
194- self ._async_redis_client .close ()
195- self ._async_redis_client = None
206+ # Use synchronous close for async client in synchronous context
207+ self ._async_redis_client .close () # type: ignore
208+ self ._async_redis_client = None # type: ignore
196209
197210 async def adisconnect (self ) -> None :
198211 """Async disconnect from Redis."""
@@ -201,8 +214,9 @@ async def adisconnect(self) -> None:
201214
202215 if self ._redis_client :
203216 self ._redis_client .close ()
204- self ._redis_client = None
217+ self ._redis_client = None # type: ignore
205218
206219 if hasattr (self , "_async_redis_client" ) and self ._async_redis_client :
207- await self ._async_redis_client .aclose ()
208- self ._async_redis_client = None
220+ # Use proper async close method
221+ await self ._async_redis_client .aclose () # type: ignore
222+ self ._async_redis_client = None # type: ignore
0 commit comments