Add caching

This commit is contained in:
Robbe Van Herck 2020-05-13 18:20:20 +02:00
parent b317725023
commit edf09757ae
3 changed files with 18 additions and 2 deletions

View file

@ -19,6 +19,10 @@ pip install -r requirements.txt
SHEET_ID = #FILLMEIN
RANGE_NAME = #METOO
[Cache]
TIMEOUT = 10
```
3. [Request a Google service account](https://console.cloud.google.com/iam-admin/serviceaccounts)
@ -31,5 +35,4 @@ RANGE_NAME = #METOO
## TODO
- Database caching: don't re-request google sheet data every time
- Error handling: it shouldn't ever crash on user input

View file

@ -2,19 +2,31 @@ from .sheet_data import google_sheet_to_json
from flask import Flask
from flask_cors import CORS, cross_origin
from flask_caching import Cache
import configparser
# Setup Flask app
app = Flask(__name__)
# Add CORS
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
import configparser
# Add caching
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
# Read the config file
CONFIG = configparser.ConfigParser()
CONFIG.read("config.ini")
SPREADSHEET_ID = CONFIG["Google"]["SHEET_ID"]
RANGE_NAME = CONFIG["Google"]["RANGE_NAME"]
CACHE_TIMEOUT = int(CONFIG["Cache"]["TIMEOUT"])
@app.route('/data.json')
@cross_origin()
@cache.cached(timeout=CACHE_TIMEOUT)
def data_json():
return google_sheet_to_json(SPREADSHEET_ID, RANGE_NAME)

View file

@ -3,6 +3,7 @@ certifi==2020.4.5.1
chardet==3.0.4
click==7.1.2
Flask==1.1.2
Flask-Caching==1.8.0
Flask-Cors==3.0.8
google-api-core==1.17.0
google-api-python-client==1.8.2