Skip to content

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

@rjnocelli

Description

@rjnocelli

cached 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()
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions