title: Memory-aware LRU caching in Python?
body: <p>I'm using Python 3's builtin <code>functools.lru_cache</code> decorator to memoize some expensive functions. I would like to memoize as many calls as possible without using too much memory, since caching too many values causes thrashing.</p>
  
  <p>Is there a preferred technique or library for accomplishing this in Python?</p>
  
  <p>For example, <a href="https://stackoverflow.com/questions/20000404/automatic-cache-eviction-based-on-memory-consumption">this question</a> lead me to a Go library for <a href="https://github.com/eaigner/last" rel="noreferrer">system memory aware LRU caching</a>. Something similar for Python would be ideal.</p>
  
  <hr>
  
  <p><strong>Note:</strong> I can't just estimate the memory used per value and set <code>maxsize</code> accordingly, since several processes will be calling the decorated function in parallel; a solution would need to actually dynamically check how much memory is free.</p>