DocScoDoc/app/scodoc/sco_core.py

196 lines
6.9 KiB
Python
Raw Normal View History

2021-05-29 18:22:51 +02:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
2021-07-15 15:05:54 +02:00
"""Gestion des caches
XXX à déplacer dans sco_cache ?
-écriture en cours pour ScoDoc8, utilise flask_caching et memcached
ScoDoc est maintenant multiprocessus / mono-thread, avec un cache en mémoire partagé.
2021-05-29 18:22:51 +02:00
"""
2021-06-17 00:08:37 +02:00
import time
2021-07-09 17:47:06 +02:00
import six.moves._thread
2021-05-29 18:22:51 +02:00
2021-07-15 15:05:54 +02:00
2021-06-13 23:37:14 +02:00
from scodoc_manager import sco_mgr
import app.scodoc.sco_utils as scu
from app.scodoc.notes_log import log
from app.scodoc.sco_exceptions import NoteProcessError
from app.scodoc import sco_cache
2021-06-13 23:37:14 +02:00
#
# Cache global: chaque instance, repérée par sa connexion db, a un cache
# qui est recréé à la demande
#
2021-06-17 00:08:37 +02:00
NOTES_CACHE_INST = {} # { db_cnx_string : CacheNotesTable instance }
2021-07-15 15:05:54 +02:00
# XXX OBSOLETE... XXX
2021-06-13 23:37:14 +02:00
2021-07-09 23:31:16 +02:00
class CacheNotesTable(object):
2021-06-17 00:08:37 +02:00
"""gestion rudimentaire de cache pour les NotesTables"""
def __init__(self):
log("new CacheTable (id=%s)" % id(self))
#
2021-07-09 17:47:06 +02:00
self.lock = six.moves._thread.allocate_lock()
2021-06-17 00:08:37 +02:00
self.owner_thread = None # thread owning this cache
self.nref = 0
# Cache des NotesTables
self.cache = {} # { formsemestre_id : NoteTable instance }
# Cache des classeur PDF (bulletins)
2021-07-15 15:05:54 +02:00
self.pdfcache = {} # { (formsemestre_id, version) : (filename, pdfdoc) }
2021-06-17 00:08:37 +02:00
# Listeners:
self.listeners = scu.DictDefault(
defaultvalue={}
) # {formsemestre_id : {listener_id : callback }}
2021-07-15 15:05:54 +02:00
def acquire(self): # OBSOLETE
2021-06-17 00:08:37 +02:00
"If this thread does not own the cache, acquire the lock"
2021-07-09 17:47:06 +02:00
if six.moves._thread.get_ident() != self.owner_thread:
2021-06-17 00:08:37 +02:00
if self.lock.locked():
log(
2021-07-09 17:47:06 +02:00
"acquire: ident=%s waiting for lock" % six.moves._thread.get_ident()
2021-06-17 00:08:37 +02:00
) # XXX debug
self.lock.acquire()
2021-07-09 17:47:06 +02:00
self.owner_thread = six.moves._thread.get_ident()
2021-06-17 00:08:37 +02:00
if self.owner_thread is None: # bug catching
log("WARNING: None thread id !")
self.nref += 1
# log('nref=%d' % self.nref)
2021-07-15 15:05:54 +02:00
def release(self): # OBSOLETE
2021-06-17 00:08:37 +02:00
"Release the lock"
cur_owner_thread = self.owner_thread
# log('release: ident=%s (nref=%d)' % (thread.get_ident(), self.nref))
self.nref -= 1
if self.nref == 0:
self.lock.release()
self.owner_thread = None
# Debug:
2021-07-09 17:47:06 +02:00
if six.moves._thread.get_ident() != cur_owner_thread:
2021-06-17 00:08:37 +02:00
log(
"WARNING: release: ident=%s != owner=%s nref=%d"
2021-07-09 17:47:06 +02:00
% (six.moves._thread.get_ident(), cur_owner_thread, self.nref)
2021-06-17 00:08:37 +02:00
)
raise NoteProcessError("problem with notes cache")
def get_NotesTable(self, context, formsemestre_id): # >
2021-07-12 15:13:10 +02:00
from app.scodoc import notes_table
2021-06-17 00:08:37 +02:00
try:
self.acquire()
2021-07-09 17:47:06 +02:00
if formsemestre_id in self.cache:
2021-06-17 00:08:37 +02:00
# log('cache hit %s (id=%s, thread=%s)'
# % (formsemestre_id, id(self), thread.get_ident()))
return self.cache[formsemestre_id]
else:
t0 = time.time()
nt = notes_table.NotesTable(context, formsemestre_id)
dt = time.time() - t0
self.cache[formsemestre_id] = nt
log(
"caching formsemestre_id=%s (id=%s) (%gs)"
% (formsemestre_id, id(self), dt)
)
return nt
finally:
self.release()
def inval_cache(self, context, formsemestre_id=None, pdfonly=False): # >
"expire cache pour un semestre (ou tous si pas d'argument)"
from app.scodoc import sco_parcours_dut
2021-07-19 19:53:01 +02:00
XXX
2021-06-17 00:08:37 +02:00
def store_bulletins_pdf(self, formsemestre_id, version, filename, pdfdoc):
"cache pdf data"
log(
"caching PDF formsemestre_id=%s version=%s (id=%s)"
% (formsemestre_id, version, id(self))
)
try:
self.acquire()
self.pdfcache[(formsemestre_id, version)] = (filename, pdfdoc)
finally:
self.release()
def get_bulletins_pdf(self, formsemestre_id, version):
"returns cached PDF, or None if not in the cache"
try:
self.acquire()
if not hasattr(self, "pdfcache"):
self.pdfcache = {} # fix for old zope instances...
r = self.pdfcache.get((formsemestre_id, version), None)
if r:
log(
"get_bulletins_pdf(%s): cache hit %s (id=%s, thread=%s)"
2021-07-09 23:31:16 +02:00
% (
version,
formsemestre_id,
id(self),
six.moves._thread.get_ident(),
)
2021-06-17 00:08:37 +02:00
)
return r
finally:
self.release()
def add_listener(self, callback, formsemestre_id, listener_id):
"""Add a "listener": a function called each time a formsemestre is modified"""
self.listeners[formsemestre_id][listener_id] = callback
2021-07-15 15:05:54 +02:00
def remove_listener(self, formsemestre_id, listener_id): # UNUSED
2021-06-17 00:08:37 +02:00
"""Remove a listener.
May raise exception if does not exists.
"""
del self.listeners[formsemestre_id][listener_id]
def _call_listeners(self, formsemestre_id):
for listener_id, callback in self.listeners[formsemestre_id].items():
callback(listener_id)
def _call_all_listeners(self):
for formsemestre_id in self.listeners:
self._call_listeners(formsemestre_id)
2021-06-13 23:37:14 +02:00
def get_notes_cache(context):
"returns CacheNotesTable instance for us"
u = sco_mgr.get_db_uri() # identifie le dept de facon unique
2021-07-09 17:47:06 +02:00
if u not in NOTES_CACHE_INST:
2021-06-13 23:37:14 +02:00
log("getNotesCache: creating cache for %s" % u)
NOTES_CACHE_INST[u] = CacheNotesTable()
return NOTES_CACHE_INST[u]
def inval_cache(
context, formsemestre_id=None, pdfonly=False, formsemestre_id_list=None
): # >
"expire cache pour un semestre (ou tous si pas d'argument)"
if formsemestre_id_list:
for formsemestre_id in formsemestre_id_list:
get_notes_cache(context).inval_cache(
context, formsemestre_id=formsemestre_id, pdfonly=pdfonly
)
# Affecte aussi cache inscriptions
get_formsemestre_inscription_cache(context).inval_cache(key=formsemestre_id)
else:
get_notes_cache(context).inval_cache(
context, formsemestre_id=formsemestre_id, pdfonly=pdfonly
)
# Affecte aussi cache inscriptions
get_formsemestre_inscription_cache(context).inval_cache(key=formsemestre_id)
# Cache inscriptions semestres
2021-07-15 15:05:54 +02:00
def get_formsemestre_inscription_cache(context):
2021-06-17 00:08:37 +02:00
u = sco_mgr.get_db_uri()
2021-07-09 17:47:06 +02:00
if u in CACHE_formsemestre_inscription:
2021-06-13 23:37:14 +02:00
return CACHE_formsemestre_inscription[u]
else:
log("get_formsemestre_inscription_cache: new simpleCache")
CACHE_formsemestre_inscription[u] = sco_cache.simpleCache()
return CACHE_formsemestre_inscription[u]