Quantcast
Channel: What are best practices for caching for a "Home Feed" or a "News Feed" in Django - Stack Overflow
Viewing all articles
Browse latest Browse all 2

What are best practices for caching for a "Home Feed" or a "News Feed" in Django

$
0
0

I use Django as an API for my mobile front end. I just send JSON back and forth. I've created an endpoint for a home feed. Each user has a unique home feed depending on the people that they follow. Users post a photo, and that photo is pushed out to all of their followers' home feeds. Pretty simple and straight forward thus far.

A couple of my colleagues suggested that I should implement some sort of caching layer, but the problem is, this isn't just an regular site that is static. Each view is dynamic based on the user accessing it.

So for example, the home feed is a list of photos posted on the platform in DESC order of time (recent to old).

The home feed view is very basic. Each user has a 'homefeed:user_id:%s' list in Redis which contains the primary keys of the photo objects. I make a call over Redis and grab the request.user's homefeed list and then query the database with that list for those objects like so:

homefeed_pk_list = redis_server.lrange('homefeed:user_id:%s' % request.user.pk, 0, 100)# Home feed querysetqueryset = Photo.objects.filter(pk__in = homefeed_pk_list)response_data= []for photo in queryset:       # Code to return back JSON datareturn HttpResponse(json.dumps(response_data), content_type="application/json")    

Pretty simple. Now my question is, what should the best practice be for caching in this case? I could cache each serialized photo object individually and set an expiry of 24hrs since some photo objects are in multiple feeds (users . If the object doesn't exist in the cache, I'll hit the DB. What do you think of this approach?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images