Skip to content

Commit 68fed45

Browse files
authored
Remove make-key alias (#20)
1 parent 347a863 commit 68fed45

File tree

2 files changed

+8
-23
lines changed

2 files changed

+8
-23
lines changed

src/pgcachewatch/decorators.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import asyncio
2+
from functools import _make_key as make_key
23
from typing import Awaitable, Callable, Hashable, Literal, TypeVar
34

45
from typing_extensions import ParamSpec
56

6-
from pgcachewatch import strategies, utils
7+
from pgcachewatch import strategies
78
from pgcachewatch.logconfig import logger
89

910
P = ParamSpec("P")
@@ -12,7 +13,7 @@
1213

1314
def cache(
1415
strategy: strategies.Strategy,
15-
statistics_callback: Callable[[Literal["hit", "miss"]], None] | None = None,
16+
statistics_callback: Callable[[Literal["hit", "miss"]], None] = lambda _: None,
1617
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]:
1718
def outer(fn: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]:
1819
cached = dict[Hashable, asyncio.Future[T]]()
@@ -29,24 +30,20 @@ async def inner(*args: P.args, **kwargs: P.kwargs) -> T:
2930
logger.debug("Cache clear")
3031
cached.clear()
3132

32-
key = utils.make_key(args, kwargs)
33+
key = make_key(args, kwargs, typed=False)
3334

3435
try:
3536
waiter = cached[key]
3637
except KeyError:
3738
# Cache miss
38-
...
39+
logger.debug("Cache miss")
40+
statistics_callback("miss")
3941
else:
4042
# Cache hit
4143
logger.debug("Cache hit")
42-
if statistics_callback:
43-
statistics_callback("hit")
44+
statistics_callback("hit")
4445
return await waiter
4546

46-
logger.debug("Cache miss")
47-
if statistics_callback:
48-
statistics_callback("miss")
49-
5047
# Initialize Future to prevent cache stampedes.
5148
cached[key] = waiter = asyncio.Future[T]()
5249

src/pgcachewatch/utils.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
import asyncio
22
import datetime
3-
import functools
4-
from typing import Generator, Hashable
3+
from typing import Generator
54

65
import asyncpg
76

87
from pgcachewatch import listeners, models
98

109

11-
def make_key(
12-
args: tuple[Hashable, ...],
13-
kwds: dict,
14-
typed: bool = False,
15-
) -> Hashable:
16-
"""
17-
Create a cache key from the given function arguments and keyword arguments.
18-
"""
19-
return functools._make_key(args, kwds, typed)
20-
21-
2210
async def emit_event(
2311
conn: asyncpg.Connection | asyncpg.Pool,
2412
event: models.Event,

0 commit comments

Comments
 (0)