about summary refs log tree commit diff
path: root/.venv/lib/python3.12/site-packages/litellm/caching/_internal_lru_cache.py
diff options
context:
space:
mode:
Diffstat (limited to '.venv/lib/python3.12/site-packages/litellm/caching/_internal_lru_cache.py')
-rw-r--r--.venv/lib/python3.12/site-packages/litellm/caching/_internal_lru_cache.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/litellm/caching/_internal_lru_cache.py b/.venv/lib/python3.12/site-packages/litellm/caching/_internal_lru_cache.py
new file mode 100644
index 00000000..54b0fe96
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/litellm/caching/_internal_lru_cache.py
@@ -0,0 +1,30 @@
+from functools import lru_cache
+from typing import Callable, Optional, TypeVar
+
+T = TypeVar("T")
+
+
+def lru_cache_wrapper(
+    maxsize: Optional[int] = None,
+) -> Callable[[Callable[..., T]], Callable[..., T]]:
+    """
+    Wrapper for lru_cache that caches success and exceptions
+    """
+
+    def decorator(f: Callable[..., T]) -> Callable[..., T]:
+        @lru_cache(maxsize=maxsize)
+        def wrapper(*args, **kwargs):
+            try:
+                return ("success", f(*args, **kwargs))
+            except Exception as e:
+                return ("error", e)
+
+        def wrapped(*args, **kwargs):
+            result = wrapper(*args, **kwargs)
+            if result[0] == "error":
+                raise result[1]
+            return result[1]
+
+        return wrapped
+
+    return decorator