Google App Engine + Memcached + FF To Go = Bliss

Google App Engine gained support for memcached today, a very high performance caching system, and I’m already using it in FF To Go.

I’ll share some code with you so you know how it works.  This is the code that renders the public feed:

def public(request):
    f = friendfeed.FriendFeed()
    try:
        start = int(request.GET.get('start', 0))
    except:
        start = 0
    service = request.GET.get('service', None)
    num = int(request.session.get('num', NUM))
    key = 'public_%i_%i_%s' % (num, start, service)
    data = memcache.get(key)
    if not data:
        try:
            data = f.fetch_public_feed(num=num, start=start, service=service)
        except Exception, e:
            return HttpResponseRedirect('/%s/' % e)
        memcache.set(key, data, CACHE_TIME)
    extra_context = {
        'entries': data['entries'],
        'next': start + num,
    }
    if start > 0:
        extra_context['has_previous'] = True
        extra_context['previous'] = start - num
    return render_to_response('public.html', extra_context, context_instance=RequestContext(request))

And that’s it!  The FriendFeed team should be happy; I’m using less of their resources because I don’t need to query them as much as I used to.  Now this is really only useful on the FriendFeed public feed because all other feeds are likely to be accessed using your FriendFeed credentials and have user specific differences (ie hiding settings) which we don’t want to cache but the public feed does not respond to your hide settings so it is a good candidate.  It’s nice to see that using memcached on Google App Engine is no different from using it on your own server.

Am I the first to use memcached on Google App Engine?

Viewing 4 Comments

Trackbacks

close Reblog this comment
blog comments powered by Disqus