Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to redis in Django?

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

I am trying to connect to redis to save my object in it, but it gives me this error when i try to connect

Error 10061 connecting to 127.0.0.1:6379. No connection could be made because the target machine actively refused it

How does it work, what should i give in location and i am on a proxy from my company. Need some detailed explanation on location.

like image 721
Yogi Avatar asked Jan 06 '23 09:01

Yogi


2 Answers

if your redis is password protected, you should have a config like this:

CACHES.update({
    "redis": {
        "BACKEND": "redis_cache.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
             "PASSWORD": "XXXXXXXXXXX",
             "CLIENT_CLASS": "redis_cache.client.DefaultClient",
        },
    },
})
like image 63
Armita Avatar answered Jan 07 '23 22:01

Armita


First start the redis server. Your OS will provide a mechanism to do that, e.g. on some Linuxes you could use systemctl start redis, or /etc/init.d/redis start or similar. Or you could just start it directly with:

$ redis-server

which will run it as a foreground process.

Then try running the redis-cli ping command. Receiving a PONG response indicates that redis is in fact up and running on your local machine:

$ redis-cli ping
PONG

Once you have that working try Django again.

like image 43
mhawke Avatar answered Jan 07 '23 21:01

mhawke