diff --git a/util/cache.py b/util/cache.py new file mode 100644 index 0000000..de01c8b --- /dev/null +++ b/util/cache.py @@ -0,0 +1,68 @@ +# +# Copyright (c) 2009 Helmut Merz helmutm@cy55.de +# +# 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 +# + +""" +A simple caching mechanism. + +$Id$ +""" + +from cybertools.util.date import getTimeStamp + +INVALID = object() + + +class CacheManager(dict): + + pass + +manager = CacheManager() + + +class CacheItem(object): + + def __init__(self, identifier, value=INVALID, lifetime=3600): + self.identifier = identifier + self.lifetime = lifetime + self.set(value) + + def set(self, value): + self.value = value + self.modified = getTimeStamp() + + def get(self): + self.check() + return self.value + + def check(self): + if getTimeStamp() - self.modified > self.lifetime: + self.value = INVALID + + +def cache(getIdentifier, lifetime=3600): + def _cache(fct): + def __cache(*args, **kw): + id = getIdentifier(*args) + item = manager.setdefault(id, CacheItem(id, lifetime=lifetime)) + value = item.get() + if value is INVALID: + value = fct(*args, **kw) + item.set(value) + return value + return __cache + return _cache diff --git a/util/cache.txt b/util/cache.txt new file mode 100644 index 0000000..e6ed568 --- /dev/null +++ b/util/cache.txt @@ -0,0 +1,41 @@ +============ +Data Caching +============ + +$Id$ + + >>> from cybertools.util.cache import cache + + >>> @cache(lambda *args: 'calc') + ... def calculate(): + ... print 'calculating' + ... return 42 + + >>> calculate() + calculating + 42 + >>> calculate() + 42 + + >>> class Demo(object): + ... id = '4711' + ... def getId(self, *args): + ... return self.id + ... @cache(getId) + ... def calculate(self): + ... print 'calculating' + ... return 42 + + >>> demo = Demo() + >>> demo.calculate() + calculating + 42 + >>> demo.calculate() + 42 + + >>> demo.id = '4712' + >>> demo.calculate() + calculating + 42 + >>> demo.calculate() + 42 diff --git a/util/tests.py b/util/tests.py index 75b23df..0ac1c0f 100755 --- a/util/tests.py +++ b/util/tests.py @@ -20,6 +20,7 @@ def test_suite(): #doctest.DocTestSuite(cybertools.util.property, optionflags=flags), doctest.DocFileSuite('adapter.txt', optionflags=flags), doctest.DocFileSuite('aop.txt', optionflags=flags), + doctest.DocFileSuite('cache.txt', optionflags=flags), doctest.DocFileSuite('config.txt', optionflags=flags), doctest.DocFileSuite('defer.txt', optionflags=flags), doctest.DocFileSuite('format.txt', optionflags=flags),