0

Introduction:

  • django 3.2.16
  • django-redis 4.12.1

Problem: I use redis as a cache storage. I put the data to redis manually using the next code:

from django_redis import get_redis_connection


def redis_handler() -> None:
    redis = get_redis_connection()
    ...
    # some business logic
    ...
    redis.close()

I call redis_handler() often and every time a new connection to redis created.

Idea/Solution: Can I create a "global" redis connection once for a project and then import and use it everywhere? Where I can do it? What problems can be? Because I will not close it while the project works.

I was inspired FastApi - it has hooks when the project starts and stops.

Vadim Beglov
  • 343
  • 1
  • 4
  • 15

0 Answers0