-
Notifications
You must be signed in to change notification settings - Fork 154
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
Comments
Did you overlook 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 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 |
The text was updated successfully, but these errors were encountered: