Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to delete cache with specified prefix #910

Open
elkon028 opened this issue Oct 11, 2024 · 2 comments
Open

How to delete cache with specified prefix #910

elkon028 opened this issue Oct 11, 2024 · 2 comments

Comments

@elkon028
Copy link

elkon028 commented Oct 11, 2024

cache = Cache(Cache.REDIS, endpoint="127.0.0.1", port=6379, namespace="main")
await cache.set('key:1', 1)
await cache.set('key:2', 2)
await cache.set('key:3', 3)

# delete prefix 'key:' ????
await cache.delete("key:") ??
await cache.delete("key:*") ??
@alessio-locatelli
Copy link

alessio-locatelli commented Oct 11, 2024

Did you overlook clear()?

But if you want to delete multiple keys from different namespaces or something more complex, then prepare a list of the keys and call https://redis.io/docs/latest/commands/unlink/

@elkon028
Copy link
Author

Did you overlook clear()?

But if you want to delete multiple keys from different namespaces or something more complex, then prepare a list of the keys and call https://redis.io/docs/latest/commands/unlink/

I don't have a better solution
I just extended 'RedisCache'

from aiocache import RedisCache, caches
from aiocache.base import API

from internal.core.config import settings


class RedisPlusCache(RedisCache):
    def __init__(self, serializer=None, **kwargs):
        super().__init__(serializer=serializer, **kwargs)

    @API.register
    @API.aiocache_enabled(fake_return=0)
    @API.timeout
    @API.plugins
    async def remove(self, key, namespace=None):
        ns = namespace if namespace is not None else self.namespace
        ns_key = self.build_key(key, namespace=ns)
        keys = await self.client.keys(ns_key)
        if keys:
            await self.client.delete(*keys)


caches.set_config({
    'default': {
        'cache': 'aiocache.SimpleMemoryCache',
        'namespace': settings.REDIS_PREFIX,
        'serializer': {'class': 'aiocache.serializers.PickleSerializer'},
    },
    'redis': {
        'cache': 'internal.core.RedisPlusCache',
        'endpoint': settings.REDIS_HOST,
        'port': settings.REDIS_PORT,
        'timeout': settings.REDIS_TIMEOUT,
        'namespace': settings.REDIS_PREFIX,
        'serializer': {'class': 'aiocache.serializers.PickleSerializer'},
        'plugins': [
            {'class': 'aiocache.plugins.HitMissRatioPlugin'},
            {'class': 'aiocache.plugins.TimingPlugin'},
        ],
    },
})


memoryCache = caches.get('default')
redisCache = caches.get('redis')
redisCache.set('key:1', 1)
redisCache.set('key:2', 2)
redisCache.set('key:3', 3)

redisCache.remove('key:*')  # It succeeded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants