summaryrefslogtreecommitdiff
path: root/pylru.py
diff options
context:
space:
mode:
Diffstat (limited to 'pylru.py')
-rw-r--r--pylru.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/pylru.py b/pylru.py
index a6fce0f..4953b8b 100644
--- a/pylru.py
+++ b/pylru.py
@@ -501,7 +501,22 @@ class WriteBackCacheManager(object):
return False
+class FunctionCacheManager(object):
+ def __init__(self, func, size):
+ self.func = func
+ self.cache = lrucache(size)
+
+ def __call__(self, *args, **kwargs):
+ kwtuple = tuple((key, kwargs[key]) for key in sorted(kwargs.keys()))
+ key = (args, kwtuple)
+ try:
+ return self.cache[key]
+ except KeyError:
+ pass
+ value = self.func(*args, **kwargs)
+ self.cache[key] = value
+ return value
def lruwrap(store, size, writeback=False):
@@ -511,8 +526,6 @@ def lruwrap(store, size, writeback=False):
return WriteThroughCacheManager(store, size)
-
-
class lrudecorator(object):
def __init__(self, size):
self.cache = lrucache(size)