Is there a way to restrict putting object which is greater than a given size.
Asked
Active
Viewed 146 times
0
-
1And how would you determine that size? You cannot reliably determine the size in bytes of a POJO since it is entirely implementation dependent (even if the POJO itself is pretty well defined, CPU alignment constraints are out of the scope of even the JVM spec) – fge Dec 14 '15 at 15:06
-
@fge I thought of using java.lang.instrument.Instrumentation. – Eranda Dec 14 '15 at 15:07
-
What do you want to happen to the entry when its oversized? Do you throw an exception (e.g. from the `Weigher`) or is it loaded but not cached? – Ben Manes Dec 14 '15 at 19:50
1 Answers
3
If you're using a LoadingCache
, easy: just make your CacheLoader
throw an exception if the loaded-in object is too big.
If you're using a non-loading cache, then no. There is no facility to veto cache updates.

C. K. Young
- 219,335
- 46
- 382
- 435
-
I want to use it when I put it into the cache. Implement in load/reload methods seems not helping me there. – Eranda Dec 14 '15 at 15:11
-
3@Eranda Huh? The sole purpose of the load method is "putting things into the cache". If you write your own load method, you can implement any rules that you want. – rmlan Dec 14 '15 at 15:17
-
@rmlan I want to check whether there is OOTB support like setting the size. Seems there is not. Thanks. – Eranda Dec 14 '15 at 15:42