MonScoDocEssai/app/scodoc/notes_cache.py

66 lines
2.1 KiB
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
2021-01-01 17:51:08 +01:00
# Copyright (c) 1999 - 2021 Emmanuel Viennet. All rights reserved.
2020-09-26 16:19:37 +02:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Emmanuel Viennet emmanuel.viennet@viennet.net
#
##############################################################################
"""Gestion rudimentaire de cache pour Notes
NOTA: inutilisable dans une instance Zope (can't pickle functions)
"""
from app.scodoc.notes_log import log
2020-09-26 16:19:37 +02:00
class CacheFunc(object):
2020-09-26 16:19:37 +02:00
"""gestion rudimentaire de cache pour une fonction
func doit etre sans effet de bord, et sans arguments nommés
"""
def __init__(self, func):
log("new CacheFunc for %s" % str(func))
self.func = func
self.cache = {} # { arguments : function result }
def __call__(self, *args):
2021-07-09 16:29:45 +02:00
if args in self.cache:
2020-09-26 16:19:37 +02:00
# log('cache hit %s' % str(args))
return self.cache[args]
else:
val = self.func(*args)
self.cache[args] = val
log("caching %s(%s)" % (str(self.func), str(args)))
return val
def inval_cache_entry(self, *args): # >
"expire cache for these args"
log("inval_cache_entry %s(%s)" % (str(self.func), str(args))) # >
del self.cache[args]
def inval_cache(self): # >
"clear whole cache"
2021-01-01 18:40:47 +01:00
log("inval_cache %s" % (str(self.func))) # >
2020-09-26 16:19:37 +02:00
self.cache = {}