lonelyenvoy / python-memoization

A powerful caching library for Python, with TTL support and multiple algorithm options.
MIT License
230 stars 15 forks source link

[feature] item_size function #20

Open FreakTheMighty opened 3 years ago

FreakTheMighty commented 3 years ago

Overview

When determining how large the cache using max_size is it may be useful to treat some items as larger than other to provide a better proxy for their memory footprint. For example, I have a function that caches 3D meshes. Setting a max_size to a fixed number doesn't capture the fact that some 3D meshes are very large while others are very small.

Proposal

Similar to the custom_key_maker, a developer could provide an item_size function that returns an integer allowing them to calculate the size of cached items based on the cache entry. In the use-case described above, I might return a value based on the number of vertexes in my mesh.