Skip to content

Commit c87bb8b

Browse files
committed
adjust backend's context manager protocol with glide
1 parent 78cb40d commit c87bb8b

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

aiocache/backends/valkey.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import logging
22
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+
)
614
from glide.exceptions import RequestError as IncrbyException
7-
from glide.protobuf.command_request_pb2 import RequestType
815

916
from aiocache.base import BaseCache, API
1017
from aiocache.serializers import JsonSerializer
@@ -194,10 +201,12 @@ class ValkeyCache(ValkeyBackend):
194201

195202
def __init__(
196203
self,
197-
client: GlideClient,
204+
client: Optional[GlideClient] = None,
198205
serializer: Optional["BaseSerializer"] = None,
199206
namespace: str = "",
200207
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,
201210
**kwargs: Any,
202211
):
203212
super().__init__(
@@ -207,6 +216,17 @@ def __init__(
207216
key_builder=key_builder,
208217
**kwargs,
209218
)
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()
210230

211231
@classmethod
212232
def parse_uri_path(cls, path):

0 commit comments

Comments
 (0)