Skip to content

Commit acd2cc2

Browse files
committed
refactor: update cache decorator to use create_with method for TTLMapAsyncStorage
1 parent da961d1 commit acd2cc2

4 files changed

Lines changed: 7 additions & 7 deletions

File tree

cachium/_decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def cache(
7474
>>> import asyncio
7575
>>> from cachium import cache
7676
>>> from cachium.storages.ttl_map import TTLMapAsyncStorage
77-
>>> @cache(storage=lambda: TTLMapAsyncStorage())
77+
>>> @cache(storage=TTLMapAsyncStorage.create_with())
7878
... async def add_async(a: int, b: int) -> int:
7979
... return a + b
8080
>>> asyncio.run(add_async(1, 2))

cachium/storages/ttl_map.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def create_with(
153153
max_size: int | None = 1000,
154154
ttl: timedelta | None = timedelta(minutes=1),
155155
) -> Callable[..., Self]:
156-
"""Return a callable that creates a new instance of `TTLMapAsyncStorage` with the specified parameters."""
156+
"""Return a callable that creates a new instance of `TTLMapStorage` with the specified parameters."""
157157
return lambda: cls(max_size=max_size, ttl=ttl)
158158

159159
@override

docs/guides/quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ from py_cashier import cache
3232
from py_cashier.storages.ttl_map import TTLMapAsyncStorage
3333

3434
# Configure TTL and max size for async storage
35-
@cache(storage=lambda: TTLMapAsyncStorage(max_size=512, ttl=timedelta(seconds=30)))
35+
@cache(storage=TTLMapAsyncStorage.create_with(max_size=512, ttl=timedelta(seconds=30)))
3636
async def add_async(a: int, b: int) -> int:
3737
# Simulate I/O
3838
await asyncio.sleep(0.1)

tests/test__decorator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async def test_cache_async(a: int, b: int, expected: int) -> None:
5858
"""Test that the cache decorator works correctly for asynchronous functions."""
5959
calls: dict[tuple[int, int], int] = defaultdict(int)
6060

61-
@cache(storage=lambda: TTLMapAsyncStorage(max_size=1000, ttl=timedelta(seconds=1)))
61+
@cache(storage=TTLMapAsyncStorage.create_with(max_size=1000, ttl=timedelta(seconds=1)))
6262
async def func(x: int, y: int) -> int:
6363
nonlocal calls
6464
calls[(x, y)] += 1
@@ -91,7 +91,7 @@ async def test_cache_dog_piling_async(a: int, b: int, expected: int, tasks_count
9191
"""
9292
calls = 0
9393

94-
@cache(storage=lambda: TTLMapAsyncStorage(max_size=1000, ttl=timedelta(seconds=1)))
94+
@cache(storage=TTLMapAsyncStorage.create_with(max_size=1000, ttl=timedelta(seconds=1)))
9595
async def func(x: int, y: int) -> int:
9696
await asyncio.sleep(0.1)
9797
nonlocal calls
@@ -192,7 +192,7 @@ async def test_cache_failing_func_async(a: int, b: int, tasks_count: int) -> Non
192192
"""Test that failing functions are not cached in asynchronous context."""
193193
calls = 0
194194

195-
@cache(storage=lambda: TTLMapAsyncStorage(max_size=1000, ttl=timedelta(seconds=1)))
195+
@cache(storage=TTLMapAsyncStorage.create_with(max_size=1000, ttl=timedelta(seconds=1)))
196196
async def func(x: int, y: int) -> int:
197197
await asyncio.sleep(0.1)
198198
nonlocal calls
@@ -227,7 +227,7 @@ def _f() -> None:
227227
("func", "storage"),
228228
[
229229
(_af, TTLMapStorage.create_with(max_size=1000, ttl=timedelta(seconds=1))),
230-
(_f, lambda: TTLMapAsyncStorage(max_size=1000, ttl=timedelta(seconds=1))),
230+
(_f, TTLMapAsyncStorage.create_with(max_size=1000, ttl=timedelta(seconds=1))),
231231
],
232232
)
233233
def test__decorator__invalid_storage(func: Callable[..., Any], storage: PStorage | PAsyncStorage) -> None:

0 commit comments

Comments
 (0)