forked from VimPlug/jedi
add get_in_function_call caching (3s for now)
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import time
|
||||
import settings
|
||||
|
||||
# memoize caches will be deleted after every action
|
||||
memoize_caches = []
|
||||
|
||||
time_caches = []
|
||||
|
||||
|
||||
def clear_caches():
|
||||
""" Jedi caches many things, that should be completed after each completion
|
||||
@@ -13,6 +18,8 @@ def clear_caches():
|
||||
for m in memoize_caches:
|
||||
m.clear()
|
||||
|
||||
time_caches = []
|
||||
|
||||
|
||||
def memoize_default(default=None, cache=memoize_caches):
|
||||
""" This is a typical memoization decorator, BUT there is one difference:
|
||||
@@ -47,3 +54,34 @@ class CachedMetaClass(type):
|
||||
@memoize_default()
|
||||
def __call__(self, *args, **kwargs):
|
||||
return super(CachedMetaClass, self).__call__(*args, **kwargs)
|
||||
|
||||
|
||||
def time_cache(time_add_setting):
|
||||
""" This decorator works as follows: Call it with a setting and after that
|
||||
use the function with a callable that returns the key.
|
||||
But: This function is only called if the key is not available. After a
|
||||
certain amount of time (`time_add_setting`) the cache is invalid.
|
||||
"""
|
||||
def _temp(key_func):
|
||||
dct = {}
|
||||
time_caches.append(dct)
|
||||
def wrapper(optional_callable, *args, **kwargs):
|
||||
key = key_func(*args, **kwargs)
|
||||
value = None
|
||||
if key in dct:
|
||||
expiry, value = dct[key]
|
||||
if expiry > time.time():
|
||||
return value
|
||||
value = optional_callable()
|
||||
time_add = getattr(settings, time_add_setting)
|
||||
if key is not None:
|
||||
dct[key] = time.time() + time_add, value
|
||||
return value
|
||||
return wrapper
|
||||
return _temp
|
||||
|
||||
|
||||
@time_cache("get_in_function_call_validity")
|
||||
def cache_get_in_function_call(stmt):
|
||||
module_path = stmt.get_parent_until().path
|
||||
return None if module_path is None else (module_path, stmt.start_pos)
|
||||
|
||||
Reference in New Issue
Block a user