1

I have implemented django caching using redis following this blog: https://realpython.com/caching-in-django-with-redis/

So I followed this, installed the package, Added in

CACHES = {
"default": {
    "BACKEND": "redis_cache.RedisCache",
    "LOCATION": "redis://127.0.0.1:8000/",
    "OPTIONS": {
        "CLIENT_CLASS": "django_redis.client.DefaultClient"
    },
    "KEY_PREFIX": "example"
}

}

Then in views.

from django.conf import settings
from django.core.cache.backends.base import DEFAULT_TIMEOUT
from django.views.decorators.cache import cache_page

CACHE_TTL = getattr(settings, 'CACHE_TTL', DEFAULT_TIMEOUT)

and then added the decorator for the function

@cache_page(CACHE_TTL)
@login_required_dietitian
def patient_profile(request, id):
    data = {}
    return render(request, 'profile.html', {'data':data})

And then I am getting this error while I run the server

redis.exceptions.ConnectionError: Connection closed by server.

I am new to such caching technique, Any suggestion how to resolve this issue?

Harshit verma
  • 506
  • 3
  • 25

2 Answers2

3

Your configuration specifies Redis on port 8000, Redis, by default, runs on port 6379. Looks like its trying to connect your Django app, hence the Connection Error. Redis runs as a separate process, listening for requests on port 6379.

Mark Barrett
  • 366
  • 2
  • 16
  • Okay initially I thought i am suppose to enter my local host port number. – Harshit verma Sep 27 '19 at 13:07
  • But now I changed the port number to 6379 and I am getting this error: "Error 10061 connecting to 127.0.0.1:6379. No connection could be made because the target machine actively refused it." Any idea about this? – Harshit verma Sep 27 '19 at 13:08
  • Have you started the Redis server? Redis itself has to be started alongside Django. Start it: redis-server on the command line. – Mark Barrett Sep 27 '19 at 13:09
  • Getting this error now, and couldn't find any simple solution for this : "'redis-server' is not recognized as an internal or external command, operable program or batch file." – Harshit verma Sep 27 '19 at 13:18
  • So have you installed Redis? If so, the reason it can't find it is because you haven't added redis-server to your environment variables. Check this out https://redislabs.com/blog/redis-on-windows-8-1-and-previous-versions/, scroll down to where it mentions Environment Variables and follow that. – Mark Barrett Sep 27 '19 at 13:21
  • Actually I am using redis as a add on on heroku and now I have changed the port as per url provided by the add on package, still same error is coming! – Harshit verma Sep 27 '19 at 13:27
  • If you are using Redis in the cloud via Heroku, then simply give the IP of where it is and the Redis port. I've never used it on Heroku before, but connecting to Redis anywhere involves connecting to the box its on (whether that be a remote server or locally), on the 6379 port. Similar to connecting to MySQL etc. – Mark Barrett Sep 27 '19 at 13:30
  • Just done that, but facing a new issue now : "invalid DB index" – Harshit verma Sep 27 '19 at 13:32
  • Okay, at least you are now connected to Redis! :) It looks like in your URL you provided for Redis, you didn't give a database after the trailing forward slash. Try: yourIp:6379/0 This will instruct Redis to use database 0 – Mark Barrett Sep 27 '19 at 13:35
  • Okay so now igot the idea how to do it, the database=0 means it is to be included in the url with '/0' in the last... Thanks a lot! :) I wish I could upvote yur answer but I lack power at the time, so will do it once I got the power :) – Harshit verma Sep 27 '19 at 13:44
  • No problem, glad I helped! If you could "Accepted the Answer" using the tick, that would be awesome! – Mark Barrett Sep 27 '19 at 13:46
0

First of all follow this https://computingforgeeks.com/how-to-install-redis-on-fedora/ guide to install redis into your system and start it. In my case it is fedora and there is a link to Ubuntu on the page.

Change the port from 8000 to 6379 on LOCATION. Now then you'll be up and running.

I'd encourage this for a tutorial on redis for caching

Boy Nandi
  • 83
  • 7