Skip to content

Commit

Permalink
Add decorator for view caching
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof committed Nov 18, 2024
1 parent 0adb8ed commit 549043c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 20 deletions.
18 changes: 16 additions & 2 deletions cache_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions deposit/deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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')
4 changes: 2 additions & 2 deletions general/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -15,7 +15,7 @@


@general_bp.route('/')
@cache.cached(make_cache_key=make_key)
@cache_view()
def index() -> Response:
return render_template('index.html')

Expand Down
4 changes: 2 additions & 2 deletions group_manager/group_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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={})

Expand Down
4 changes: 2 additions & 2 deletions research/research.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__,
Expand Down Expand Up @@ -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')

Expand Down
6 changes: 3 additions & 3 deletions stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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={})

Expand All @@ -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={})

Expand Down
8 changes: 4 additions & 4 deletions user/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions vault/vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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')

Expand Down

0 comments on commit 549043c

Please sign in to comment.