I found cache.close() saying below in the doc. *I'm learning Django Cache:
You can close the connection to your cache with close() if implemented by the cache backend.
So, I use cache.close() after I finish using cache in Django Views as shown below:
# "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")
My questions:
- Is it needed to use
cache.close()after finishing using cache in Django Views? - If
cache.close()is not used after finishing using cache in Django Views, are there anything bad?
no, see django/core/cache/__init__.py:
if the request finishes, all caches are closed.
I think it is for management commands or thirdparty integrations.