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.
- Running django in virtualenv – ImportError: No module named django.core.management?
- Django, Cannot assign None, does not allow null values
- Django per user view caching
- In Django admin, can I require fields in a model but not when it is inline?
- Celery chain breaks if one of the tasks fail
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.
- Django – Things to know about sorl-thumbnail
- Get a queryset's current order_by ordering
- Docker + Django + Postgres Add-on + Heroku
- Django – Access fields on a model's "through" table from an instance
- Adding static() to urlpatterns only work by appending to the list