2

If I only set and get David with version=0, then I can get John and David in order as shown below. *I use LocMemCache which is the default cache in Django and I'm learning Django Cache:

from django.core.cache import cache

cache.set("name", "John")
cache.set("name", "David", version=0)
print(cache.get("name")) # John
print(cache.get("name", version=0)) # David

And, if I only set and get David with version=2, then I can get John and David in order as well as shown below:

from django.core.cache import cache

cache.set("name", "John")
cache.set("name", "David", version=2)
print(cache.get("name")) # John
print(cache.get("name", version=2)) # David

But, if I only set and get David with version=1, then I can get David and David in order as shown below so this is because John is set with version=1 by default?:

from django.core.cache import cache

cache.set("name", "John")
cache.set("name", "David", version=1)
print(cache.get("name")) # David
print(cache.get("name", version=1)) # David

In addition, if I set and get John and David without version=1, then I can get David and David in order as well as shown below:

from django.core.cache import cache

cache.set("name", "John")
cache.set("name", "David")
print(cache.get("name")) # David
print(cache.get("name")) # David

I know that the doc shows version=None for cache.set() as shown below:

                                               ↓ ↓ Here ↓ ↓
cache.set(key, value, timeout=DEFAULT_TIMEOUT, version=None)

So, does cache.set() actually use version=1 by default instead of version=None in Django?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129

2 Answers2

2

So, does cache.set() actually use version=1 by default instead of version=None in Django?

It uses the version specified in the cache. Indeed, if we look at the source code [GitHub], we see:

def make_key(self, key, version=None):
    """
    Construct the key used by all other methods. By default, use the
    key_func to generate a key (which, by default, prepends the
    `key_prefix' and 'version'). A different key function can be provided
    at the time of cache construction; alternatively, you can subclass the
    cache backend to provide custom key making behavior.
    """
    if version is None:
        version = self.version

    return self.key_func(key, self.key_prefix, version)

and the version can be set as parameter, or will default to one [GitHub]:

def __init__(self, params):
    # …
    self.version = params.get("VERSION", 1)
    # …

so you can set this in the cache settings with:

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
        "LOCATION": "unique-snowflake",
        "VERSION": 1425,
    }
}

if not, it will indeed default to one. This is also (briefly) explained in the options for the cache in the documentation on settings [Django-doc]:

VERSION

Default: 1

The default version number for cache keys generated by the Django server.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

In the official documentation (part about cache versioning) write "...By default, any key request will automatically include the site default cache key version..." And in this place of documentation we can see than this default cache key version = 1 by default