When using the module that comes with django to handle the redis cache, the set key is, for example, "banners". When you see the name in the redis cli, it becomes ":1:banners". This is the mechanism of django. , or redis
from django.core.cache import cache
def get_queryset(self):
key = "banners"
queryset = cache.get(key)
if not queryset:
queryset = Banner.objects.filter(valid=True)
cache.set(key, queryset, 3600)
return queryset
When configuring redis yourself, you will choose the redis library. Redis itself is divided into several libraries, it depends on which one you configure
This is the mechanism of Django cache. The key consists of prefix, version number and real key. Django is useful when upgrading or refactoring code. You can determine which version number the key is for compatibility (other additions are welcome)
1. First look at the implementation of django redis client api:
It uses the make_key function to create a new key. The real key comes from _backend.key_func.
2. Next, check out the django cache backend implementation
It can be seen that the key of django redis cache is composed of prefix, version number and real key.