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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Okay initially I thought i am suppose to enter my local host port number.
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?
Have you started the Redis server? Redis itself has to be started alongside Django. Start it: redis-server on the command line.
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."
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 redislabs.com/blog/redis-on-windows-8-1-and-previous-versions, scroll down to where it mentions Environment Variables and follow that.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.