From 65635f57a8f79b3365967cc06797ffa0a1f1aa73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillip=20K=C3=BChne?= Date: Sat, 9 Oct 2021 01:04:15 +0200 Subject: [PATCH] Fix overly agressive caching --- backend/app/helpers.py | 19 ++++++++++++++++++- backend/app/main.py | 21 ++++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/backend/app/helpers.py b/backend/app/helpers.py index ad01058..148fa76 100644 --- a/backend/app/helpers.py +++ b/backend/app/helpers.py @@ -3,6 +3,9 @@ from bs4 import BeautifulSoup import json import os import uuid +from flask import make_response +from functools import wraps, update_wrapper +from datetime import datetime data_directory = "data" config_file = data_directory+"/config.json" @@ -57,4 +60,18 @@ def setup_config(app): app.config['BASIC_AUTH_USERNAME'] = config['username'] app.config['BASIC_AUTH_PASSWORD'] = config['password'] app.config['ENTRY_QUOTA'] = config['entryquota'] - app.config['MAX_QUEUE'] = config['maxqueue'] \ No newline at end of file + app.config['MAX_QUEUE'] = config['maxqueue'] + + + +def nocache(view): + @wraps(view) + def no_cache(*args, **kwargs): + response = make_response(view(*args, **kwargs)) + response.headers['Last-Modified'] = datetime.now() + response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0' + response.headers['Pragma'] = 'no-cache' + response.headers['Expires'] = '-1' + return response + + return update_wrapper(no_cache, view) \ No newline at end of file diff --git a/backend/app/main.py b/backend/app/main.py index a7f2d4f..55611b6 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -3,15 +3,14 @@ import helpers import database import data_adapters import os -import errno import json from flask_basicauth import BasicAuth +from helpers import nocache app = Flask(__name__, static_url_path='/static') basic_auth = BasicAuth(app) accept_entries = False - @app.route("/") def home(): if basic_auth.authenticate(): @@ -27,6 +26,7 @@ def favicon(): @app.route('/api/enqueue', methods=['POST']) +@nocache def enqueue(): if not request.json: print(request.data) @@ -69,12 +69,14 @@ def songlist(): @app.route("/settings") +@nocache @basic_auth.required def settings(): return render_template('settings.html', app=app, auth=basic_auth.authenticate()) @app.route("/settings", methods=['POST']) +@nocache @basic_auth.required def settings_post(): entryquota = request.form.get("entryquota") @@ -92,24 +94,28 @@ def settings_post(): @app.route("/api/queue") +@nocache def queue_json(): list = data_adapters.dict_from_rows(database.get_list()) return Response(json.dumps(list, ensure_ascii=False).encode('utf-8'), mimetype='text/json') @app.route("/plays") +@nocache @basic_auth.required def played_list(): return render_template('played_list.html', list=database.get_played_list(), auth=basic_auth.authenticate()) @app.route("/api/songs") +@nocache def songs(): list = database.get_song_list() return Response(json.dumps(list, ensure_ascii=False).encode('utf-8'), mimetype='text/json') @app.route("/api/songs/update") +@nocache @basic_auth.required def update_songs(): database.delete_all_entries() @@ -120,6 +126,7 @@ def update_songs(): @app.route("/api/songs/compl") +@nocache def get_song_completions(input_string=""): input_string = request.args.get('search', input_string) if input_string != "": @@ -132,6 +139,7 @@ def get_song_completions(input_string=""): @app.route("/api/entries/delete/") +@nocache @basic_auth.required def delete_entry(entry_id): if database.delete_entry(entry_id): @@ -141,6 +149,7 @@ def delete_entry(entry_id): @app.route("/api/entries/delete", methods=['POST']) +@nocache @basic_auth.required def delete_entries(): if not request.json: @@ -155,6 +164,7 @@ def delete_entries(): @app.route("/api/entries/mark_sung/") +@nocache @basic_auth.required def mark_sung(entry_id): if database.add_sung_song(entry_id): @@ -164,6 +174,7 @@ def mark_sung(entry_id): @app.route("/api/entries/accept/") +@nocache @basic_auth.required def set_accept_entries(value): global accept_entries @@ -175,12 +186,14 @@ def set_accept_entries(value): @app.route("/api/entries/accept") +@nocache def get_accept_entries(): global accept_entries return Response('{"status": "OK", "value": '+str(int(accept_entries))+'}', mimetype='text/json') @app.route("/api/played/clear") +@nocache @basic_auth.required def clear_played_songs(): if database.clear_played_songs(): @@ -190,6 +203,7 @@ def clear_played_songs(): @app.route("/api/entries/delete_all") +@nocache @basic_auth.required def delete_all_entries(): if database.delete_all_entries(): @@ -223,7 +237,8 @@ def add_header(response): Add headers to both force latest IE rendering engine or Chrome Frame, and also to cache the rendered page for 10 minutes. """ - response.headers['Cache-Control'] = 'private, max-age=600' + if not 'Cache-Control' in response.headers: + response.headers['Cache-Control'] = 'private, max-age=600' return response @app.context_processor