Skip to content

Commit

Permalink
PoC with response caching
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof committed Oct 25, 2024
1 parent 609dd99 commit 12d9303
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from admin.admin import admin_bp, set_theme_loader
from api import api_bp
from cache_config import cache
from datarequest.datarequest import datarequest_bp
from deposit.deposit import deposit_bp
from fileviewer.fileviewer import fileviewer_bp
Expand Down Expand Up @@ -128,6 +129,9 @@ def load_admin_setting() -> Dict[str, Any]:
# Start Flask-Session
Session(app)

# Initialize the 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
monitor_enabled: bool = app.config.get("MONITOR_SIGNAL_FILE", "/var/www/yoda/show-tech.sig") != ""
Expand Down Expand Up @@ -192,6 +196,10 @@ def static_loader() -> Optional[Response]:
return None


def authenticated() -> bool:
return g.get('user') is not None and g.get('irods') is not None


@app.before_request
def protect_pages() -> Optional[Response]:
"""Restricted pages access protection."""
Expand Down
27 changes: 27 additions & 0 deletions cache_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3

__copyright__ = 'Copyright (c) 2024, Utrecht University'
__license__ = 'GPLv3, see LICENSE'

from flask import g, session
from flask_caching import Cache

config = {
'CACHE_TYPE': 'FileSystemCache',
'CACHE_DIR': '/tmp',
'CACHE_DEFAULT_TIMEOUT': 300
}

# Create a Cache instance.
cache = Cache(config=config)


def authenticated() -> bool:
return g.get('user') is not None and g.get('irods') is not None


def make_key() -> str:
if authenticated():
return session.sid
else:
return "unauthenticated"
3 changes: 3 additions & 0 deletions general/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
from flask import Blueprint, redirect, render_template, Response, url_for
from flask_wtf.csrf import CSRFError

from cache_config import cache, make_key

general_bp = Blueprint('general_bp', __name__,
template_folder='templates/general',
static_folder='static/general',
static_url_path='/assets')


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

Expand Down
2 changes: 2 additions & 0 deletions group_manager/group_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask import Blueprint, make_response, render_template, request, Response

import api
from cache_config import cache, make_key

group_manager_bp = Blueprint('group_manager_bp', __name__,
template_folder='templates',
Expand All @@ -14,6 +15,7 @@


@group_manager_bp.route('/')
@cache.cached(timeout=60, make_cache_key=make_key)
def index() -> Response:
response = api.call('group_data', data={})
group_hierarchy = response['data']['group_hierarchy']
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ python-irodsclient==2.2.0
Flask==3.0.2
flask-session==0.6.0
Flask-WTF==1.2.1
Flask-Caching==2.3.0
mod-wsgi==5.0.0
pyjwt[crypto]==2.8.0
requests==2.32.0
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import-order-style=smarkets
strictness=short
docstring_style=sphinx
max-line-length=127
application-import-names=admin,api,connman,errors,fileviewer,general,group_manager,monitor,research,search,open_search,stats,user,vault,deposit,intake,datarequest,util
application-import-names=admin,api,cache_config,connman,errors,fileviewer,general,group_manager,monitor,research,search,open_search,stats,user,vault,deposit,intake,datarequest,util
exclude=venv

[mypy]
Expand Down

0 comments on commit 12d9303

Please sign in to comment.