Skip to content

Commit 76269c8

Browse files
committed
refactore cache instantiation
only context manager is supported to instantiate ValkeyCache
1 parent 6f2b231 commit 76269c8

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

aiocache/backends/valkey.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import time
3-
from typing import Any, Callable, Optional, TYPE_CHECKING, List
3+
from typing import Any, Callable, List, Optional, Self, TYPE_CHECKING
44

55
from glide import (
66
ConditionalChange,
@@ -24,15 +24,6 @@
2424

2525

2626
class ValkeyBackend(BaseCache[str]):
27-
def __init__(
28-
self,
29-
client: GlideClient,
30-
**kwargs,
31-
):
32-
super().__init__(**kwargs)
33-
34-
self.client = client
35-
3627
async def _get(self, key, encoding="utf-8", _conn=None):
3728
value = await self.client.get(key)
3829
if encoding is None or value is None:
@@ -203,31 +194,33 @@ class ValkeyCache(ValkeyBackend):
203194

204195
def __init__(
205196
self,
206-
client: Optional[GlideClient] = None,
207197
serializer: Optional["BaseSerializer"] = None,
208198
namespace: str = "",
209199
key_builder: Callable[[str, str], str] = lambda k, ns: f"{ns}:{k}" if ns else k,
210-
backend: type[GlideClient] = GlideClient,
211200
config: GlideClientConfiguration = None,
212201
**kwargs: Any,
213202
):
214203
super().__init__(
215-
client=client,
216204
serializer=serializer or JsonSerializer(),
217205
namespace=namespace,
218206
key_builder=key_builder,
219207
**kwargs,
220208
)
221-
self.backend = backend
222209
self.config = config
223210

224-
async def __aenter__(self):
211+
async def __aenter__(self) -> Self:
225212
if not self.config:
226213
raise AttributeError("Configuration must be provided for context manager")
227-
self.client = await self.backend.create(config=self.config)
214+
self.client = await self._connect(self.config)
228215
return self
229216

230-
async def __aexit__(self, *args, **kwargs):
217+
async def __aexit__(self, *args, **kwargs) -> None:
218+
await self._disconnect()
219+
220+
async def _connect(self, config: GlideClientConfiguration) -> GlideClient:
221+
return await GlideClient.create(config=config)
222+
223+
async def _disconnect(self) -> None:
231224
await self.client.close()
232225

233226
@classmethod

0 commit comments

Comments
 (0)