1
\$\begingroup\$

I want to limit the load so I rewrite the most expensive method and add caching, both memcache and cache-control:

class GeoRSS(webapp.RequestHandler):

    def get(self):
        start = datetime.datetime.now() - timedelta(days=60)
        count = (int(self.request.get('count'
                 )) if not self.request.get('count') == '' else 1000)
        memcache_key = 'ads'
        data = memcache.get(memcache_key)
        if data is None:
            a = Ad.all().filter('modified >',
                                start).filter('published =',
                    True).order('-modified').fetch(count)
            memcache.set('ads', a)
        else:
            a = data
        template_values = {'a': a, 'request': self.request,
                           'host': os.environ.get('HTTP_HOST',
                           os.environ['SERVER_NAME'])}
        dispatch = 'templates/georss.html'
        path = os.path.join(os.path.dirname(__file__), dispatch)
        output = template.render(path, template_values)
        self.response.headers["Cache-Control"] = "public,max-age=%s" % 86400
        self.response.headers['Content-Type'] = 'application/rss+xml'
        self.response.out.write(output)

Will this limit the load on my app? Did I prioritize correctly when I see the load on the app:

enter image description here

\$\endgroup\$
3
\$\begingroup\$

You can simplify (and sometimes speed up) things like this

try:
    a = memcache.get('ads')
except KeyError:
    a = Ad.all().filter('modified >',
                            start).filter('published =',
                True).order('-modified').fetch(count)
    memcache.set('ads', a)

It's (often) slightly faster in Python to avoid the if statement.

After measuring that with timeit, you should rename the variable a. That's a poor name.

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.