Skip to content

Commit 0644a2b

Browse files
authored
Add in memory cache class (#2266)
* Add in memory cache class * formatting
1 parent 2053dd9 commit 0644a2b

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

autogen/cache/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from .cache import Cache, AbstractCache
1+
from .abstract_cache_base import AbstractCache
2+
from .cache import Cache
23

34
__all__ = ["Cache", "AbstractCache"]

autogen/cache/in_memory_cache.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from types import TracebackType
2+
from typing import Any, Dict, Optional, Type, Union
3+
import sys
4+
from .abstract_cache_base import AbstractCache
5+
6+
if sys.version_info >= (3, 11):
7+
from typing import Self
8+
else:
9+
from typing_extensions import Self
10+
11+
12+
class InMemoryCache(AbstractCache):
13+
14+
def __init__(self, seed: Union[str, int] = ""):
15+
self._seed = str(seed)
16+
self._cache: Dict[str, Any] = {}
17+
18+
def _prefixed_key(self, key: str) -> str:
19+
separator = "_" if self._seed else ""
20+
return f"{self._seed}{separator}{key}"
21+
22+
def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
23+
result = self._cache.get(self._prefixed_key(key))
24+
if result is None:
25+
return default
26+
return result
27+
28+
def set(self, key: str, value: Any) -> None:
29+
self._cache[self._prefixed_key(key)] = value
30+
31+
def close(self) -> None:
32+
pass
33+
34+
def __enter__(self) -> Self:
35+
"""
36+
Enter the runtime context related to the object.
37+
38+
Returns:
39+
self: The instance itself.
40+
"""
41+
return self
42+
43+
def __exit__(
44+
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
45+
) -> None:
46+
"""
47+
Exit the runtime context related to the object.
48+
49+
Args:
50+
exc_type: The exception type if an exception was raised in the context.
51+
exc_value: The exception value if an exception was raised in the context.
52+
traceback: The traceback if an exception was raised in the context.
53+
"""
54+
self.close()

test/cache/test_in_memory_cache.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from autogen.cache.in_memory_cache import InMemoryCache
2+
3+
4+
def test_prefixed_key():
5+
cache = InMemoryCache(seed="test")
6+
assert cache._prefixed_key("key") == "test_key"
7+
8+
9+
def test_get_with_default_value():
10+
cache = InMemoryCache()
11+
assert cache.get("key", "default_value") == "default_value"
12+
13+
14+
def test_get_without_default_value():
15+
cache = InMemoryCache()
16+
assert cache.get("key") is None
17+
18+
19+
def test_get_with_set_value():
20+
cache = InMemoryCache()
21+
cache.set("key", "value")
22+
assert cache.get("key") == "value"
23+
24+
25+
def test_get_with_set_value_and_seed():
26+
cache = InMemoryCache(seed="test")
27+
cache.set("key", "value")
28+
assert cache.get("key") == "value"
29+
30+
31+
def test_set():
32+
cache = InMemoryCache()
33+
cache.set("key", "value")
34+
assert cache._cache["key"] == "value"
35+
36+
37+
def test_set_with_seed():
38+
cache = InMemoryCache(seed="test")
39+
cache.set("key", "value")
40+
assert cache._cache["test_key"] == "value"
41+
42+
43+
def test_context_manager():
44+
with InMemoryCache() as cache:
45+
cache.set("key", "value")
46+
assert cache.get("key") == "value"

0 commit comments

Comments
 (0)