Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see current cache size when using functools.lru_cache?

Tags:

python

I am doing performance/memory analysis on a certain method that is wrapped with the functools.lru_cache decorator. I want to see how to inspect the current size of my cache without doing some crazy inspect magic to get to the underlying cache.

Does anyone know how to see the current cache size of method decorated with functools.lru_cache?

like image 993
JarbingleMan Avatar asked Apr 21 '26 15:04

JarbingleMan


1 Answers

Digging around in the docs showed the answer is calling .cache_info() on the method.

To help measure the effectiveness of the cache and tune the maxsize parameter, the wrapped function is instrumented with a cache_info() function that returns a named tuple showing hits, misses, maxsize and currsize. In a multi-threaded environment, the hits and misses are approximate.

like image 139
JarbingleMan Avatar answered Apr 23 '26 05:04

JarbingleMan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!