views:

52

answers:

1

My app on google app engine returns content items (just text) and comments on them. It works like this (pseudo-ish code):

query: get keys of latest content #query to datastore
for each item in content
    if item_dict in memcache:
        use item_dict
    else:
        build_item_dict(item) #by fetching from datastore
        store item_dict in memcache
send all item_dicts to template

Sorry if the code isn't understandable. I get all of the content dictionaries and send them to the template, which uses them to create the webpage.

My problem is that if the memcache has expired, for each item I want to display, I have to (1) lookup item in memcache, (2) since no memcache exists I must fetch item from the datastore, and (3) store the item in memcache. These calls build up quickly.

I don't set an expire time for the entries to the memcache, so this really only happens once in the morning, but the webpage takes long enough to load (~1 sec) that the browser reports it as not existing. Regularly, my webpages take about 50ms to load.

This approach works decently for frequent visits, but it has its flaws as shown above. How can I remedy this? The entries are dynamic enough that I don't think it would be in my best interest to cache my initial request.

Thanks in advance

+3  A: 

You can do bulk operations - both on memcache and the datastore - and reduce all this to one round-trip to memcache, plus an extra datastore and memcache roundtrip for any missing entries. See the docs on memcache.get_multi and set_multi and on db.get.

I don't set an expire time for the entries to the memcache, so this really only happens once in the morning, but the webpage takes long enough to load (~1 sec) that the browser reports it as not existing.

Huh? Your browser gives up after 1 second?

Nick Johnson
Thanks, this will help a lot. Yeah, for some reason Chrome gives up really easily sometimes.
awegawef
that sounds like a bug in chrome, not a problem with your webpage.
Peter Recore