Skip to content

Commit

Permalink
feat: use ServiceLoader to discover cache providers (#367)
Browse files Browse the repository at this point in the history
Co-authored-by: iProdigy <[email protected]>
  • Loading branch information
DerSimeon and iProdigy authored Jan 1, 2025
1 parent ac805e8 commit 210f7a5
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,5 +35,10 @@ protected void handleExpiration(Duration time, ExpiryType type, BiConsumer<Durat
protected ExpiryType preferredType() {
return ExpiryType.POST_ACCESS; // LRU
}

@ApiStatus.Internal
public int getDiscoveryOrder() {
return 1000;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;

/**
* Holds a registry of default settings and cache providers.
Expand Down Expand Up @@ -103,31 +104,12 @@ public void registerCacheProvider(@NonNull Class<? extends CacheProvider> cacheP
private void populateProviders() {
log.debug("Xanthic: Registering canonical cache providers from the classpath...");

AtomicInteger registered = new AtomicInteger();
Consumer<String> loadImpl = (providerClass) -> {
try {
Class<? extends CacheProvider> 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<AbstractCacheProvider> 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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ protected void entryRemoved(boolean evicted, @NotNull K key, @NotNull V oldValue
};
}

@Override
public int getDiscoveryOrder() {
return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.androidx.AndroidLruProvider
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,8 @@ private static <K, V> Collection<CacheEntryOperationListener<K, V>> buildListene
);
}

@Override
public int getDiscoveryOrder() {
return 4;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.cache2k.Cache2kProvider
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ private static RemovalCause getCause(com.github.benmanes.caffeine.cache.RemovalC
return RemovalCause.OTHER;
}
}

@Override
public int getDiscoveryOrder() {
return 3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.caffeine.CaffeineProvider
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ private static RemovalCause getCause(com.github.benmanes.caffeine.cache.RemovalC
return RemovalCause.OTHER;
}
}

@Override
public int getDiscoveryOrder() {
return 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.caffeine3.Caffeine3Provider
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ private static RemovalCause getCause(EventType type) {
}
}

@Override
public int getDiscoveryOrder() {
return 10;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.ehcache.EhcacheProvider
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public <K, V> Cache<K, V> build(ICacheSpec<K, V> spec) {

return new ExpiringMapDelegate<>(builder.build());
}

@Override
public int getDiscoveryOrder() {
return 8;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.expiringmap.ExpiringMapProvider
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ private static RemovalCause getCause(com.google.common.cache.RemovalCause cause)
return RemovalCause.OTHER;
}
}

@Override
public int getDiscoveryOrder() {
return 9;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.guava.GuavaProvider
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public <K, V> Cache<K, V> build(ICacheSpec<K, V> spec) {

return new InfinispanDelegate<>(cache);
}

@Override
public int getDiscoveryOrder() {
return 6;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.infinispanjdk11.InfinispanProvider
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public <K, V> Cache<K, V> build(ICacheSpec<K, V> spec) {

return new InfinispanDelegate<>(cache);
}

@Override
public int getDiscoveryOrder() {
return 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.infinispanjdk17.InfinispanProvider
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public <K, V> Cache<K, V> build(ICacheSpec<K, V> spec) {

return new InfinispanDelegate<>(cache);
}

@Override
public int getDiscoveryOrder() {
return 7;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.xanthic.cache.provider.infinispan.InfinispanProvider

0 comments on commit 210f7a5

Please sign in to comment.