Sumit Singh
Sumit Singh

Reputation: 23

redis not connected with django-redis

i have set a query set in my cache as :

cache.set('person',Lecture_Detail.objects.all())

in my views:

from django.core.cahe import cache
t3=datetime.datetime.now()
list(Lecture_Detail.objects.all())
t5 = datetime.datetime.now()
print "time before",(t5 - t3)
g = cache.get('person')
t4 = datetime.datetime.now()
print "time after",(t4 - t5)
g = cache.get('person')
t6 = datetime.datetime.now()
print "time after",t6-t4
g = cache.get('person')
t7 = datetime.datetime.now()
print "time after",t7-t6

when i exexute this its output is:

time before 0:00:00.014256
time after 0:00:01.366022
time after 0:00:01.552436
time after 0:00:01.433049

so i think that my redis is not connected with django-redis . my settings are:

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

so plz me suggest me something .. thanx in advance..

Upvotes: 0

Views: 575

Answers (2)

Sumit Singh
Sumit Singh

Reputation: 23

Actually my mistake was that i was using django cache instead of using redis cache .... to use redis we have to do something like this :

first set data from your python shell or view as:

import redis
r=redis.StrictRedis()
r.set('person',Lecture_Detail.objects.all())

to get data from redis cache:

import redis

r=redis.StrictRedis()
t3=datetime.datetime.now()
list(Lecture_Detail.objects.all())
t5 = datetime.datetime.now()
print "time before",(t5 - t3)
g = r.get('person')
t4 = datetime.datetime.now()
print "time after",(t4 - t5)

and now the time difference is just unbelieveable

Upvotes: 0

sinitsynsv
sinitsynsv

Reputation: 893

When you write queryset=Lecture_Detail.objects.all() database sql query is not yet executed. When the query is executed:

  1. iteration
  2. slicing
  3. Pickling/Caching
  4. repr()
  5. len()
  6. list()
  7. bool()

Read more about this here: When QuerySets are evaluated. So in your example database query would be executed on line cache.set('person',queryset)

You can test by change line from queryset=Lecture_Detail.objects.all() to list(queryset=Lecture_Detail.objects.all())

Upvotes: 5

Related Questions