python - `cache.set()`在Django中默认使用 `version=1`而不是 `version=None`吗?

标签 python django caching version django-cache

如果我仅使用 version=0 设置并获取 David,那么我可以获得 JohnDavid按如下所示的顺序。 *我用LocMemCache这是 Django 中的默认缓存,我正在学习 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

而且,如果我仅使用 version=2 设置并获取 David,那么我可以获得 JohnDavid按顺序如下所示:

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

但是,如果我仅使用 version=1 设置并获取 David,那么我可以获得 DavidDavid 的顺序如下所示,这是因为 John 默认设置为 version=1 吗?:

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

此外,如果我设置并获取 JohnDavid 而没有 version=1,那么我可以获得 David code> 和 David 按顺序如下所示:

from django.core.cache import cache

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

我知道the doc显示 cache.set()version=None 如下所示:

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

那么,cache.set() 实际上在 Django 中默认使用 version=1 而不是 version=None 吗?

最佳答案

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

它使用缓存中指定的版本。事实上,如果我们看一下 source code [GitHub] ,我们看到:

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:
        <b>version = self.version</b>

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

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

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

因此您可以在缓存设置中进行设置:

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

如果没有,它确实会默认为1。 the documentation on settings [Django-doc] 中的缓存选项也对此进行了(简要)解释。 :

VERSION

Default: 1

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

关于python - `cache.set()`在Django中默认使用 `version=1`而不是 `version=None`吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76936038/

相关文章:

javascript - Django 无法从 HTML 模板加载 JS 文件

Spring 缓存 key 生成器

Python eve 分页请求和更新请求

python - 成功更新资源后,我收到 JSON 解码错误

Python hadoop 流式传输 : Setting a job name

django - 如何将django rest api中的用户与Auth0同步

php - 交响乐团 : clear cache and warmup when users are using the website

python - Django 使用 redist 操作缓存值的有效方法

python - 在 MultiIndex 上使用 df.query 给出 UndefinedVariableError

python - QtDesigner 将字体大小设置为-1