You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/07/08 01:22:17 UTC

[GitHub] [superset] zhaoyongjie commented on a diff in pull request #17319: fix(core): memory leak in memorized decorator

zhaoyongjie commented on code in PR #17319:
URL: https://github.com/apache/superset/pull/17319#discussion_r916386090


##########
superset/utils/memoized.py:
##########
@@ -15,67 +15,33 @@
 # specific language governing permissions and limitations
 # under the License.
 import functools
-from typing import Any, Callable, Dict, Optional, Tuple, Type
+from datetime import datetime, timedelta
+from typing import Any, Callable
 
 
-class _memoized:
-    """Decorator that caches a function's return value each time it is called
-
-    If called later with the same arguments, the cached value is returned, and
-    not re-evaluated.
-
-    Define ``watch`` as a tuple of attribute names if this Decorator
-    should account for instance variable changes.
+def _memoized(seconds: int = 24 * 60 * 60, maxsize: int = 1024,) -> Callable[..., Any]:
+    """
+    A simple wrapper of functools.lru_cache, encapsulated for thread safety
+    :param seconds: LRU expired time, seconds
+    :param maxsize: LRU size
+    :return: a wrapped function by LRU
     """
 
-    def __init__(
-        self, func: Callable[..., Any], watch: Optional[Tuple[str, ...]] = None
-    ) -> None:
-        self.func = func
-        self.cache: Dict[Any, Any] = {}
-        self.is_method = False
-        self.watch = watch or ()
-
-    def __call__(self, *args: Any, **kwargs: Any) -> Any:
-        key = [args, frozenset(kwargs.items())]
-        if self.is_method:
-            key.append(tuple(getattr(args[0], v, None) for v in self.watch))
-        key = tuple(key)  # type: ignore
-        try:
-            if key in self.cache:
-                return self.cache[key]
-        except TypeError as ex:
-            # Uncachable -- for instance, passing a list as an argument.
-            raise TypeError("Function cannot be memoized") from ex
-        value = self.func(*args, **kwargs)
-        try:
-            self.cache[key] = value
-        except TypeError as ex:
-            raise TypeError("Function cannot be memoized") from ex
-        return value
-
-    def __repr__(self) -> str:
-        """Return the function's docstring."""
-        return self.func.__doc__ or ""
+    def wrapper_cache(func: Callable[..., Any]) -> Callable[..., Any]:

Review Comment:
   the argument of the function is implicit `watch` hash id.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org