-
Notifications
You must be signed in to change notification settings - Fork 0
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 java 11 module (#132)
- Loading branch information
Showing
8 changed files
with
244 additions
and
0 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:14.0.10.Final")) | ||
|
||
compileOnly("org.infinispan:infinispan-component-annotations") | ||
implementation("org.infinispan:infinispan-core") | ||
|
||
testImplementation(testFixtures(project(":cache-core"))) | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
|
||
publishing.publications.withType<MavenPublication> { | ||
pom { | ||
name.set("Xanthic - Infinispan Provider Module for JDK 11") | ||
description.set("Xanthic Provider dependency for Infinispan on JDK 11+") | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...11/src/main/java/io/github/xanthic/cache/provider/infinispanjdk11/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,81 @@ | ||
package io.github.xanthic.cache.provider.infinispanjdk11; | ||
|
||
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.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); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...11/src/main/java/io/github/xanthic/cache/provider/infinispanjdk11/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.infinispanjdk11; | ||
|
||
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
...11/src/main/java/io/github/xanthic/cache/provider/infinispanjdk11/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.infinispanjdk11; | ||
|
||
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.configuration.global.GlobalConfigurationBuilder; | ||
import org.infinispan.manager.DefaultCacheManager; | ||
|
||
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 { | ||
@Override | ||
public <K, V> Cache<K, V> build(ICacheSpec<K, V> spec) { | ||
GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder(); | ||
DefaultCacheManager manager = new DefaultCacheManager(global.build()); | ||
|
||
ConfigurationBuilder builder = new ConfigurationBuilder(); | ||
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) | ||
.getOrCreateCache(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/infinispanjdk11/InfinispanJava11ProviderTest.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.infinispanjdk11; | ||
|
||
import io.github.xanthic.cache.core.provider.ProviderTestBase; | ||
|
||
public class InfinispanJava11ProviderTest extends ProviderTestBase { | ||
|
||
public InfinispanJava11ProviderTest() { | ||
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