forked from VimPlug/jedi
Rework the time cache.
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user