Commits

Ashley Felton  committed f3f3b77

Add simple caching of views via Flask-Cache.

  • Participants
  • Parent commits 334d2a9

Comments (0)

Files changed (3)

File requirements.txt

 Flask==0.9
+Flask-Cache==0.7.1

File temper/__init__.py

 from flask import Flask
+from flaskext.cache import Cache
 
 # Configure application
-DEBUG = True
+DEBUG = False
+CACHE_TYPE = 'simple'
+CACHE_DEFAULT_TIMEOUT = 60 * 5  # Cache for 5 minutes by default.
+CACHE_THRESHOLD = 20
 
 app = Flask(__name__)
+cache = Cache(app)
 app.config.from_object(__name__)
 
 import temper.views

File temper/views.py

 import json
 from flask import request, render_template
-from temper import app, utils
+from temper import app, utils, cache
 from datetime import datetime
 
 
 
 @app.route('/')
 @app.route('/temps')
+@cache.cached()
 def temps_week():
     # Read up to 7 days-worth of log files.
     temps_li = utils.read_logs(n=7)
 
 
 @app.route('/temps/all')
+@cache.cached()
 def temps_all():
     # Read all log files.
     temps_li = utils.read_logs()
 
 
 @app.route('/temps/<int:year>/<int:month>/<int:day>')
+@cache.memoize(timeout=60 * 60 * 24)  # Cache 24h
 def log_date(year, month, day):
     # Read the log file for the specifed date.
     log_date = '{0}-{1}-{2}'.format(year, month, day)