Jand
Jand

Reputation: 2727

How to match redis key patterns using native django cache?

I have a series of caches which follow this pattern:

key_x_y = value

Like:

  'key_1_3' = 'foo'
  'key_2_5' = 'bar'
  'key_1_7' = 'baz'

Now I'm wondering how can I iterate over all keys to match pattern like key_1_* to get foo and baz using the native django cache.get()?

(I know that there are way, particularly for redis, that allow using more extensive api like iterate, but I'd like to stick to vanilla django cache, if possible)

Upvotes: 16

Views: 13709

Answers (3)

Rizky Arlin
Rizky Arlin

Reputation: 383

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.

Upvotes: 1

Muhammad Tahir
Muhammad Tahir

Reputation: 5184

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_*"))

Upvotes: 25

AKS
AKS

Reputation: 19861

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.

Upvotes: 2

Related Questions