summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Hutchinson <jlhutch@gmail.com>2015-03-17 23:52:56 -0500
committerJay Hutchinson <jlhutch@gmail.com>2015-03-17 23:52:56 -0500
commitb893123f3efa8aec051cc81c4ceb7c79df803379 (patch)
treea7f737bdf0c5a63576fe35b21943a1be69c48133
parentd9ef20b0e3452034824e8f63faf9638a68bb695a (diff)
downloadpylru-b893123f3efa8aec051cc81c4ceb7c79df803379.tar.gz
Added FunctionCacheManager.
-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)