Hello,
I have an application based on Spring Boot 4.0.1 and that is using JCache with Redisson for the cache. I have a method with @CacheResult(cacheName = "foo") which can return null. When calling the method the first time with a param that make it return null there is no problem, the method returns null. However, when calling the method a second time, there is problem because I get the following error:
java.lang.ClassCastException: class org.springframework.cache.support.NullValue cannot be cast to class myClass (org.springframework.cache.support.NullValue and myClass are in unnamed module of loader 'app')
Note: I replaced the full class declaration with myClass here.
I gave a look to AbstractValueAdaptingCache which is the one responsible to convert null to NullValue.INSTANCE in the method toStoreValue(...) and is supposed to convert it back in the method fromStoreValue(...). But while debugging I found that in:
protected @Nullable Object fromStoreValue(@Nullable Object storeValue) {
if (this.allowNullValues && storeValue == NullValue.INSTANCE) {
return null;
}
return storeValue;
}
The storeValue I get is not NullValue.INSTANCE but another instance of NullValue (probably because Redisson de-serialized it into another instance), so the test storeValue == NullValue.INSTANCE fails.
I found out that in RedisCache this is worked around (with BINARY_NULL_VALUE) where any instance of NullValue is serialized as BINARY_NULL_VALUE and BINARY_NULL_VALUE is always de-serialized as NullValue.INSTANCE. But no such workaround seems to exists when using JCache.
Can't the test in fromStoreValue(...) be changed to storeValue instanceof NullValue so it wouldn't be affected by serialization/de-serialization? Or should I ask Redisson to implement a similar workaround?
Thank you
Hello,
I have an application based on Spring Boot 4.0.1 and that is using JCache with Redisson for the cache. I have a method with
@CacheResult(cacheName = "foo")which can returnnull. When calling the method the first time with a param that make it returnnullthere is no problem, the method returnsnull. However, when calling the method a second time, there is problem because I get the following error:java.lang.ClassCastException: class org.springframework.cache.support.NullValue cannot be cast to class myClass (org.springframework.cache.support.NullValue and myClass are in unnamed module of loader 'app')Note: I replaced the full class declaration with myClass here.
I gave a look to AbstractValueAdaptingCache which is the one responsible to convert
nulltoNullValue.INSTANCEin the methodtoStoreValue(...)and is supposed to convert it back in the methodfromStoreValue(...). But while debugging I found that in:The storeValue I get is not
NullValue.INSTANCEbut another instance ofNullValue(probably because Redisson de-serialized it into another instance), so the teststoreValue == NullValue.INSTANCEfails.I found out that in
RedisCachethis is worked around (withBINARY_NULL_VALUE) where any instance ofNullValueis serialized asBINARY_NULL_VALUEandBINARY_NULL_VALUEis always de-serialized asNullValue.INSTANCE. But no such workaround seems to exists when using JCache.Can't the test in
fromStoreValue(...)be changed tostoreValue instanceof NullValueso it wouldn't be affected by serialization/de-serialization? Or should I ask Redisson to implement a similar workaround?Thank you