bili
bili

Reputation: 610

GAE side panel listing recent post

I'm building a blog using python GAE and jinja2 template. The blog has the typical layout of two columns, the big one for main content and the smaller one for side panel stuff.

Now I'm considering to add in 5 recent posts from a user to the side panel, pretty much like the Google Blogger 'My blog List". But my concern is that I will have to pass this list to different Handlers when user goes to other pages within. For example, if the user in the main blog page, the side panel will show the 5 most recent posts. But if the user goes to another to say, add a new post then this page still displays 5 most recent posts. Here is the pseudo code of my current method of doing this.

MainPage_handler(self):
   self.render('main.html', recent_posts = function_to_get_recent_posts())

AddNewPost_handler(self):
   self.render('add_post.html', recent_posts = function_to_get_recent_posts())

The function looks like this:

class Blog(db.Model):
owner_id = db.StringProperty()
......
created = db.DateTimeProperty(auto_now_add = True)

@classmethod
def recent_post(cls, limit, onwer_name):
    blogs = cls.all().filter('owner_id = ', onwer_name).order('-created').fetch(limit)
    return blogs;

Is there a better way to achieve this?

Upvotes: 2

Views: 41

Answers (1)

Totem
Totem

Reputation: 7369

Since you want the 5 most recent posts, I would suggest that it makes sense to 're-calculate' what these are when redirecting or rendering a new page. Under that assumption, I would have to say that the way you are doing it appears to do what needs doing, and unless it is causing some issues with the page loading time or something, then leave it be. Perhaps if you don't want to hit the database everytime, you could have these recent posts cached using the memcached options built in to GAE, but I guess that may possibly already be what your function returns.

I see from your function that you appear to be hitting the database directly each time you call the function. What about caching the 5 most recent posts each time a post is added? That way, you will only hit the database when someone adds a post(in the context of the side panel posts), instead of every time you redirect etc.

Check out memcached here

However, if you are using the GAE ndb, then this manages caching automatically.

Upvotes: 1

Related Questions