Add additional_key to memoization function

Closes #29.
This commit is contained in:
ZyX 2013-01-05 17:35:06 +04:00 committed by Kim Silkebækken
parent 5b43960e0e
commit 332dc45bf9
2 changed files with 7 additions and 3 deletions

View File

@ -163,7 +163,7 @@ def branch():
# TODO Drop cache on BufWrite event
@memoize(2)
@memoize(2, additional_key=lambda: vim.current.buffer.number)
def file_vcs_status():
if vim.current.buffer.name and not vim.eval('&buftype'):
repo = guess(os.path.abspath(vim.current.buffer.name))

View File

@ -11,8 +11,9 @@ class memoize(object):
_caches = {}
_timeouts = {}
def __init__(self, timeout):
def __init__(self, timeout, additional_key=None):
self.timeout = timeout
self.additional_key = additional_key
def collect(self):
'''Clear cache of results which have timed out.
@ -31,7 +32,10 @@ class memoize(object):
def func(*args, **kwargs):
kw = kwargs.items()
kw.sort()
key = (args, tuple(kw))
if self.additional_key:
key = (args, tuple(kw), self.additional_key())
else:
key = (args, tuple(kw))
try:
v = self.cache[key]
if (time.time() - v[1]) > self.timeout: