@@ -15,20 +15,21 @@ class ThrottlerStorageService(IThrottlerStorage):
1515 def __init__ (self ) -> None :
1616 self ._storage : t .Dict [str , ThrottlerStorageOption ] = {}
1717
18- def _set (self , key : str , value : ThrottlerStorageOption ) -> None :
19- self ._storage [key ] = value
18+ def _set (self , key : str , value : ThrottlerStorageOption ) -> ThrottlerStorageOption :
19+ self .storage [key ] = value
20+ return value
2021
2122 def _has_expired (self , key : str ) -> bool :
22- exp = self ._storage .get (key )
23+ exp = self .storage .get (key )
2324 return exp is not None and exp .expires_at <= time .time ()
2425
2526 def _has_key (self , key : str ) -> bool :
2627 return self ._get (key ) is not None
2728
2829 def _delete (self , key : str ) -> bool :
2930 try :
30- self ._storage .pop (key )
31- except KeyError :
31+ self .storage .pop (key )
32+ except KeyError : # pragma: no cover
3233 return False
3334 return True
3435
@@ -37,14 +38,17 @@ def _get(self, key: str) -> t.Optional[ThrottlerStorageOption]:
3738 self ._delete (key )
3839 return None
3940
40- return self ._storage .get (key )
41+ return self .storage .get (key )
4142
4243 @property
4344 def storage (self ) -> t .Dict [str , ThrottlerStorageOption ]:
4445 return self ._storage
4546
4647 def get_expiration_time (self , key : str ) -> int :
47- return math .floor (self .storage [key ].expires_at - time .time ())
48+ cache = self ._get (key )
49+ if cache is None : # pragma: no cover
50+ return - 1
51+ return math .floor (cache .expires_at - time .time ())
4852
4953 async def increment (self , key : str , ttl : int ) -> ThrottlerStorageRecord :
5054 if not self ._has_key (key ):
@@ -53,20 +57,20 @@ async def increment(self, key: str, ttl: int) -> ThrottlerStorageRecord:
5357 )
5458
5559 history = self ._get (key )
56- assert history , "value can not be None"
57- now = time .time ()
5860
5961 time_to_expire = self .get_expiration_time (key )
6062
6163 if time_to_expire <= 0 :
62- history .total_hits = 0
63- history .expires_at = now + ttl
64+ history = self ._set (
65+ key , ThrottlerStorageOption (total_hits = 0 , expires_at = time .time () + ttl )
66+ )
6467 time_to_expire = self .get_expiration_time (key )
6568
66- history .total_hits += 1
69+ history .total_hits += 1 # type:ignore[union-attr]
6770
6871 return ThrottlerStorageRecord (
69- total_hits = history .total_hits , time_to_expire = time_to_expire
72+ total_hits = history .total_hits , # type:ignore[union-attr]
73+ time_to_expire = time_to_expire ,
7074 )
7175
7276
@@ -76,7 +80,7 @@ def __init__(self, cache_service: ICacheService) -> None:
7680 self ._cache_service = cache_service
7781
7882 @property
79- def storage (self ) -> t .Any :
83+ def storage (self ) -> t .Any : # pragma: no cover
8084 return self ._cache_service .get_backend ()
8185
8286 async def get_expiration_time (self , key : str ) -> int :
0 commit comments