cache.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. This caching is very important for speed and memory optimizations. There's
  3. nothing really spectacular, just some decorators. The following cache types are
  4. available:
  5. - ``time_cache`` can be used to cache something for just a limited time span,
  6. which can be useful if there's user interaction and the user cannot react
  7. faster than a certain time.
  8. This module is one of the reasons why |jedi| is not thread-safe. As you can see
  9. there are global variables, which are holding the cache information. Some of
  10. these variables are being cleaned after every API usage.
  11. """
  12. import time
  13. from functools import wraps
  14. from typing import Any, Dict, Tuple
  15. from jedi import settings
  16. from parso.cache import parser_cache
  17. _time_caches: Dict[str, Dict[Any, Tuple[float, Any]]] = {}
  18. def clear_time_caches(delete_all: bool = False) -> None:
  19. """ Jedi caches many things, that should be completed after each completion
  20. finishes.
  21. :param delete_all: Deletes also the cache that is normally not deleted,
  22. like parser cache, which is important for faster parsing.
  23. """
  24. global _time_caches
  25. if delete_all:
  26. for cache in _time_caches.values():
  27. cache.clear()
  28. parser_cache.clear()
  29. else:
  30. # normally just kill the expired entries, not all
  31. for tc in _time_caches.values():
  32. # check time_cache for expired entries
  33. for key, (t, value) in list(tc.items()):
  34. if t < time.time():
  35. # delete expired entries
  36. del tc[key]
  37. def signature_time_cache(time_add_setting):
  38. """
  39. This decorator works as follows: Call it with a setting and after that
  40. use the function with a callable that returns the key.
  41. But: This function is only called if the key is not available. After a
  42. certain amount of time (`time_add_setting`) the cache is invalid.
  43. If the given key is None, the function will not be cached.
  44. """
  45. def _temp(key_func):
  46. dct = {}
  47. _time_caches[time_add_setting] = dct
  48. def wrapper(*args, **kwargs):
  49. generator = key_func(*args, **kwargs)
  50. key = next(generator)
  51. try:
  52. expiry, value = dct[key]
  53. if expiry > time.time():
  54. return value
  55. except KeyError:
  56. pass
  57. value = next(generator)
  58. time_add = getattr(settings, time_add_setting)
  59. if key is not None:
  60. dct[key] = time.time() + time_add, value
  61. return value
  62. return wrapper
  63. return _temp
  64. def time_cache(seconds):
  65. def decorator(func):
  66. cache = {}
  67. @wraps(func)
  68. def wrapper(*args, **kwargs):
  69. key = (args, frozenset(kwargs.items()))
  70. try:
  71. created, result = cache[key]
  72. if time.time() < created + seconds:
  73. return result
  74. except KeyError:
  75. pass
  76. result = func(*args, **kwargs)
  77. cache[key] = time.time(), result
  78. return result
  79. wrapper.clear_cache = lambda: cache.clear()
  80. return wrapper
  81. return decorator
  82. def memoize_method(method):
  83. """A normal memoize function."""
  84. @wraps(method)
  85. def wrapper(self, *args, **kwargs):
  86. cache_dict = self.__dict__.setdefault('_memoize_method_dct', {})
  87. dct = cache_dict.setdefault(method, {})
  88. key = (args, frozenset(kwargs.items()))
  89. try:
  90. return dct[key]
  91. except KeyError:
  92. result = method(self, *args, **kwargs)
  93. dct[key] = result
  94. return result
  95. return wrapper