Skip to content

Commit

Permalink
memcache for heavy pages
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalopitz committed Oct 10, 2011
1 parent 5e5d9ba commit 96b3bbf
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions handlers.py
Expand Up @@ -4,6 +4,7 @@
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import memcache

from models import *

Expand All @@ -15,24 +16,36 @@ def __init__(self):
self.is_admin = users.is_current_user_admin()
self.user = users.get_current_user()

def render(self, template_file, template_values):
def render(self, template_file, template_values, cache_key=None):

template.register_template_library('filters')
template_path = os.path.join(os.path.dirname(__file__), 'templates', template_file)

template_values['is_admin'] = self.is_admin
template_values['user'] = self.user
template_values['http_host'] = self.request.host_url
template_values['logout_url'] = users.create_logout_url('/')

if(self.session.has_key('message')):
template_values['message'] = self.session['message']
del self.session['message']
if(None != cache_key):
cache_value = memcache.get(cache_key)

if(None != cache_value):
self.response.out.write(cache_value)
return
else:
template_values['message'] = False
template.register_template_library('filters')
template_path = os.path.join(os.path.dirname(__file__), 'templates', template_file)

template_values['is_admin'] = self.is_admin
template_values['user'] = self.user
template_values['http_host'] = self.request.host_url
template_values['logout_url'] = users.create_logout_url('/')

if(self.session.has_key('message')):
template_values['message'] = self.session['message']
del self.session['message']
else:
template_values['message'] = False

output = template.render(template_path, template_values)
self.response.out.write(output)
output = template.render(template_path, template_values)

if(None != cache_key):
memcache.add(cache_key, output, 600)

self.response.out.write(output)
return

def render404(self):
self.error(404)
Expand Down Expand Up @@ -76,7 +89,7 @@ def get(self):
template_values['offset'] = 5
template_values['count'] = 5
template_values['more'] = ( total > 5 )
self.render('posts.html', template_values)
self.render('posts.html', template_values, 'index')

class FeedHandler(BaseHandler):
def get(self):
Expand All @@ -91,7 +104,7 @@ def get(self):
template_values['date'] = datetime.datetime.now()

self.response.headers["Content-Type"] = "application/atom+xml"
self.render('atom.xml', template_values)
self.render('atom.xml', template_values, 'feed')

class AjaxMoreHandler(BaseHandler):
def get(self):
Expand Down

0 comments on commit 96b3bbf

Please sign in to comment.