Skip to content

Commit

Permalink
Make caching conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof committed Nov 14, 2024
1 parent 0adb8ed commit b99ccc3
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 28 deletions.
7 changes: 4 additions & 3 deletions admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from werkzeug.utils import secure_filename

import api
from cache_config import clear_api_cache_keys, clear_view_cache_keys
from cache_config import cache, clear_api_cache_keys, clear_view_cache_keys
from util import get_theme_directories, length_check

# Blueprint configuration
Expand Down Expand Up @@ -176,8 +176,9 @@ def save_settings(settings: Dict[str, Any], flash_msg: str) -> Response:
# Load the theme template if the current theme is changed
set_theme_loader(app, remove_cache=True)

clear_view_cache_keys(all_users=True)
clear_api_cache_keys("admin_save_settings")
if cache:
clear_view_cache_keys(all_users=True)
clear_api_cache_keys("admin_save_settings")
flash(flash_msg, 'success')

return redirect(url_for("admin_bp.index"))
Expand Down
6 changes: 4 additions & 2 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ def call(fn: str, data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
encoded_params = hashlib.shake_256(params.encode('utf-8')).hexdigest(20)

# Clear API cache keys if the API function called impacts keys.
clear_api_cache_keys(fn)
timeout = 0
if cache:
clear_api_cache_keys(fn)
timeout = get_api_cache_timeout(fn)

timeout = get_api_cache_timeout(fn)
cached_result = None
if timeout > 0:
cached_result = cache.get(make_key(f"{fn}-{encoded_params}"))
Expand Down
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def load_admin_setting() -> Dict[str, Any]:
Session(app)

# Initialize the cache.
cache.init_app(app)
if cache:
cache.init_app(app)

# Start monitoring thread for extracting tech support information
# Monitor signal file can be set to empty to completely disable monitor thread
Expand Down
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
13 changes: 7 additions & 6 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, 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 @@ -154,7 +154,8 @@ def logout() -> Response:
"""Logout user and redirect to index."""

# Clear view cache keys for user.
clear_view_cache_keys()
if cache:
clear_view_cache_keys()

connman.clean(session.sid)
session.clear()
Expand Down Expand Up @@ -190,14 +191,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 +207,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 Expand Up @@ -527,7 +528,7 @@ def populate_api_cache_thread(fn: str, user: str, irods: str, session_id: str) -
populate_api_cache(fn, user, irods, session_id)

# Submit the task to the global thread executor.
if should_populate_api_cache():
if cache and should_populate_api_cache():
for fn in get_api_cache_functions():
executor.submit(populate_api_cache_thread, fn, g.user, g.irods, session.sid)

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 b99ccc3

Please sign in to comment.