diff options
| author | Panos Christeas <xrg@linux.gr> | 2009-11-26 19:24:00 +0530 |
|---|---|---|
| committer | Harry (Open ERP) <hmo@tinyerp.com> | 2009-11-26 19:24:00 +0530 |
| commit | 8d0b4a741816302a30efa28442563afefea8b9cd (patch) | |
| tree | b55e82f0b338715645c6e3a584ba0231fa66e390 /addons/document_webdav/cache.py | |
| parent | e65f61d4920949bbcdff1577624d36422c9f5473 (diff) | |
[MERGE] merge from lp:~xrg/openobject-addons/doc2-5.2
bzr revid: hmo@tinyerp.com-20091126135400-z32iycvadbt9so8n
Diffstat (limited to 'addons/document_webdav/cache.py')
| -rw-r--r-- | addons/document_webdav/cache.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/addons/document_webdav/cache.py b/addons/document_webdav/cache.py new file mode 100644 index 00000000000..bc75114c4ae --- /dev/null +++ b/addons/document_webdav/cache.py @@ -0,0 +1,39 @@ +import time +import heapq + +def memoize(maxsize): + """decorator to 'memoize' a function - caching its results""" + def decorating_function(f): + cache = {} # map from key to value + heap = [] # list of keys, in LRU heap + cursize = 0 # because len() is slow + def wrapper(*args): + key = repr(args) + # performance crap + _cache=cache + _heap=heap + _heappop = heapq.heappop + _heappush = heapq.heappush + _time = time.time + _cursize = cursize + _maxsize = maxsize + if not _cache.has_key(key): + if _cursize == _maxsize: + # pop oldest element + (_,oldkey) = _heappop(_heap) + _cache.pop(oldkey) + else: + _cursize += 1 + # insert this element + _cache[key] = f(*args) + _heappush(_heap,(_time(),key)) + wrapper.misses += 1 + else: + wrapper.hits += 1 + return cache[key] + wrapper.__doc__ = f.__doc__ + wrapper.__name__ = f.__name__ + wrapper.hits = wrapper.misses = 0 + return wrapper + return decorating_function + |
