Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to have several Cache objects in my application and I'd like them to share a maximum weight. So given some total weight X, I'd like the sum of the weights of the cached items across all my caches to not exceed X. I don't know how many caches will be needed up front, nor do the caches need to be 'fair' -- if one is heavily used while others are not, it should be able to take the lion's share of the global maximum. I don't want misses on a cache because its considered full while other caches are mostly un-used and that space could be allocated by the heavily used cache. These will be keeping me from going back to disk to get data, so low miss rates are important.

I thought about having a Weigher implementation that used a static variable that kept the total cached weight. Then a Weigher would return the weight of the object, if that weight is less than the total remaining space, I'd return the weight as-is. If it's more than the total weight left, I could return Long.MAX_VALUE so the item is not cached. Each cache would have a high maximum weight, probably Long.MAX_VALUE-1 so that an element can always be inserted as long as we can calculate/allocate a weight for it.

The problem is that I don't see any way to know the total weight of cached objects. I could decrement the total weight w/ eviction listeners, but there's no way that I can see to be notified when something gets inserted into the cache, nor do the cache stats reflect this in their data.

Is there any workaround for this or approach I could take to achieve this functionality? I have a custom cache implementation that handles global weights, but lacks many of the features provided by the Guava cache, so I'd like to avoid re-implementing stuff if possible. Though I don't mind trying to wire this into the Guava Cache if you can point me in a general direction.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

The Guava caches do not interact with each other at all -- there's no "global" interaction -- but softValues() is the traditional way to make "memory-sensitive" caches, since soft references are collected by the GC (typically in globally least-recently-used order), but only when memory is tight.

I don't even believe Guava caches internally keep track of their total weight -- they track total weight on a per-segment basis, I believe, but keeping track of the total weight would require too much locking.

share|improve this answer

I see only one solution: Can you just combine these into a single cache with a composite key of some kind?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com?utm_source=stackoverflow.com&amp;utm_medium=site-ui&amp;utm_campaign=footerlink\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2014 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a>                   with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2014.11.28.2054\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </no"], "copyright__copyright__copyright": ["otal weight -- they track total weight on a per-segment basis, I believe, but keeping track of the total weight would require too much locking.\n\nshare|improve this answer\n\nI see only one solution: Can you just combine these into a single cache with a composite key of some kind?\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}