1
0
forked from ScoDoc/ScoDoc

Cache: delete_pattern

This commit is contained in:
Emmanuel Viennet 2024-01-15 17:49:28 +01:00
parent a90fd6dcd0
commit 524df5cbc8
3 changed files with 32 additions and 1 deletions

View File

@ -121,6 +121,33 @@ class ScoDocCache:
for oid in oids:
cls.delete(oid)
@classmethod
def delete_pattern(cls, pattern: str, std_prefix=True) -> int:
"""Delete all keys matching pattern.
The pattern starts with flask_cache_<dept_acronym>.
If std_prefix is true (default), the prefix is added
to the given pattern.
Examples:
'TABASSI_tableau-etud-1234:*'
Or, with std_prefix false, 'flask_cache_RT_TABASSI_tableau-etud-1234:*'
Returns number of keys deleted.
"""
# see https://stackoverflow.com/questions/36708461/flask-cache-list-keys-based-on-a-pattern
assert CACHE.cache.__class__.__name__ == "RedisCache" # Redis specific
import redis
if std_prefix:
pattern = "flask_cache_" + g.scodoc_dept + "_" + cls.prefix + "_" + pattern
r = redis.Redis()
count = 0
for key in r.scan_iter(pattern):
log(f"{cls.__name__}.delete_pattern({key})")
r.delete(key)
count += 1
return count
class EvaluationCache(ScoDocCache):
"""Cache for evaluations.

View File

@ -1,7 +1,7 @@
# -*- mode: python -*-
# -*- coding: utf-8 -*-
SCOVERSION = "9.6.77"
SCOVERSION = "9.6.78"
SCONAME = "ScoDoc"

View File

@ -48,6 +48,10 @@ def test_notes_table(test_client): # XXX A REVOIR POUR TESTER RES TODO
formsemestre_id = sem["formsemestre_id"]
nt: NotesTableCompat = res_sem.load_formsemestre_results(formsemestre)
assert sco_cache.ResultatsSemestreCache.get(formsemestre_id)
# Efface les semestres
sco_cache.ResultatsSemestreCache.delete_pattern("*")
for sem in sems[:10]:
assert sco_cache.ResultatsSemestreCache.get(formsemestre_id) is None
def test_cache_evaluations(test_client):