From 210f7a524bed1b900c3c9ba01889aef40bc2a626 Mon Sep 17 00:00:00 2001 From: "Simeon L." <78266888+DerSimeon@users.noreply.github.com> Date: Wed, 1 Jan 2025 23:59:07 +0100 Subject: [PATCH] feat: use ServiceLoader to discover cache providers (#367) Co-authored-by: iProdigy --- .../cache/core/AbstractCacheProvider.java | 6 ++++ .../xanthic/cache/core/CacheApiSettings.java | 36 +++++-------------- .../provider/androidx/AndroidLruProvider.java | 4 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + .../provider/cache2k/Cache2kProvider.java | 4 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + .../provider/caffeine/CaffeineProvider.java | 5 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + .../provider/caffeine3/Caffeine3Provider.java | 5 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + .../provider/ehcache/EhcacheProvider.java | 4 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + .../expiringmap/ExpiringMapProvider.java | 5 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + .../cache/provider/guava/GuavaProvider.java | 5 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + .../infinispanjdk11/InfinispanProvider.java | 5 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + .../infinispanjdk17/InfinispanProvider.java | 5 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + .../infinispan/InfinispanProvider.java | 5 +++ ...b.xanthic.cache.core.AbstractCacheProvider | 1 + 22 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 provider-androidx/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider create mode 100644 provider-cache2k/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider create mode 100644 provider-caffeine/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider create mode 100644 provider-caffeine3/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider create mode 100644 provider-ehcache/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider create mode 100644 provider-expiringmap/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider create mode 100644 provider-guava/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider create mode 100644 provider-infinispan-java11/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider create mode 100644 provider-infinispan-java17/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider create mode 100644 provider-infinispan/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider diff --git a/core/src/main/java/io/github/xanthic/cache/core/AbstractCacheProvider.java b/core/src/main/java/io/github/xanthic/cache/core/AbstractCacheProvider.java index 76f0a352..261fcca7 100644 --- a/core/src/main/java/io/github/xanthic/cache/core/AbstractCacheProvider.java +++ b/core/src/main/java/io/github/xanthic/cache/core/AbstractCacheProvider.java @@ -4,6 +4,7 @@ import io.github.xanthic.cache.api.domain.ExpiryType; import io.github.xanthic.cache.api.domain.MisconfigurationPolicy; import io.github.xanthic.cache.api.exception.MisconfiguredCacheException; +import org.jetbrains.annotations.ApiStatus; import java.time.Duration; import java.util.function.BiConsumer; @@ -34,5 +35,10 @@ protected void handleExpiration(Duration time, ExpiryType type, BiConsumer cacheP private void populateProviders() { log.debug("Xanthic: Registering canonical cache providers from the classpath..."); - AtomicInteger registered = new AtomicInteger(); - Consumer loadImpl = (providerClass) -> { - try { - Class clazz = Class.forName(providerClass).asSubclass(CacheProvider.class); - registerCacheProvider(clazz, null); // lazy, init if needed - registered.incrementAndGet(); - } catch (ClassNotFoundException cx) { - log.trace("Xanthic: Could not find optional cache provider " + providerClass); - } catch (Exception e) { - log.trace("Xanthic: Could not find optional cache provider " + providerClass, e); - } - }; - - loadImpl.accept("io.github.xanthic.cache.provider.androidx.AndroidLruProvider"); - loadImpl.accept("io.github.xanthic.cache.provider.caffeine3.Caffeine3Provider"); - loadImpl.accept("io.github.xanthic.cache.provider.caffeine.CaffeineProvider"); - loadImpl.accept("io.github.xanthic.cache.provider.cache2k.Cache2kProvider"); - loadImpl.accept("io.github.xanthic.cache.provider.infinispanjdk17.InfinispanProvider"); - loadImpl.accept("io.github.xanthic.cache.provider.infinispanjdk11.InfinispanProvider"); - loadImpl.accept("io.github.xanthic.cache.provider.infinispan.InfinispanProvider"); - loadImpl.accept("io.github.xanthic.cache.provider.expiringmap.ExpiringMapProvider"); - loadImpl.accept("io.github.xanthic.cache.provider.guava.GuavaProvider"); - loadImpl.accept("io.github.xanthic.cache.provider.ehcache.EhcacheProvider"); - - log.debug("Xanthic: Loaded {} canonical cache provider(s) on settings construction!", registered.get()); + ServiceLoader load = ServiceLoader.load(AbstractCacheProvider.class); + StreamSupport.stream(load.spliterator(), false) + .sorted(Comparator.comparingInt(AbstractCacheProvider::getDiscoveryOrder)) + .forEach(provider -> registerCacheProvider(provider.getClass(), provider)); + + log.debug("Xanthic: Loaded {} canonical cache provider(s) on settings construction!", providers.size()); } /** diff --git a/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/AndroidLruProvider.java b/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/AndroidLruProvider.java index fff87f2a..3befb0a9 100644 --- a/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/AndroidLruProvider.java +++ b/provider-androidx/src/main/java/io/github/xanthic/cache/provider/androidx/AndroidLruProvider.java @@ -55,4 +55,8 @@ protected void entryRemoved(boolean evicted, @NotNull K key, @NotNull V oldValue }; } + @Override + public int getDiscoveryOrder() { + return 1; + } } diff --git a/provider-androidx/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-androidx/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..4897624a --- /dev/null +++ b/provider-androidx/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.androidx.AndroidLruProvider diff --git a/provider-cache2k/src/main/java/io/github/xanthic/cache/provider/cache2k/Cache2kProvider.java b/provider-cache2k/src/main/java/io/github/xanthic/cache/provider/cache2k/Cache2kProvider.java index f0726eb4..cf06961b 100644 --- a/provider-cache2k/src/main/java/io/github/xanthic/cache/provider/cache2k/Cache2kProvider.java +++ b/provider-cache2k/src/main/java/io/github/xanthic/cache/provider/cache2k/Cache2kProvider.java @@ -108,4 +108,8 @@ private static Collection> buildListene ); } + @Override + public int getDiscoveryOrder() { + return 4; + } } diff --git a/provider-cache2k/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-cache2k/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..1a5ecebc --- /dev/null +++ b/provider-cache2k/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.cache2k.Cache2kProvider diff --git a/provider-caffeine/src/main/java/io/github/xanthic/cache/provider/caffeine/CaffeineProvider.java b/provider-caffeine/src/main/java/io/github/xanthic/cache/provider/caffeine/CaffeineProvider.java index 8a890df7..7fdb21e5 100644 --- a/provider-caffeine/src/main/java/io/github/xanthic/cache/provider/caffeine/CaffeineProvider.java +++ b/provider-caffeine/src/main/java/io/github/xanthic/cache/provider/caffeine/CaffeineProvider.java @@ -51,4 +51,9 @@ private static RemovalCause getCause(com.github.benmanes.caffeine.cache.RemovalC return RemovalCause.OTHER; } } + + @Override + public int getDiscoveryOrder() { + return 3; + } } diff --git a/provider-caffeine/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-caffeine/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..bd4ab12a --- /dev/null +++ b/provider-caffeine/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.caffeine.CaffeineProvider diff --git a/provider-caffeine3/src/main/java/io/github/xanthic/cache/provider/caffeine3/Caffeine3Provider.java b/provider-caffeine3/src/main/java/io/github/xanthic/cache/provider/caffeine3/Caffeine3Provider.java index 82c1b4c8..3ce7d960 100644 --- a/provider-caffeine3/src/main/java/io/github/xanthic/cache/provider/caffeine3/Caffeine3Provider.java +++ b/provider-caffeine3/src/main/java/io/github/xanthic/cache/provider/caffeine3/Caffeine3Provider.java @@ -49,4 +49,9 @@ private static RemovalCause getCause(com.github.benmanes.caffeine.cache.RemovalC return RemovalCause.OTHER; } } + + @Override + public int getDiscoveryOrder() { + return 2; + } } diff --git a/provider-caffeine3/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-caffeine3/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..12be24c2 --- /dev/null +++ b/provider-caffeine3/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.caffeine3.Caffeine3Provider diff --git a/provider-ehcache/src/main/java/io/github/xanthic/cache/provider/ehcache/EhcacheProvider.java b/provider-ehcache/src/main/java/io/github/xanthic/cache/provider/ehcache/EhcacheProvider.java index 6cdf3ed0..a836628e 100644 --- a/provider-ehcache/src/main/java/io/github/xanthic/cache/provider/ehcache/EhcacheProvider.java +++ b/provider-ehcache/src/main/java/io/github/xanthic/cache/provider/ehcache/EhcacheProvider.java @@ -101,4 +101,8 @@ private static RemovalCause getCause(EventType type) { } } + @Override + public int getDiscoveryOrder() { + return 10; + } } diff --git a/provider-ehcache/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-ehcache/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..d2737dd4 --- /dev/null +++ b/provider-ehcache/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.ehcache.EhcacheProvider diff --git a/provider-expiringmap/src/main/java/io/github/xanthic/cache/provider/expiringmap/ExpiringMapProvider.java b/provider-expiringmap/src/main/java/io/github/xanthic/cache/provider/expiringmap/ExpiringMapProvider.java index be656f01..cfd6ad8e 100644 --- a/provider-expiringmap/src/main/java/io/github/xanthic/cache/provider/expiringmap/ExpiringMapProvider.java +++ b/provider-expiringmap/src/main/java/io/github/xanthic/cache/provider/expiringmap/ExpiringMapProvider.java @@ -36,4 +36,9 @@ public Cache build(ICacheSpec spec) { return new ExpiringMapDelegate<>(builder.build()); } + + @Override + public int getDiscoveryOrder() { + return 8; + } } diff --git a/provider-expiringmap/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-expiringmap/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..64146b24 --- /dev/null +++ b/provider-expiringmap/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.expiringmap.ExpiringMapProvider diff --git a/provider-guava/src/main/java/io/github/xanthic/cache/provider/guava/GuavaProvider.java b/provider-guava/src/main/java/io/github/xanthic/cache/provider/guava/GuavaProvider.java index cdbbec9f..4f1d209e 100644 --- a/provider-guava/src/main/java/io/github/xanthic/cache/provider/guava/GuavaProvider.java +++ b/provider-guava/src/main/java/io/github/xanthic/cache/provider/guava/GuavaProvider.java @@ -72,4 +72,9 @@ private static RemovalCause getCause(com.google.common.cache.RemovalCause cause) return RemovalCause.OTHER; } } + + @Override + public int getDiscoveryOrder() { + return 9; + } } diff --git a/provider-guava/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-guava/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..ea344459 --- /dev/null +++ b/provider-guava/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.guava.GuavaProvider diff --git a/provider-infinispan-java11/src/main/java/io/github/xanthic/cache/provider/infinispanjdk11/InfinispanProvider.java b/provider-infinispan-java11/src/main/java/io/github/xanthic/cache/provider/infinispanjdk11/InfinispanProvider.java index 0b0eb1f6..d1d383a3 100644 --- a/provider-infinispan-java11/src/main/java/io/github/xanthic/cache/provider/infinispanjdk11/InfinispanProvider.java +++ b/provider-infinispan-java11/src/main/java/io/github/xanthic/cache/provider/infinispanjdk11/InfinispanProvider.java @@ -47,4 +47,9 @@ public Cache build(ICacheSpec spec) { return new InfinispanDelegate<>(cache); } + + @Override + public int getDiscoveryOrder() { + return 6; + } } diff --git a/provider-infinispan-java11/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-infinispan-java11/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..c3c8c52d --- /dev/null +++ b/provider-infinispan-java11/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.infinispanjdk11.InfinispanProvider diff --git a/provider-infinispan-java17/src/main/java/io/github/xanthic/cache/provider/infinispanjdk17/InfinispanProvider.java b/provider-infinispan-java17/src/main/java/io/github/xanthic/cache/provider/infinispanjdk17/InfinispanProvider.java index 1cd587cc..c3d43b5c 100644 --- a/provider-infinispan-java17/src/main/java/io/github/xanthic/cache/provider/infinispanjdk17/InfinispanProvider.java +++ b/provider-infinispan-java17/src/main/java/io/github/xanthic/cache/provider/infinispanjdk17/InfinispanProvider.java @@ -47,4 +47,9 @@ public Cache build(ICacheSpec spec) { return new InfinispanDelegate<>(cache); } + + @Override + public int getDiscoveryOrder() { + return 5; + } } diff --git a/provider-infinispan-java17/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-infinispan-java17/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..1b1ea9f4 --- /dev/null +++ b/provider-infinispan-java17/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.infinispanjdk17.InfinispanProvider diff --git a/provider-infinispan/src/main/java/io/github/xanthic/cache/provider/infinispan/InfinispanProvider.java b/provider-infinispan/src/main/java/io/github/xanthic/cache/provider/infinispan/InfinispanProvider.java index 5bc82bb9..72f6fab7 100644 --- a/provider-infinispan/src/main/java/io/github/xanthic/cache/provider/infinispan/InfinispanProvider.java +++ b/provider-infinispan/src/main/java/io/github/xanthic/cache/provider/infinispan/InfinispanProvider.java @@ -47,4 +47,9 @@ public Cache build(ICacheSpec spec) { return new InfinispanDelegate<>(cache); } + + @Override + public int getDiscoveryOrder() { + return 7; + } } diff --git a/provider-infinispan/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider b/provider-infinispan/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider new file mode 100644 index 00000000..e2a0d749 --- /dev/null +++ b/provider-infinispan/src/main/resources/META-INF/services/io.github.xanthic.cache.core.AbstractCacheProvider @@ -0,0 +1 @@ +io.github.xanthic.cache.provider.infinispan.InfinispanProvider