DeferredSemCacheManager réentrant

This commit is contained in:
Emmanuel Viennet 2022-07-11 18:22:05 +02:00
parent a1f772b22e
commit b698196a09

View File

@ -231,29 +231,29 @@ def invalidate_formsemestre( # was inval_cache(formsemestre_id=None, pdfonly=Fa
"""expire cache pour un semestre (ou tous si formsemestre_id non spécifié).
Si pdfonly, n'expire que les bulletins pdf cachés.
"""
from app.models.formsemestre import FormSemestre
from app.scodoc import sco_cursus
if getattr(g, "defer_cache_invalidation", False):
assert isinstance(formsemestre_id, int) or formsemestre_id is None
if getattr(g, "defer_cache_invalidation", 0) > 0:
g.sem_to_invalidate.add(formsemestre_id)
return
log(f"inval_cache, formsemestre_id={formsemestre_id} pdfonly={pdfonly}")
if formsemestre_id is None:
# clear all caches
log("----- invalidate_formsemestre: clearing all caches -----")
formsemestre_ids = [
x[0]
for x in ndb.SimpleQuery(
"""SELECT id FROM notes_formsemestre s
WHERE s.dept_id=%(dept_id)s
""",
{"dept_id": g.scodoc_dept_id},
log(
f"----- invalidate_formsemestre: clearing all caches. pdfonly={pdfonly}-----"
)
formsemestre_ids = [
formsemestre.id
for formsemestre in FormSemestre.query.filter_by(dept_id=g.scodoc_dept_id)
]
else:
formsemestre_ids = [
formsemestre_id
] + sco_cursus.list_formsemestre_utilisateurs_uecap(formsemestre_id)
log(f"----- invalidate_formsemestre: clearing {formsemestre_ids} -----")
log(
f"----- invalidate_formsemestre: clearing {formsemestre_ids}. pdfonly={pdfonly} -----"
)
if not pdfonly:
# Delete cached notes and evaluations
@ -289,14 +289,16 @@ class DeferredSemCacheManager:
"""
def __enter__(self):
assert not hasattr(g, "defer_cache_invalidation")
g.defer_cache_invalidation = True
if not hasattr(g, "defer_cache_invalidation"):
g.defer_cache_invalidation = 0
g.sem_to_invalidate = set()
g.defer_cache_invalidation += 1
return True
def __exit__(self, exc_type, exc_value, exc_traceback):
assert g.defer_cache_invalidation
g.defer_cache_invalidation = False
g.defer_cache_invalidation -= 1
if g.defer_cache_invalidation == 0:
while g.sem_to_invalidate:
formsemestre_id = g.sem_to_invalidate.pop()
invalidate_formsemestre(formsemestre_id)