Reputation: 23
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
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
Reputation: 893
When you write queryset=Lecture_Detail.objects.all()
database sql query is not yet executed.
When the query is executed:
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