Cache - What is the significance of the serial number added before the name of the redis key set with django cache?
给我你的怀抱
给我你的怀抱 2017-06-10 09:48:11
0
2
1755

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
给我你的怀抱
给我你的怀抱

reply all(2)
伊谢尔伦

When configuring redis yourself, you will choose the redis library. Redis itself is divided into several libraries, it depends on which one you configure

过去多啦不再A梦

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:

# https://github.com/niwinz/django-redis/blob/master/django_redis/client/default.py
def get(self, key, default=None, version=None, client=None):
    """
    Retrieve a value from the cache.
    Returns decoded value if key is found, the default if not.
    """
    if client is None:
        client = self.get_client(write=False)

    key = self.make_key(key, version=version)

    try:
        value = client.get(key)
    except _main_exceptions as e:
        raise ConnectionInterrupted(connection=client, parent=e)

    if value is None:
        return default

    return self.decode(value)

... 中间省略代码 ...


def make_key(self, key, version=None, prefix=None):
    if isinstance(key, CacheKey):
        return key

    if prefix is None:
        prefix = self._backend.key_prefix

    if version is None:
        version = self._backend.version

    return CacheKey(self._backend.key_func(key, prefix, version))
  

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

# https://github.com/django/django/blob/master/django/core/cache/backends/base.py
def default_key_func(key, key_prefix, version):
    """
    Default function to generate keys.

    Constructs the key used by all other methods. By default it prepends
    the `key_prefix'. KEY_FUNCTION can be used to specify an alternate
    function with custom key making behavior.
    """
    return '%s:%s:%s' % (key_prefix, version, key)


def get_key_func(key_func):
    """
    Function to decide which key function to use.

    Defaults to ``default_key_func``.
    """
    if key_func is not None:
        if callable(key_func):
            return key_func
        else:
            return import_string(key_func)
    return default_key_func
    
class BaseCache(object):
    def __init__(self, params):
    
        ... 中间省略代码 ...
        
        self.key_prefix = params.get('KEY_PREFIX', '')
        self.version = params.get('VERSION', 1)
        self.key_func = get_key_func(params.get('KEY_FUNCTION'))
    

It can be seen that the key of django redis cache is composed of prefix, version number and real key.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template