Rework the time cache.

This commit is contained in:
Dave Halter
2018-04-15 15:51:16 +02:00
parent a972d49e88
commit 22b0c0f1fe
4 changed files with 27 additions and 6 deletions

View File

@@ -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()

View File

@@ -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

View File

@@ -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):

View File

@@ -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