1- from ._cachebox import BaseCacheImpl , FIFOCache
2- from collections import namedtuple , defaultdict
3- import functools
4- import asyncio
51import _thread
2+ import asyncio
3+ import functools
64import inspect
75import typing
6+ from collections import defaultdict , namedtuple
87
8+ from ._cachebox import BaseCacheImpl , FIFOCache
99
1010KT = typing .TypeVar ("KT" )
1111VT = typing .TypeVar ("VT" )
@@ -46,6 +46,10 @@ def cache(self) -> BaseCacheImpl[KT, VT]:
4646 def maxsize (self ) -> int :
4747 return self .__cache .maxsize
4848
49+ @property
50+ def maxmemory (self ) -> int :
51+ return self .__cache .maxmemory
52+
4953 def __len__ (self ) -> int :
5054 return len (self .__cache )
5155
@@ -85,6 +89,9 @@ def __richcmp__(self, other: typing.Any, op: int) -> bool:
8589 def capacity (self ) -> int :
8690 return self .__cache .capacity ()
8791
92+ def memory (self ) -> int :
93+ return self .__cache .memory ()
94+
8895 def is_full (self ) -> bool :
8996 return self .__cache .is_full ()
9097
@@ -140,7 +147,9 @@ def shrink_to_fit(self) -> None:
140147
141148 def update (
142149 self ,
143- iterable : typing .Union [typing .Iterable [typing .Tuple [KT , VT ]], typing .Dict [KT , VT ]],
150+ iterable : typing .Union [
151+ typing .Iterable [typing .Tuple [KT , VT ]], typing .Dict [KT , VT ]
152+ ],
144153 * args ,
145154 ** kwargs ,
146155 ) -> None :
@@ -270,16 +279,22 @@ def _cached_wrapper(
270279 cache : typing .Union [BaseCacheImpl , typing .Callable ],
271280 key_maker : typing .Callable [[tuple , dict ], typing .Hashable ],
272281 clear_reuse : bool ,
273- callback : typing .Optional [typing .Callable [[int , typing .Any , typing .Any ], typing .Any ]],
282+ callback : typing .Optional [
283+ typing .Callable [[int , typing .Any , typing .Any ], typing .Any ]
284+ ],
274285 copy_level : int ,
275286 is_method : bool ,
276287):
277288 is_method = cache_is_function = inspect .isfunction (cache )
278- _key_maker = (lambda args , kwds : key_maker (args [1 :], kwds )) if is_method else key_maker
289+ _key_maker = (
290+ (lambda args , kwds : key_maker (args [1 :], kwds )) if is_method else key_maker
291+ )
279292
280293 hits = 0
281294 misses = 0
282- locks : defaultdict [typing .Hashable , _LockWithCounter ] = defaultdict (_LockWithCounter )
295+ locks : defaultdict [typing .Hashable , _LockWithCounter ] = defaultdict (
296+ _LockWithCounter
297+ )
283298 exceptions : typing .Dict [typing .Hashable , BaseException ] = {}
284299
285300 def _wrapped (* args , ** kwds ):
@@ -308,7 +323,9 @@ def _wrapped(*args, **kwds):
308323
309324 with locks [key ]:
310325 if exceptions .get (key , None ) is not None :
311- cached_error = exceptions [key ] if locks [key ].waiters > 1 else exceptions .pop (key )
326+ cached_error = (
327+ exceptions [key ] if locks [key ].waiters > 1 else exceptions .pop (key )
328+ )
312329 raise cached_error
313330
314331 try :
@@ -337,7 +354,7 @@ def _wrapped(*args, **kwds):
337354 if not cache_is_function :
338355 _wrapped .cache = cache
339356 _wrapped .cache_info = lambda : CacheInfo (
340- hits , misses , cache .maxsize , len (cache ), cache .capacity ()
357+ hits , misses , cache .maxsize , len (cache ), cache .memory ()
341358 )
342359
343360 _wrapped .callback = callback
@@ -362,12 +379,16 @@ def _async_cached_wrapper(
362379 cache : typing .Union [BaseCacheImpl , typing .Callable ],
363380 key_maker : typing .Callable [[tuple , dict ], typing .Hashable ],
364381 clear_reuse : bool ,
365- callback : typing .Optional [typing .Callable [[int , typing .Any , typing .Any ], typing .Any ]],
382+ callback : typing .Optional [
383+ typing .Callable [[int , typing .Any , typing .Any ], typing .Any ]
384+ ],
366385 copy_level : int ,
367386 is_method : bool ,
368387):
369388 is_method = cache_is_function = inspect .isfunction (cache )
370- _key_maker = (lambda args , kwds : key_maker (args [1 :], kwds )) if is_method else key_maker
389+ _key_maker = (
390+ (lambda args , kwds : key_maker (args [1 :], kwds )) if is_method else key_maker
391+ )
371392
372393 hits = 0
373394 misses = 0
@@ -404,7 +425,9 @@ async def _wrapped(*args, **kwds):
404425
405426 async with locks [key ]:
406427 if exceptions .get (key , None ) is not None :
407- cached_error = exceptions [key ] if locks [key ].waiters > 1 else exceptions .pop (key )
428+ cached_error = (
429+ exceptions [key ] if locks [key ].waiters > 1 else exceptions .pop (key )
430+ )
408431 raise cached_error
409432
410433 try :
@@ -435,7 +458,7 @@ async def _wrapped(*args, **kwds):
435458 if not cache_is_function :
436459 _wrapped .cache = cache
437460 _wrapped .cache_info = lambda : CacheInfo (
438- hits , misses , cache .maxsize , len (cache ), cache .capacity ()
461+ hits , misses , cache .maxsize , len (cache ), cache .memory ()
439462 )
440463
441464 _wrapped .callback = callback
@@ -459,7 +482,9 @@ def cached(
459482 cache : typing .Union [BaseCacheImpl , dict , None ],
460483 key_maker : typing .Callable [[tuple , dict ], typing .Hashable ] = make_key ,
461484 clear_reuse : bool = False ,
462- callback : typing .Optional [typing .Callable [[int , typing .Any , typing .Any ], typing .Any ]] = None ,
485+ callback : typing .Optional [
486+ typing .Callable [[int , typing .Any , typing .Any ], typing .Any ]
487+ ] = None ,
463488 copy_level : int = 1 ,
464489) -> typing .Callable [[FT ], FT ]:
465490 """
@@ -532,7 +557,9 @@ def cachedmethod(
532557 cache : typing .Union [BaseCacheImpl , dict , None ],
533558 key_maker : typing .Callable [[tuple , dict ], typing .Hashable ] = make_key ,
534559 clear_reuse : bool = False ,
535- callback : typing .Optional [typing .Callable [[int , typing .Any , typing .Any ], typing .Any ]] = None ,
560+ callback : typing .Optional [
561+ typing .Callable [[int , typing .Any , typing .Any ], typing .Any ]
562+ ] = None ,
536563 copy_level : int = 1 ,
537564) -> typing .Callable [[FT ], FT ]:
538565 """
0 commit comments