Skip to content

Commit

Permalink
Improve cache code
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Dec 13, 2024
1 parent 9d9bfbc commit 566b004
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
19 changes: 10 additions & 9 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import logging
import copy
import requests
import datetime
from requests.exceptions import Timeout
from werkzeug.middleware.proxy_fix import ProxyFix
from flask_dance.consumer import OAuth2ConsumerBlueprint
Expand Down Expand Up @@ -251,6 +252,9 @@ def home():
flash("Error getting User info: \n" + account_info.text, 'error')
return render_template('home.html', oidc_name=settings.oidcName)

scheduler.add_job(func=utils.get_cache_creds, trigger='date', run_date=datetime.datetime.now(),
misfire_grace_time=20, args=[cred, get_cred_id()], id='get_cache_creds')

# if there are any next url, redirect to it
if "next" in session and session["next"]:
next_url = session.pop("next")
Expand Down Expand Up @@ -497,7 +501,7 @@ def showinfrastructures():
app.logger.exception("Error getting vm info: %s" % ex)
radl_json = []
try:
creds = utils.get_cache_creds(session, cred, get_cred_id())
creds = utils.get_cache_creds(cred, get_cred_id())
except Exception as ex:
app.logger.exception("Error getting user credentials: %s" % ex)
creds = []
Expand Down Expand Up @@ -767,7 +771,7 @@ def configure():
app.logger.debug("Template: " + json.dumps(toscaInfo[selected_tosca]))

try:
creds = utils.get_cache_creds(session, cred, get_cred_id(), 1)
creds = utils.get_cache_creds(cred, get_cred_id(), 1)
except Exception as ex:
flash("Error getting user credentials: %s" % ex, "error")
creds = []
Expand Down Expand Up @@ -1121,7 +1125,7 @@ def manage_creds():
creds = {}

try:
creds = utils.get_cache_creds(session, cred, get_cred_id())
creds = utils.get_cache_creds(cred, get_cred_id())
# Get the project_id in case it has changed
utils.get_project_ids(creds)
except Exception as e:
Expand Down Expand Up @@ -1176,8 +1180,7 @@ def write_creds():
# Get project_id to save it to de DB
utils.get_project_ids([creds])
# delete cached credentials
if 'creds' in session:
del session['creds']
utils.clear_cache_creds(get_cred_id())
cred.write_creds(creds["id"], get_cred_id(), creds, cred_id in [None, ''])
if val_res == 0:
flash("Credentials successfully written!", 'success')
Expand All @@ -1195,8 +1198,7 @@ def delete_creds():
cred_id = request.args.get('cred_id', "")
try:
# delete cached credentials
if 'creds' in session:
del session['creds']
utils.clear_cache_creds(get_cred_id())
cred.delete_cred(cred_id, get_cred_id())
flash("Credentials successfully deleted!", 'success')
except Exception as ex:
Expand All @@ -1215,8 +1217,7 @@ def enable_creds():
if val_res == 2:
flash(val_msg, 'warning')
# delete cached credentials
if 'creds' in session:
del session['creds']
utils.clear_cache_creds(get_cred_id())
cred.enable_cred(cred_id, get_cred_id(), enable)
except Exception as ex:
flash("Error updating credentials %s!" % ex, 'error')
Expand Down
17 changes: 12 additions & 5 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
SITE_LIST = {}
LAST_UPDATE = 0
PORT_SPECT_TYPES = ["PortSpec", "tosca.datatypes.network.PortSpec", "tosca.datatypes.indigo.network.PortSpec"]

CREDS_CACHE = {}

def _getStaticSitesInfo(force=False):
# Remove cache if force is True
Expand Down Expand Up @@ -984,13 +984,20 @@ def merge_templates(template, new_template):
return template


def get_cache_creds(session, cred, userid, enabled=None):
if "creds" not in session:
session["creds"] = cred.get_creds(userid)
def get_cache_creds(cred, userid, enabled=None):
global CREDS_CACHE
if userid not in CREDS_CACHE:
CREDS_CACHE[userid] = cred.get_creds(userid)

res = []
for cred in session["creds"]:
for cred in CREDS_CACHE[userid]:
if enabled is None or enabled == cred['enabled']:
res.append(cred)

return res


def clear_cache_creds(userid):
global CREDS_CACHE
if userid in CREDS_CACHE:
del CREDS_CACHE[userid]

0 comments on commit 566b004

Please sign in to comment.