Permalink
Browse files

better caching, thanks Niphlod

  • Loading branch information...
1 parent ce2fbbd commit bb19d2a0069af1384f2b5a68b34ae2d7b8a1ca35 @mdipierro mdipierro committed Sep 7, 2012
Showing with 14 additions and 3 deletions.
  1. +14 −3 controllers/default.py
View
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-import os, glob
+import os, datetime
session.forget()
response.title = 'web2py'
@@ -74,16 +74,24 @@ def truncate(x): return x[:70]+'...' if len(x)>70 else x
def index():
books = {}
for subfolder in FOLDERS:
- books[subfolder] = get_info(subfolder)
+ books[subfolder] = cache.ram('info_%s' % subfolder, lambda: get_info(subfolder), time_expire=60*60*24)
return locals()
+def calc_date(now=request.utcnow.date()):
+ TOMORROW = now + datetime.timedelta(days=1)
+ format = '%a, %d %b %Y %H:%M:%S GMT'
+ return TOMORROW.strftime(format)
+
def chapter():
book_id, chapter_id = request.args(0), request.args(1,cast=int,default='0')
subfolder = get_subfolder(book_id)
info = cache.ram('info_%s' % subfolder, lambda: get_info(subfolder), time_expire=60*60*24)
chapters = cache.ram('chapters_%s' % subfolder, lambda: get_chapters(subfolder), time_expire=60*60*24)
filename = os.path.join(FOLDER,subfolder,'%.2i.markmin' % chapter_id)
dest = os.path.join(request.folder, 'static_chaps', subfolder, '%.2i.html' % chapter_id)
+ response.headers['Cache-Control'] = 'public, must-revalidate'
+ response.headers['Expires'] = calc_date()
+ response.headers['Pragma'] = None
if not os.path.isfile(dest):
content = open(filename).read()
content = convert2html(book_id,content).xml()
@@ -131,7 +139,10 @@ def image():
filename = os.path.join(FOLDER,subfolder,'images',key)
if not os.path.isfile(filename):
raise HTTP(404)
- return response.stream(open(filename,'r'))
+ response.headers['Cache-Control'] = 'public, must-revalidate'
+ response.headers['Expires'] = calc_date()
+ response.headers['Pragma'] = None
+ return response.stream(filename)
def reference():

0 comments on commit bb19d2a

Please sign in to comment.