I am using django's local memory cache (django.core.cache.backends.locmem.LocMemCache
) and using tastypie
for REST API.
I have following code in CommentResource
object in file api.py
:
def get_object_list(self, request):
cached_comments = cache.get('comments')
if cached_comments is not None:
print 'got comments in cache'
return cached_comments
print "didn't get comments in cache"
comments = super(CommentResource, self).get_object_list(request).order_by('-created')
cache.set('comments', comments, 100)
return comments
Now when I save a new comment, the API returns the new comment too, even though it prints got comments in cache
. How can the new comment possibly be cached?
There must be something wrong. Could the tastypie's dehydrate
method be an issue here?
UPDATE: I commented out the dehydrate
method and still the same issue. So that might not be the problem.
UPDATE 2: Even though, django's querysets
are lazy, this answer says that
Querysets are lazy, which means they don't call the database until they're evaluated. One way they could get evaluated would be to serialize them, which is what cache.set does behind the scenes.
So laziness of querysets
doesn't explain this behaviour.