Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a CacheManager class to manage storing and clearing caches #1782

Merged
merged 3 commits into from Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog
Expand Up @@ -17,7 +17,9 @@ What's New in astroid 2.12.10?
==============================
Release date: TBA

* ``decorators.cached`` now gets its cache cleared by calling ``AstroidManager.clear_cache``.

Refs #1780

What's New in astroid 2.12.9?
=============================
Expand Down
25 changes: 25 additions & 0 deletions astroid/_cache.py
@@ -0,0 +1,25 @@
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
from __future__ import annotations
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved

from typing import Any


class CacheManager:
"""Manager of caches, to be used as a singleton."""

def __init__(self) -> None:
self.dict_caches: list[dict[Any, Any]] = []

def clear_all_caches(self) -> None:
"""Clear all caches."""
for dict_cache in self.dict_caches:
dict_cache.clear()

def add_dict_cache(self, cache: dict[Any, Any]) -> None:
"""Add a dictionary cache to the manager."""
self.dict_caches.append(cache)


CACHE_MANAGER = CacheManager()
3 changes: 2 additions & 1 deletion astroid/decorators.py
Expand Up @@ -15,7 +15,7 @@

import wrapt

from astroid import util
from astroid import _cache, util
from astroid.context import InferenceContext
from astroid.exceptions import InferenceError

Expand All @@ -34,6 +34,7 @@ def cached(func, instance, args, kwargs):
cache = getattr(instance, "__cache", None)
if cache is None:
instance.__cache = cache = {}
_cache.CACHE_MANAGER.add_dict_cache(cache)
try:
return cache[func]
except KeyError:
Expand Down
3 changes: 3 additions & 0 deletions astroid/manager.py
Expand Up @@ -16,6 +16,7 @@
from importlib.util import find_spec, module_from_spec
from typing import TYPE_CHECKING, ClassVar

from astroid._cache import CACHE_MANAGER
from astroid.const import BRAIN_MODULES_DIRECTORY
from astroid.exceptions import AstroidBuildingError, AstroidImportError
from astroid.interpreter._import import spec, util
Expand Down Expand Up @@ -391,6 +392,8 @@ def clear_cache(self) -> None:
# NB: not a new TransformVisitor()
AstroidManager.brain["_transform"].transforms = collections.defaultdict(list)

CACHE_MANAGER.clear_all_caches()

for lru_cache in (
LookupMixIn.lookup,
_cache_normalize_path_,
Expand Down