Redis cache configuration in Django using django-environ

1.3k views Asked by At

Hereis the relevant part of my settings.py:

CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': env.str("REDIS_URI"),
        'OPTIONS': {
            'DB': 1,
            'SOCKET_TIMEOUT': 5,
            'SOCKET_CONNECT_TIMEOUT': 5,
            'CONNECTION_POOL_CLASS': 'redis.BlockingConnectionPool',
            'CONNECTION_POOL_CLASS_KWARGS': {
                'max_connections': 50,
                'timeout': 20},
            'PICKLE_VERSION': -1,
        },
    },
}

I'm moving the above configruation to django-environ:

env REDIS_URL=rediscache://127.0.0.1:6379/1client_class=redis_cache.RedisCache&default_timeout=360

How can I add the connection URL from REDIS_URL to Django's DATABASES?

1

There are 1 answers

0
rocksteady On

First of all you are missing a ? in the URL parameters:

REDIS_URL=rediscache://127.0.0.1:6379/1?client_class=redis_cache.RedisCache&default_timeout=360

Second, it is all described in the docs.

In your case:

CACHES = {
    'default': env.cache()  # default = 'CACHE_URL' environmennt variable
}

CACHE_URL needs to be set as environment variable.

export CACHE_URL=rediscache://127.0.0.1:6379/1?client_class=redis_cache.RedisCache&default_timeout=360

If you insist on using REDIS_URL you can do the following:

CACHES = {
    'default': env.cache('REDIS_URL')
}

Other options can be added using URL parameters.