Skip to content

Commit

Permalink
Add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
beaugunderson committed Feb 8, 2014
1 parent 5467834 commit 32ebb27
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import twilio.twiml

from flask import Flask, request, render_template, url_for
from flask_cache import Cache
from flask_jsonpify import jsonify
from raven.contrib.flask import Sentry
from twilio import TwilioRestException
Expand All @@ -19,6 +20,7 @@

app.config.from_object('config.ConfigProduction')

cache = Cache(app, config={'CACHE_TYPE': 'simple'})
sentry = Sentry(app)

db.init_app(app)
Expand All @@ -28,6 +30,13 @@
data = PoliticalData()


def make_cache_key(*args, **kwargs):
path = request.path
args = str(hash(frozenset(request.args.items())))

return (path + args).encode('utf-8')


def play_or_say(resp_or_gather, msg_template, **kwds):
# take twilio response and play or say a mesage
# can use mustache templates to render keword arguments
Expand Down Expand Up @@ -297,11 +306,13 @@ def demo():
return render_template('demo.html')


@cache.cached(timeout=5)
@app.route('/count')
def count():
return jsonify(count=call_count())


@cache.cached(timeout=60, key_prefix=make_cache_key)
@app.route('/stats')
def stats():
password = request.values.get('password', None)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Flask==0.10.1
Flask-Cache==0.12
Flask-Jsonpify==1.1
-e git+https://github.com/mitsuhiko/flask-sqlalchemy.git@d4560013c1c51ef035381e35dd42a1628bb212ee#egg=Flask_SQLAlchemy
Jinja2==2.7.2
Expand Down

0 comments on commit 32ebb27

Please sign in to comment.