我在文档中发现了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")
我的问题:
cache.close()
吗?cache.close()
,有什么不好吗?不,请参阅 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)
如果请求完成,所有缓存都将关闭。
我认为它用于管理命令或第三方集成。