Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make it so that if the badge image is already in memcache we don't ne…

…ed to hit the datastore at all.
  • Loading branch information...
commit 0f38522e2f19d92b7a9533bb4a15fd305a4650d6 1 parent 7160628
Alex Komoroske authored
Showing with 15 additions and 9 deletions.
  1. +15 −9 voting.py
View
24 voting.py
@@ -115,20 +115,24 @@ def get_or_create_issue(number):
issue.put()
return issue
+def get_cached_issue_image(issue_number):
+ memcache_key = "issue_%d_image" % issue_number
+ cached_values = memcache.get_multi([memcache_key, memcache_key + "_width"])
+ image_data = cached_values.get(memcache_key)
+ width = cached_values.get(memcache_key + "_width")
+ if image_data and width:
+ return Image.fromstring("RGBA", (width, BADGE_HEIGHT), image_data)
+ return None
class Issue(db.Model):
number = db.IntegerProperty(required=True)
vote_count = db.IntegerProperty(default = 0)
def get_image(self):
#TODO: support multiple badge sizes
- memcache_key = "issue_%d_image" % self.number
- cached_values = memcache.get_multi([memcache_key, memcache_key + "_width"])
- image_data = cached_values.get(memcache_key)
- width = cached_values.get(memcache_key + "_width")
- if image_data and width:
- im = Image.fromstring("RGBA", (width, BADGE_HEIGHT), image_data)
- else:
+ im = get_cached_issue_image(self.number)
+ if not im:
im, width = draw_image(self.number, self.vote_count)
+ memcache_key = "issue_%d_image" % self.number
memcache.set_multi({memcache_key: im.tostring(), memcache_key + "_width" : width})
return im
def has_vote(self, username):
@@ -252,8 +256,10 @@ def render_template(self, issue, args = None):
class BadgePage(webapp2.RequestHandler):
def get(self, issue_number):
issue_number = int(issue_number)
- issue = get_or_create_issue(issue_number)
- im = issue.get_image() if issue else NO_ISSUE_IMAGE
+ im = get_cached_issue_image(issue_number)
+ if not im:
+ issue = get_or_create_issue(issue_number)
+ im = issue.get_image() if issue else NO_ISSUE_IMAGE
self.response.headers['Content-Type'] = "image/png"
im.save(self.response.out, "PNG")
Please sign in to comment.
Something went wrong with that request. Please try again.