0

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..

  • 1
    Where do you run your Redis instance? Same server or somewhere else? Is `queryset` loaded all att once or is it lazy? (That is: does it fetch all rows or does it only fetch rows as you read them?). How large of a difference is it? You can edit your question to include answers to these questions. – Emil Vikström Sep 21 '15 at 11:04

2 Answers2

5

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())

sinitsynsv
  • 863
  • 6
  • 17
  • as you have suggest me now i'm applying following operation on both condition (after getting data from database and from cache) : t1=datetime.datetime.now() g = Lecture_Detail.objects.all() name = g.filter(lecture_name = 'Chemistry') length = len(name) t2=datetime.datetime.now() bt still time difference is very much its about : "without cache 0:00:00.903429 with cache 0:00:02.490702 " so please suggest me something.. – Sumit Singh Sep 22 '15 at 06:14
  • How many records in the table? How many records after filtering? – sinitsynsv Sep 22 '15 at 09:21
  • 101006 records are in table and 50000 afer filtering – Sumit Singh Sep 22 '15 at 09:36
  • I tried myself, but I used filesystem caching, and got similar results: without cache 0:00:00.246513 and wich cache 0:00:00.666614 – sinitsynsv Sep 22 '15 at 10:07
0

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