Skip to content
Permalink
Browse files

Merge pull request #2067 from internetarchive/feature/cache-home

Caching homepage to decrease TTFB
  • Loading branch information...
hornc committed Apr 23, 2019
2 parents 4021acd + 3bc6085 commit b5c19aecd37bcb1837e5b147fc101bc08bf7d781
Showing with 31 additions and 12 deletions.
  1. +31 −12 openlibrary/plugins/openlibrary/home.py
@@ -32,25 +32,44 @@
'preset:authorsalliance_mitpress': '(openlibrary_subject:(authorsalliance) OR collection:(mitpress) OR publisher:(MIT Press) OR openlibrary_subject:(mitpress)) AND (!loans__status__status:UNAVAILABLE)'
}


def get_homepage():
if 'env' not in web.ctx:
delegate.fakeload()
try:
stats = admin.get_stats()
except Exception:
logger.error("Error in getting stats", exc_info=True)
stats = None
blog_posts = get_blog_feeds()

# render tempalte should be setting ctx.bodyid
# but because get_homepage is cached, this doesn't happen
# during subsequent called
page = render_template(
"home/index", stats=stats,
blog_posts=blog_posts
)
page.v2 = True
return dict(page)

def get_cached_homepage():
five_minutes = 5 * dateutil.MINUTE_SECS
return cache.memcache_memoize(
get_homepage, "home.homepage", timeout=five_minutes)()

class home(delegate.page):
path = "/"

def is_enabled(self):
return "lending_v2" in web.ctx.features

def GET(self):
try:
stats = admin.get_stats()
except Exception:
logger.error("Error in getting stats", exc_info=True)
stats = None
blog_posts = get_blog_feeds()
page = render_template(
"home/index", stats=stats,
blog_posts=blog_posts,
)
page.v2 = True
return page
cached_homepage = get_cached_homepage()
# when homepage is cached, home/index.html template
# doesn't run ctx.setdefault to set the bodyid so we must do so here:
web.template.Template.globals['ctx']['bodyid'] = 'home'
return web.template.TemplateResult(cached_homepage)

class random_book(delegate.page):
path = "/random"

0 comments on commit b5c19ae

Please sign in to comment.
You can’t perform that action at this time.