2022-02-06 16:09:17 +01:00
|
|
|
##############################################################################
|
|
|
|
# ScoDoc
|
2023-01-02 13:16:27 +01:00
|
|
|
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
|
2022-02-06 16:09:17 +01:00
|
|
|
# See LICENSE
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""Cache pour résultats (super classe)
|
|
|
|
"""
|
|
|
|
|
|
|
|
from app.models import FormSemestre
|
|
|
|
|
|
|
|
|
|
|
|
class ResultatsCache:
|
2022-04-02 10:56:10 +02:00
|
|
|
"""Résultats cachés (via redis)
|
|
|
|
L'attribut _cached_attrs donne la liste des noms des attributs à cacher
|
|
|
|
(doivent être sérialisables facilement, se limiter à des types simples)
|
|
|
|
|
|
|
|
store() enregistre les attributs dans le cache, et
|
|
|
|
load_cached() les recharge.
|
|
|
|
"""
|
|
|
|
|
2022-02-06 16:09:17 +01:00
|
|
|
_cached_attrs = () # virtual
|
|
|
|
|
|
|
|
def __init__(self, formsemestre: FormSemestre, cache_class=None):
|
|
|
|
self.formsemestre: FormSemestre = formsemestre
|
|
|
|
self.cache_class = cache_class
|
|
|
|
|
|
|
|
def load_cached(self) -> bool:
|
|
|
|
"Load cached dataframes, returns False si pas en cache"
|
|
|
|
data = self.cache_class.get(self.formsemestre.id)
|
|
|
|
if not data:
|
|
|
|
return False
|
|
|
|
for attr in self._cached_attrs:
|
|
|
|
setattr(self, attr, data[attr])
|
|
|
|
return True
|
|
|
|
|
|
|
|
def store(self):
|
|
|
|
"Cache our data"
|
|
|
|
self.cache_class.set(
|
|
|
|
self.formsemestre.id,
|
|
|
|
{attr: getattr(self, attr) for attr in self._cached_attrs},
|
|
|
|
)
|