-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add infinispan jdk17 module (#245)
- Loading branch information
Showing
8 changed files
with
261 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
dependencies { | ||
api(project(":cache-core")) | ||
|
||
implementation(platform("org.infinispan:infinispan-bom:15.0.0.Final")) | ||
|
||
compileOnly("org.infinispan:infinispan-component-annotations") | ||
implementation("org.infinispan:infinispan-core") | ||
|
||
testImplementation(testFixtures(project(":cache-core"))) | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
|
||
publishing.publications.withType<MavenPublication> { | ||
pom { | ||
name.set("Xanthic - Infinispan Provider Module for JDK 17") | ||
description.set("Xanthic Provider dependency for Infinispan on JDK 17+") | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...17/src/main/java/io/github/xanthic/cache/provider/infinispanjdk17/InfinispanDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.github.xanthic.cache.provider.infinispanjdk17; | ||
|
||
import io.github.xanthic.cache.api.Cache; | ||
import lombok.Value; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
@Value | ||
class InfinispanDelegate<K, V> implements Cache<K, V> { | ||
org.infinispan.Cache<K, V> cache; | ||
|
||
@Override | ||
public V get(@NotNull K key) { | ||
return cache.get(key); | ||
} | ||
|
||
@Override | ||
public V put(@NotNull K key, @NotNull V value) { | ||
return cache.put(key, value); | ||
} | ||
|
||
@Override | ||
public V remove(@NotNull K key) { | ||
return cache.remove(key); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
cache.clear(); | ||
} | ||
|
||
@Override | ||
public long size() { | ||
return cache.size(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public V compute(@NotNull K key, @NotNull BiFunction<? super K, ? super V, ? extends V> computeFunc) { | ||
return cache.compute(key, computeFunc); | ||
} | ||
|
||
@Override | ||
public V computeIfAbsent(@NotNull K key, @NotNull Function<K, V> computeFunc) { | ||
return cache.computeIfAbsent(key, computeFunc); | ||
} | ||
|
||
@Override | ||
public V computeIfPresent(@NotNull K key, @NotNull BiFunction<? super K, ? super V, ? extends V> computeFunc) { | ||
return cache.computeIfPresent(key, computeFunc); | ||
} | ||
|
||
@Override | ||
public V putIfAbsent(@NotNull K key, @NotNull V value) { | ||
return cache.putIfAbsent(key, value); | ||
} | ||
|
||
@Override | ||
public V merge(@NotNull K key, @NotNull V value, @NotNull BiFunction<V, V, V> mergeFunc) { | ||
return cache.merge(key, value, mergeFunc); | ||
} | ||
|
||
@Override | ||
public boolean replace(@NotNull K key, @NotNull V value) { | ||
return cache.replace(key, value) != null; | ||
} | ||
|
||
@Override | ||
public boolean replace(@NotNull K key, @NotNull V oldValue, @NotNull V newValue) { | ||
return cache.replace(key, oldValue, newValue); | ||
} | ||
|
||
@Override | ||
public void putAll(@NotNull Map<? extends K, ? extends V> map) { | ||
cache.putAll(map); | ||
} | ||
|
||
@Override | ||
public void forEach(@NotNull BiConsumer<? super K, ? super V> action) { | ||
cache.forEach(action); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...17/src/main/java/io/github/xanthic/cache/provider/infinispanjdk17/InfinispanListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.github.xanthic.cache.provider.infinispanjdk17; | ||
|
||
import io.github.xanthic.cache.api.RemovalListener; | ||
import io.github.xanthic.cache.api.domain.RemovalCause; | ||
import lombok.Value; | ||
import org.infinispan.notifications.Listener; | ||
import org.infinispan.notifications.cachelistener.annotation.CacheEntriesEvicted; | ||
import org.infinispan.notifications.cachelistener.annotation.CacheEntryExpired; | ||
import org.infinispan.notifications.cachelistener.annotation.CacheEntryInvalidated; | ||
import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified; | ||
import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved; | ||
import org.infinispan.notifications.cachelistener.event.CacheEntriesEvictedEvent; | ||
import org.infinispan.notifications.cachelistener.event.CacheEntryExpiredEvent; | ||
import org.infinispan.notifications.cachelistener.event.CacheEntryInvalidatedEvent; | ||
import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent; | ||
import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent; | ||
import org.infinispan.notifications.cachelistener.event.Event; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Collections; | ||
import java.util.EnumSet; | ||
import java.util.IdentityHashMap; | ||
import java.util.Set; | ||
|
||
@Value | ||
@Listener | ||
class InfinispanListener<K, V> { | ||
static final Set<Event.Type> EVENTS; | ||
static final Set<Class<? extends Annotation>> ANNOTATIONS; | ||
|
||
RemovalListener<K, V> removalListener; | ||
|
||
@CacheEntriesEvicted | ||
public void onPostEvictions(CacheEntriesEvictedEvent<K, V> event) { | ||
if (!event.isPre()) | ||
event.getEntries().forEach((k, v) -> removalListener.onRemoval(k, v, RemovalCause.SIZE)); | ||
} | ||
|
||
@CacheEntryExpired | ||
public void onExpiry(CacheEntryExpiredEvent<K, V> event) { | ||
removalListener.onRemoval(event.getKey(), event.getValue(), RemovalCause.TIME); | ||
} | ||
|
||
@CacheEntryInvalidated | ||
public void onInvalidation(CacheEntryInvalidatedEvent<K, V> event) { | ||
removalListener.onRemoval(event.getKey(), event.getValue(), RemovalCause.OTHER); | ||
} | ||
|
||
@CacheEntryModified | ||
public void onPostModifyExisting(CacheEntryModifiedEvent<K, V> event) { | ||
if (!event.isCreated() && !event.isPre()) | ||
removalListener.onRemoval(event.getKey(), event.getOldValue(), RemovalCause.REPLACED); | ||
} | ||
|
||
@CacheEntryRemoved | ||
public void onPostRemoval(CacheEntryRemovedEvent<K, V> event) { | ||
if (!event.isPre()) | ||
removalListener.onRemoval(event.getKey(), event.getOldValue(), RemovalCause.MANUAL); | ||
} | ||
|
||
static { | ||
EVENTS = EnumSet.noneOf(Event.Type.class); | ||
EVENTS.add(Event.Type.CACHE_ENTRY_EVICTED); | ||
EVENTS.add(Event.Type.CACHE_ENTRY_EXPIRED); | ||
EVENTS.add(Event.Type.CACHE_ENTRY_INVALIDATED); | ||
EVENTS.add(Event.Type.CACHE_ENTRY_MODIFIED); | ||
EVENTS.add(Event.Type.CACHE_ENTRY_REMOVED); | ||
|
||
ANNOTATIONS = Collections.newSetFromMap(new IdentityHashMap<>()); | ||
ANNOTATIONS.add(CacheEntriesEvicted.class); | ||
ANNOTATIONS.add(CacheEntryExpired.class); | ||
ANNOTATIONS.add(CacheEntryInvalidated.class); | ||
ANNOTATIONS.add(CacheEntryModified.class); | ||
ANNOTATIONS.add(CacheEntryRemoved.class); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...17/src/main/java/io/github/xanthic/cache/provider/infinispanjdk17/InfinispanProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.github.xanthic.cache.provider.infinispanjdk17; | ||
|
||
import io.github.xanthic.cache.api.Cache; | ||
import io.github.xanthic.cache.api.ICacheSpec; | ||
import io.github.xanthic.cache.api.domain.ExpiryType; | ||
import io.github.xanthic.cache.core.AbstractCacheProvider; | ||
import org.infinispan.commons.api.CacheContainerAdmin; | ||
import org.infinispan.configuration.cache.ConfigurationBuilder; | ||
import org.infinispan.manager.DefaultCacheManager; | ||
import org.infinispan.manager.EmbeddedCacheManager; | ||
|
||
import java.util.UUID; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Provides {@link Cache} instances using Infinispan's {@link org.infinispan.Cache} in heap-mode. | ||
* <p> | ||
* Implements size and time-based expiry. | ||
*/ | ||
public final class InfinispanProvider extends AbstractCacheProvider { | ||
private static final EmbeddedCacheManager MANAGER = new DefaultCacheManager(); | ||
|
||
@Override | ||
public <K, V> Cache<K, V> build(ICacheSpec<K, V> spec) { | ||
ConfigurationBuilder builder = new ConfigurationBuilder(); | ||
builder.simpleCache(true); | ||
if (spec.maxSize() != null) builder.memory().maxCount(spec.maxSize()); | ||
handleExpiration(spec.expiryTime(), spec.expiryType(), (time, type) -> { | ||
if (type == ExpiryType.POST_WRITE) | ||
builder.expiration().lifespan(time.toNanos(), TimeUnit.NANOSECONDS); | ||
else | ||
builder.expiration().maxIdle(time.toNanos(), TimeUnit.NANOSECONDS); | ||
}); | ||
|
||
org.infinispan.Cache<K, V> cache = MANAGER.administration() | ||
.withFlags(CacheContainerAdmin.AdminFlag.VOLATILE) | ||
.createCache(UUID.randomUUID().toString(), builder.build()); | ||
|
||
if (spec.removalListener() != null) { | ||
cache.addFilteredListener( | ||
new InfinispanListener<>(spec.removalListener()), | ||
(key, oldValue, oldMeta, newValue, newMeta, eventType) -> eventType != null && InfinispanListener.EVENTS.contains(eventType.getType()), | ||
null, | ||
InfinispanListener.ANNOTATIONS | ||
); | ||
} | ||
|
||
return new InfinispanDelegate<>(cache); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...t/java/io/github/xanthic/cache/provider/infinispanjdk17/InfinispanJava17ProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.github.xanthic.cache.provider.infinispanjdk17; | ||
|
||
import io.github.xanthic.cache.core.provider.ProviderTestBase; | ||
|
||
public class InfinispanJava17ProviderTest extends ProviderTestBase { | ||
|
||
public InfinispanJava17ProviderTest() { | ||
super(new InfinispanProvider()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters