11import asyncio
2+ from functools import _make_key as make_key
23from typing import Awaitable , Callable , Hashable , Literal , TypeVar
34
45from typing_extensions import ParamSpec
56
6- from pgcachewatch import strategies , utils
7+ from pgcachewatch import strategies
78from pgcachewatch .logconfig import logger
89
910P = ParamSpec ("P" )
1213
1314def 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
0 commit comments