aio-libs / aio-libs/aiocache

Add client attribute to ValkeyCache backend class to be used with `cached` decorator

Aperta
#1,065 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Python
Stelle
1.4k
Fork
181
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

[cached](https://github.com/aio-libs/aiocache/blob/master/aiocache/decorators.py#L12) decorator requires backend class to implement `client` property.

I've implemented a quick wrapper (see example) to make it useful for a FastAPI application I am currently working on but it would be nice to have it implemented in the current client. I am happy to take over this task a create a PR. Excuse me if there is an issue/PR already opened regarding this.

```
from glide import GlideClientConfiguration, NodeAddress, GlideClient
from aiocache import ValkeyCache as AiocacheValkeyCache
from aiocache.serializers import PickleSerializer

class ValkeyCache(AiocacheValkeyCache):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client = None

async def connect(self):
self.client = await self._init_async_client()
logger.info("Connected to Valkey.")

async def _init_async_client(self):
client = await GlideClient.create(self.config)
return client

async def disconnect(self):
if self.client:
logger.info("Disconnecting from Valkey...")
await self.client.close()
logger.info("Disconnected from Valkey.")
self.client = None

valkey_cache = ValkeyCache(
config=valkey_config, namespace="main", serializer=PickleSerializer()
)
```

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.