Setting cachetools LRUCache with maximum size

Viewed 1074

Trying to set cachetools cache class - more specifically the LRUCache inheriting from it.
I want to set maxsize based on bytes - which means I need to set getsizeof parameter with some lambda function for calculation of object's size of in bytes.

Looking into sys.getsizeof, we can see it's not suitable as I'm about to save in cache custom class with referenced\handles to other objects which will be the critic mass.

Any ideas how can I calculate real bytes in memory for python custom object and what it contains, meaning calculating also referenced size (not pointer size but its content)

Thanks

1 Answers

The simplest way is to use the pympler package, they've implemented the function you want already.

python -m pip install pympler
from pympler import asizeof
from cachetools import LRUCache

@LRUCache(MAX_BYTES, getsizeof=asizeof.asizeof)
def foo():
    pass
Related