[Solved]-How to match redis key patterns using native django cache?

18👍

This is not possible using standard Django’s cache wrapper. As the feature to search keys by pattern is a backend dependent operation and not supported by all the cache backends used by Django (e.g. memcached does not support it but Redis does). So you will have to use a custom cache wrapper with cache backend that supports this operation.

Edit:
If you are already using django-redis then you can do

from django.core.cache import cache
cache.keys("foo_*")

as explained here.

This will return list of keys matching the pattern then you can use cache.get_many() to get values for these keys.

cache.get_many(cache.keys("key_1_*"))

2👍

If the cache has following entries:

cache = {'key_1_3': 'foo', 'key_2_5': 'bar', 'key_1_7': 'baz'}

You can get all the entries which has key key_1_*:

x = {k: v for k, v in cache.items() if k.startswith('key_1')}

Based on the documentation from django-redis

You can list all the keys with a pattern:

>>> from django.core.cache import cache
>>> cache.keys("key_1_*")
# ["key_1_3", "key_1_7"]

once you have the keys you can get the values from this:

>>> [cache.get(k) for k in cache.keys("key_1_*")]
# ['foo', 'baz']

You can also use cache.iter_keys(pattern) for efficient implementation.

Or, as suggested by @Muhammad Tahir, you can use cache.get_many(cache.keys("key_1_*")) to get all the values in one go.

👤AKS

1👍

I saw several answers above mentioning django-redis.

Based on https://pypi.org/project/django-redis/

You can actually use delete_pattern() method

from django.core.cache import cache
cache.delete_pattern('key_1_*')

Avoid using pattern matching like this in production environment. It will drain use your resource quickly. Because the operation is O(N)

From REDIS KEYS docs

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don’t use KEYS in your regular application code. If you’re looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

Leave a comment