Is it needed to use `cache.close()` after finishing using cache in Django Views?

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:

  1. Is it needed to use cache.close() after finishing using cache in Django Views?
  2. If cache.close() is not used after finishing using cache in Django Views, are there anything bad?

Upvotes: 0

Views: 76

Answers (1)

devkral
devkral

Reputation: 11

no, see 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)

if the request finishes, all caches are closed.

I think it is for management commands or thirdparty integrations.

Upvotes: 1

Related Questions