Hi all
CaffeineCache implementation override method public ValueWrapper get(Object key) and there is a specific check when a LoadingCache is provided.
In this case, the CacheLoader of the LoadingCache is used to retrieve the data (if not already present int the cache).
I don't see the same behavior for the method public <T> T get(Object key, @Nullable Class<T> type).
In this case, the CacheLoader of the LoadingCache is never used. Using only this method... the cache will be always empty.
Is there any reason for this different behavior?
Thanks Stefano
Comment From: snicoll
Yes. The cache abstraction is meant to be used as follows:
@Cacheable
public MyObject computeSomethingExpensive(String key) { ... }
Using LoadingCache is incompatible with this pattern which explains the behaviour of get. If you have more questions, please follow-up on StackOverflow, as mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements.
Comment From: stefano-salmaso
Hi @snicoll , I agree with you that the use of a loading cache with @cacheble is incompatible (and I would add "strange").
Sorry if I haven't used Stackoverflow ... but I would take advantage of this already created issue to ask you why the loading cache is used in the public ValueWrapper get (Object key) method to retrieve the value.
If mean... if LoadingCache is incompatible, it shouldn't be used in public ValueWrapper get (Object key) also.
Comment From: snicoll
I overlooked your report, sorry about that. I find it strange that we have a case for supporting the loading cache there so let's reopen to at least check.
You haven't really shared why you're reporting this. What made you create this issue in the first place?
Comment From: stefano-salmaso
I have an application where we are using the Spring Cache abstraction without the usage of @Cacheable. In my specific case, having a LoadingCache is useful, but I discovered that it doesn't work with the method public <T> T get(Object key, @Nullable Class<T> type).
Checking the source code of CaffeineCache I found this different behavior.
Again, I agree with you that @Cacheable and LoadingCache are incompatible but I think we should be coherent in the CaffeineCache.
(In this case, removing the usage of the Loadingcache could be the right choice)
Comment From: snicoll
Thanks for the feedback and that clarifies the issue quite a bit. If we can consistently call the LoadingCache, I'd rather do that as removing the usage is a backward incompatible change and I am not keen to do that.