From 22b0c0f1fec117ba3f5eb835746297155c1e0ac3 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 15 Apr 2018 15:51:16 +0200 Subject: [PATCH] Rework the time cache. --- jedi/api/environment.py | 2 +- jedi/api/helpers.py | 4 ++-- jedi/cache.py | 25 ++++++++++++++++++++++++- jedi/settings.py | 2 -- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/jedi/api/environment.py b/jedi/api/environment.py index 40d5e626..b7d8c1db 100644 --- a/jedi/api/environment.py +++ b/jedi/api/environment.py @@ -170,7 +170,7 @@ def get_default_environment(): return SameEnvironment() -@time_cache("_default_environment_validity") +@time_cache(seconds=10 * 60) # 10 Minutes def get_cached_default_environment(): return get_default_environment() diff --git a/jedi/api/helpers.py b/jedi/api/helpers.py index 878ebf66..221fc4df 100644 --- a/jedi/api/helpers.py +++ b/jedi/api/helpers.py @@ -13,7 +13,7 @@ from jedi.evaluate.syntax_tree import eval_atom from jedi.evaluate.helpers import evaluate_call_of_leaf from jedi.evaluate.compiled import get_string_context_set from jedi.evaluate.base_context import ContextSet -from jedi.cache import time_cache +from jedi.cache import call_signature_time_cache CompletionParts = namedtuple('CompletionParts', ['path', 'has_dot', 'name']) @@ -283,7 +283,7 @@ def get_call_signature_details(module, position): return None -@time_cache("call_signatures_validity") +@call_signature_time_cache("call_signatures_validity") def cache_call_signatures(evaluator, context, bracket_leaf, code_lines, user_pos): """This function calculates the cache key.""" line_index = user_pos[0] - 1 diff --git a/jedi/cache.py b/jedi/cache.py index 01138e75..a1e07915 100644 --- a/jedi/cache.py +++ b/jedi/cache.py @@ -12,6 +12,7 @@ there are global variables, which are holding the cache information. Some of these variables are being cleaned after every API usage. """ import time +from functools import wraps from jedi import settings from parso.cache import parser_cache @@ -74,7 +75,7 @@ def clear_time_caches(delete_all=False): del tc[key] -def time_cache(time_add_setting): +def call_signature_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. @@ -106,6 +107,28 @@ def time_cache(time_add_setting): return _temp +def time_cache(seconds): + def decorator(func): + cache = {} + + @wraps(func) + def wrapper(*args, **kwargs): + key = (args, frozenset(kwargs.items())) + try: + created, result = cache[key] + if time.time() < created + seconds: + return result + except KeyError: + pass + result = func(*args, **kwargs) + cache[key] = time.time(), result + return result + + wrapper.clear_cache = lambda: cache.clear() + return wrapper + return decorator + + def memoize_method(method): """A normal memoize function.""" def wrapper(self, *args, **kwargs): diff --git a/jedi/settings.py b/jedi/settings.py index bbd3a378..f1ae6dbb 100644 --- a/jedi/settings.py +++ b/jedi/settings.py @@ -161,5 +161,3 @@ call_signatures_validity = 3.0 Finding function calls might be slow (0.1-0.5s). This is not acceptible for normal writing. Therefore cache it for a short time. """ - -_default_environment_validity = 10 * 60 # 0 Minutes