I've recently been working on upgrading an application from Ehcache 2.10.6 to 3.10.0, and also upgrading from Java 15 to 17. During testing, I ran into a problem where trying to put values into one of our caches would
always fail. Importantly, the cache was using a record as the key, and was sized using
EntryUnit. Here's the code for that cache, if you're curious:
https://github.com/nysenate/OpenLegislation/blob/3.9/src/main/java/gov/nysenate/openleg/legislation/member/dao/SessionMemberNonIdCache.javaIt took me awhile to figure out the core cause, this line in sun.misc.Unsafe throwing an error:
if (declaringClass.isRecord()) {
throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
}
https://github.com/openjdk/jdk/blob/master/src/jdk.unsupported/share/classes/sun/misc/Unsafe.javaSo, it seems that records just can't be used as part of the key or value of caches, if it's sized by entries. I haven't seen this mentioned anywhere. Are there plans to fix this? Records are a natural thing to use as keys, after all. Or at the very least, could an error be thrown if you try to use a record in a cache with entry sizing? Tracking down the issue was quite difficult.
----
Jacob Keegan
Junior Programmer
New York State Senate