-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extracts Caching Libraries into Service Loaders (Caffeine -> Java, Guava -> Android) #3977
Conversation
…ng exception to match the LoadingCache.
Codecov Report
@@ Coverage Diff @@
## master #3977 +/- ##
============================================
- Coverage 81.86% 80.24% -1.63%
- Complexity 20838 22463 +1625
============================================
Files 1421 1438 +17
Lines 77246 83118 +5872
Branches 11078 11615 +537
============================================
+ Hits 63236 66694 +3458
- Misses 9866 11384 +1518
- Partials 4144 5040 +896 Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Just want to make sure this wasn't forgotten in the discussion. If the cache-api project fit your needs, it might be a good way for you and others to collaborate. It may not and I am not familiar with it or the developers, but an fyi in case that avoids duplication of efforts. |
Thank you, Ben! I didn't know the project existed. Maybe we can collaborate with them in the future. At a first glance, it does a similar job. However, the cache-api does not standardize the interface among all implementations and it doesn't offer a provider for the
These are needed to completely match Caffeine's API. The CacheDelegator ends up looking like this: try {
return cache.get(key, () -> mappingFunction.apply(key));
} catch (ExecutionException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (UncheckedExecutionException e) {
if (e.getCause() instanceof RuntimeException) {
// Unwrap exception to match Caffeine
throw (RuntimeException)e.getCause();
}
throw e;
} catch (CacheLoader.InvalidCacheLoadException e) {
// If the entry is not found or load as null, returns null instead of an exception
// This matches the behaviour of Caffeine
return null;
} |
It might be unsatisfactory, but would using Caffeine's Guava Cache adapters fit your goals? That attempts to make Caffeine conform to Guava, with similar workarounds to your snippet. Just trying to make sure you have all the options to consider, and whatever the answer will be the best choice for your project. |
I did see those components (thank you for making them) but I discarded that approach because it adds significant complexity to the exception handling on the HAPI side. We could go there, but it wouldn't be a simple 3hr PR by then. HAPI was clearly coded with Caffeine's API in mind. And I do believe that, after looking at both, Caffeine's API is superior for users. |
@vitorpamplona - I want to get this merged in time for the 6.2.0 release. I've pulled these changes into a fresh PR that I'll be submitting to our internal review process tomorrow: #4196 |
Nice! Yes, let's go! :) |
@vitorpamplona - My copy of your PR has now merged. Thanks again for the contribution! A couple of notes:
|
Thank you, James! |
Hi @jamesagnew
Is there any chance HAPI can avoid using caffeine as default? Today every Android developer hits a roadblock when trying to use HAPI because they need to figure out that they must manually remove caffeine and add guava in their build files. The current dependency stack doesn't provide a great dev experience for Android. :( |
@vitorpamplona I thought we had? What is pulling it in? |
The Validation Module :( hapi-fhir/hapi-fhir-validation/pom.xml Line 37 in 2e4f8fe
|
@vitorpamplona pushing through a fix in #5472 |
Thank you, James! |
Background
Caffeine 3.0+ does not plan to support Android. The 3.0 version requires a
java.lang.System.getLogger
method from the JEP-264: Platform Logging API to redirect logs toslf4j
. Unfortunately, Android has not provided aSystem.getLogger
interface yet. This means a HAPI migration to Caffeine 3.0+ will simply crash in runtime on Android.Solution
Abstract the Caching function, remove direct dependencies on Caffeine, and build an Android-friendly alternative.
This PR establishes a new Caching structure via ServiceLoaders in 4 new maven modules:
hapi-fhir-cache
: Cache service loader interfaces and factory methods.hapi-fhir-cache-guava
: Delegator classes to Guava (ideal for Android)hapi-fhir-cache-caffeine
: Delegator classes to Caffeine (current implementation)hapi-fhir-cache-testing
: Utility module to quickly alternate betweenguava
andcaffeine
and run the project's test cases on.All modules dependent on
caffeine
now depend onhapi-fhir-cache
directly. Users must choose a caching implementation by also importinghapi-fhir-cache-caffeine
orhapi-fhir-cache-guava
. If none is present, the following error message will be displayed:No Cache Service Providers found. Choose between hapi-fhir-cache-caffeine (Default) and hapi-fhir-cache-guava (Android)
hapi-fhir-cache-guava
contains brand new Cache and LoadingCache implementations that mimic Caffeine's behavior completely and it's ideal for the use of HAPI on Android. In my tests, it fully replaces Caffeine for the needs of this project.Closes: #2444, Helps: #3690