Django Views 使用完缓存后还需要使用 `cache.close()` 吗?

问题描述 投票:0回答:1

我在文档中发现了cache.close()。 *我正在学习 Django Cache:

如果由缓存后端实现,您可以使用 close() 关闭与缓存的连接。

所以,我在 Django Views 中使用完缓存后使用

cache.close()
,如下所示:

# "views.py"

from django.core.cache import cache

def test(request):
    cache.set("name", "John")
    cache.set("age", 36)

    print(cache.get("first_name")) # John
    print(cache.get("age")) # 36
    
    cache.close() # Here

    return HttpResponse("Test")

我的问题:

  1. Django Views 使用完缓存后还需要使用
    cache.close()
    吗?
  2. 如果在 Django Views 中使用完缓存后还没有使用
    cache.close()
    ,有什么不好吗?
python django django-views django-cache django-caching
1个回答
0
投票

不,请参阅 django/core/cache/__init__.py:

def close_caches(**kwargs):
    # Some caches need to do a cleanup at the end of a request cycle. If not
    # implemented in a particular backend cache.close() is a no-op.
    caches.close_all()


signals.request_finished.connect(close_caches)

如果请求完成,所有缓存都将关闭。

我认为它用于管理命令或第三方集成。

© www.soinside.com 2019 - 2024. All rights reserved.