Skip to content

Commit

Permalink
Added Flask-Cache for Mostly Static Endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
super3 committed Sep 9, 2015
1 parent e811e8a commit 7d7410c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 3 deletions.
10 changes: 9 additions & 1 deletion dataserv/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from flask import make_response, jsonify, request

from sqlalchemy import desc
from dataserv.run import app, db
from dataserv.run import app, db, cache

from dataserv.config import logging
from dataserv.Farmer import Farmer, AuthError

Expand Down Expand Up @@ -42,6 +43,10 @@ def online_farmers():
return q.all()


def disable_caching():
return app.config["DISABLE_CACHING"]


# Routes
@app.route('/')
def index():
Expand Down Expand Up @@ -109,6 +114,7 @@ def get_address():


@app.route('/api/online', methods=["GET"])
@cache.cached(timeout=app.config["CACHING_TIME"], unless=disable_caching)
def online():
"""Display a readable list of online farmers."""
logger.info("CALLED /api/online")
Expand All @@ -124,6 +130,7 @@ def online():


@app.route('/api/online/json', methods=["GET"])
@cache.cached(timeout=app.config["CACHING_TIME"], unless=disable_caching)
def online_json():
"""Display a machine readable list of online farmers."""
logger.info("CALLED /api/online/json")
Expand All @@ -138,6 +145,7 @@ def online_json():


@app.route('/api/total', methods=["GET"])
@cache.cached(timeout=app.config["CACHING_TIME"], unless=disable_caching)
def total():
logger.info("CALLED /api/total")

Expand Down
3 changes: 3 additions & 0 deletions dataserv/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
logging.basicConfig(format=_log_format, filename='dataserv.log',
level=logging.DEBUG)
TOTAL_UPDATE = 30 # minutes

DISABLE_CACHING = False
CACHING_TIME = 30 # seconds
5 changes: 4 additions & 1 deletion dataserv/run.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from flask import Flask
from flask.ext.cache import Cache
from flask.ext.sqlalchemy import SQLAlchemy


# Initialize the Flask application
cache = Cache(config={'CACHE_TYPE': 'simple'})

app = Flask(__name__)
app.config.from_pyfile('config.py')
db = SQLAlchemy(app)
cache.init_app(app)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Flask-SQLAlchemy == 2.0
RandomIO == 0.2.1
partialhash == 1.1.0
btctxstore == 4.5.0
Flask-Cache == 0.13.1
2 changes: 1 addition & 1 deletion tests/test_App.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class TemplateTest(unittest.TestCase):
def setUp(self):
app.config["SKIP_AUTHENTICATION"] = True # monkey patch
app.config["DISABLE_CACHING"] = True

self.app = app.test_client()
db.create_all()
Expand Down Expand Up @@ -305,7 +306,6 @@ def test_farmer_total_bytes(self):

# check online
rv = self.app.get('/api/online')

self.assertTrue(b"0" in rv.data)
self.assertTrue(b"2475" in rv.data)
self.assertTrue(b"2525" in rv.data)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_Farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class FarmerTest(unittest.TestCase):

def setUp(self):
app.config["SKIP_AUTHENTICATION"] = True # monkey patch
app.config["DISABLE_CACHING"] = True

db.create_all()

def tearDown(self):
Expand Down

0 comments on commit 7d7410c

Please sign in to comment.