triava
triava copied to clipboard
Cache limited by Object weighed
tcache supports limiting the maximum number of entries by element count. That works well if elements are equally big. OTOH it does not work so good if elements have highly differing memory consumption. Examples:
- If the value is a List, it depends on the list size
- If the value is a complex nested object, the memory consumption is hard to know [1]
it would be good if tcache could support limiting the maximum number of entries by entry weight. That weights can be estimated via Collection.size() or an entry-specific weigher.
[1] The whole object graph needs to be traversed, which may not be possible w/o reflection tricks, it can be big, and the object graph can contain circles. Even worse it contain references to shared objects you are not interested in. Worst case are references to a Thread which is a GC root and thus may reference many objects. So in short: It is not wise to try to traverse the whole object graph.
An experimental implementation can be found in the weigher branch https://github.com/trivago/triava/tree/weigher .
Implementation committed to branch https://github.com/trivago/triava/tree/weigher Status: All unit tests are working. TCK tests pending.