From 549043ca02b96d756e782736fe14949a60a1e0e9 Mon Sep 17 00:00:00 2001 From: Lazlo Westerhof Date: Mon, 18 Nov 2024 09:46:46 +0100 Subject: [PATCH] Add decorator for view caching --- cache_config.py | 18 ++++++++++++++++-- deposit/deposit.py | 6 +++--- general/general.py | 4 ++-- group_manager/group_manager.py | 4 ++-- research/research.py | 4 ++-- stats/stats.py | 6 +++--- user/user.py | 8 ++++---- vault/vault.py | 4 ++-- 8 files changed, 34 insertions(+), 20 deletions(-) diff --git a/cache_config.py b/cache_config.py index e8261415..ffcb6d28 100644 --- a/cache_config.py +++ b/cache_config.py @@ -6,7 +6,8 @@ import hashlib import json from concurrent.futures import ThreadPoolExecutor -from typing import List, Optional +from functools import wraps +from typing import Callable, List, Optional from flask import current_app as app, g, request, session from flask_caching import Cache @@ -192,5 +193,18 @@ def populate_api_cache(fn: str, user: str, irods: str, session_id: str) -> None: log_error(f"Error prepopulating cache {fn}: {e}", True) -# Create a Cache instance. +def cache_view() -> Callable: + """Custom decorator to conditionally apply caching to views.""" + def decorator(f: Callable) -> Callable: + @wraps(f) + def wrapped(*args: str, **kwargs: int) -> Callable: + if cache: + return cache.cached(make_cache_key=make_key)(f)(*args, **kwargs) + else: + return f(*args, **kwargs) + return wrapped + return decorator + + +# Call the function cache = Cache(config=config) diff --git a/deposit/deposit.py b/deposit/deposit.py index 488d9ab4..7966da9b 100644 --- a/deposit/deposit.py +++ b/deposit/deposit.py @@ -24,7 +24,7 @@ import api import connman -from cache_config import cache, make_key +from cache_config import cache_view deposit_bp = Blueprint('deposit_bp', __name__, template_folder='templates', @@ -42,7 +42,7 @@ @deposit_bp.route('/') @deposit_bp.route('/browse') -@cache.cached(make_cache_key=make_key) +@cache_view() def index() -> Response: """Deposit overview""" return render_template('deposit/overview.html', @@ -168,7 +168,7 @@ def submit() -> Response: @deposit_bp.route('/thank-you') -@cache.cached(make_cache_key=make_key) +@cache_view() def thankyou() -> Response: """Step 4: Thank you""" return render_template('deposit/thank-you.html') diff --git a/general/general.py b/general/general.py index 0a96b94c..2ec7cfdb 100644 --- a/general/general.py +++ b/general/general.py @@ -6,7 +6,7 @@ from flask import Blueprint, redirect, render_template, Response, url_for from flask_wtf.csrf import CSRFError -from cache_config import cache, make_key +from cache_config import cache_view general_bp = Blueprint('general_bp', __name__, template_folder='templates/general', @@ -15,7 +15,7 @@ @general_bp.route('/') -@cache.cached(make_cache_key=make_key) +@cache_view() def index() -> Response: return render_template('index.html') diff --git a/group_manager/group_manager.py b/group_manager/group_manager.py index 11505191..093a4adc 100644 --- a/group_manager/group_manager.py +++ b/group_manager/group_manager.py @@ -6,7 +6,7 @@ from flask import Blueprint, make_response, render_template, request, Response import api -from cache_config import cache, make_key +from cache_config import cache_view group_manager_bp = Blueprint('group_manager_bp', __name__, template_folder='templates', @@ -68,7 +68,7 @@ def get_subcategories() -> Response: @group_manager_bp.route('/get_schemas', methods=['POST']) -@cache.cached(make_cache_key=make_key) +@cache_view() def get_schemas() -> Response: response = api.call('schema_get_schemas', data={}) diff --git a/research/research.py b/research/research.py index 5a0aa615..333dfec2 100644 --- a/research/research.py +++ b/research/research.py @@ -32,7 +32,7 @@ import api import connman -from cache_config import cache, make_key +from cache_config import cache_view from util import log_error, unicode_secure_filename research_bp = Blueprint('research_bp', __name__, @@ -89,7 +89,7 @@ def irods_writer() -> None: @research_bp.route('/') @research_bp.route('/browse') -@cache.cached(make_cache_key=make_key) +@cache_view() def index() -> Response: return render_template('research/browse.html') diff --git a/stats/stats.py b/stats/stats.py index 0e9c049d..f1115135 100644 --- a/stats/stats.py +++ b/stats/stats.py @@ -6,7 +6,7 @@ from flask import Blueprint, make_response, render_template, Response import api -from cache_config import cache, make_key +from cache_config import cache_view stats_bp = Blueprint('stats_bp', __name__, template_folder='templates', @@ -15,7 +15,7 @@ @stats_bp.route('/') -@cache.cached(make_cache_key=make_key) +@cache_view() def index() -> Response: category_response = api.call('resource_category_stats', data={}) @@ -25,7 +25,7 @@ def index() -> Response: @stats_bp.route('/export') -@cache.cached(make_cache_key=make_key) +@cache_view() def export() -> Response: response = api.call('resource_monthly_category_stats', data={}) diff --git a/user/user.py b/user/user.py index bec85072..c5f84eb4 100644 --- a/user/user.py +++ b/user/user.py @@ -35,7 +35,7 @@ import api import connman -from cache_config import cache, clear_view_cache_keys, executor, get_api_cache_functions, make_key, populate_api_cache +from cache_config import cache_view, clear_view_cache_keys, executor, get_api_cache_functions, populate_api_cache from util import is_email_in_domains, is_relative_url, log_error # Blueprint creation @@ -190,14 +190,14 @@ def settings() -> Response: @user_bp.route('/notifications') -@cache.cached(make_cache_key=make_key) +@cache_view() def notifications() -> Response: """Notifications page.""" return render_template('user/notifications.html') @user_bp.route('/data_access') -@cache.cached(make_cache_key=make_key) +@cache_view() def data_access() -> Response: """Data Access Passwords overview""" token_lifetime = app.config.get('TOKEN_LIFETIME') @@ -206,7 +206,7 @@ def data_access() -> Response: @user_bp.route('/data_transfer') -@cache.cached(make_cache_key=make_key) +@cache_view() def data_transfer() -> Response: """Data Transfer page.""" return render_template('user/data_transfer.html') diff --git a/vault/vault.py b/vault/vault.py index 0f29a9c3..e17cd75e 100644 --- a/vault/vault.py +++ b/vault/vault.py @@ -23,7 +23,7 @@ import api import connman -from cache_config import cache, make_key +from cache_config import cache_view vault_bp = Blueprint('vault_bp', __name__, template_folder='templates', @@ -33,7 +33,7 @@ @vault_bp.route('/') @vault_bp.route('/browse') -@cache.cached(make_cache_key=make_key) +@cache_view() def index() -> Response: return render_template('vault/browse.html')