1
1
import logging
2
2
import time
3
- from typing import Any , Callable , Optional , TYPE_CHECKING , List , cast
4
-
5
- from glide import GlideClient , Script , Transaction , ExpirySet , ExpiryType , ConditionalChange
3
+ from typing import Any , Callable , Optional , TYPE_CHECKING , List
4
+
5
+ from glide import (
6
+ ConditionalChange ,
7
+ ExpirySet ,
8
+ ExpiryType ,
9
+ GlideClient ,
10
+ GlideClientConfiguration ,
11
+ Script ,
12
+ Transaction ,
13
+ )
6
14
from glide .exceptions import RequestError as IncrbyException
7
- from glide .protobuf .command_request_pb2 import RequestType
8
15
9
16
from aiocache .base import BaseCache , API
10
17
from aiocache .serializers import JsonSerializer
@@ -194,10 +201,12 @@ class ValkeyCache(ValkeyBackend):
194
201
195
202
def __init__ (
196
203
self ,
197
- client : GlideClient ,
204
+ client : Optional [ GlideClient ] = None ,
198
205
serializer : Optional ["BaseSerializer" ] = None ,
199
206
namespace : str = "" ,
200
207
key_builder : Callable [[str , str ], str ] = lambda k , ns : f"{ ns } :{ k } " if ns else k ,
208
+ backend : type [GlideClient ] = GlideClient ,
209
+ config : GlideClientConfiguration = None ,
201
210
** kwargs : Any ,
202
211
):
203
212
super ().__init__ (
@@ -207,6 +216,17 @@ def __init__(
207
216
key_builder = key_builder ,
208
217
** kwargs ,
209
218
)
219
+ self .backend = backend
220
+ self .config = config
221
+
222
+ async def __aenter__ (self ):
223
+ if not self .config :
224
+ raise AttributeError ("Configuration must be provided for context manager" )
225
+ self .client = await self .backend .create (config = self .config )
226
+ return self
227
+
228
+ async def __aexit__ (self , * args , ** kwargs ):
229
+ await self .client .close ()
210
230
211
231
@classmethod
212
232
def parse_uri_path (cls , path ):
0 commit comments