All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.xanthic.cache.provider.expiringmap.ExpiringMapProvider Maven / Gradle / Ivy

package io.github.xanthic.cache.provider.expiringmap;

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.api.domain.RemovalCause;
import io.github.xanthic.cache.core.AbstractCacheProvider;
import io.github.xanthic.cache.core.delegate.GenericMapCacheDelegate;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

import java.util.concurrent.TimeUnit;

/**
 * Provides {@link Cache} instances using {@link ExpiringMap}.
 * 

* Implements size and time-based expiry. *

* Note: listeners will always receive {@link RemovalCause#OTHER} due to limitations of the backing library. *

* Consider using Caffeine or Cache2k for better performance. */ public final class ExpiringMapProvider extends AbstractCacheProvider { @Override public Cache build(ICacheSpec spec) { ExpiringMap.Builder builder = ExpiringMap.builder(); if (spec.maxSize() != null) builder.maxSize(spec.maxSize().intValue()); if (spec.removalListener() != null) builder.expirationListener((key, value) -> spec.removalListener().onRemoval(key, value, RemovalCause.OTHER)); handleExpiration(spec.expiryTime(), spec.expiryType(), (time, type) -> { builder.expiration(time.toNanos(), TimeUnit.NANOSECONDS); if (type == ExpiryType.POST_WRITE) builder.expirationPolicy(ExpirationPolicy.CREATED); else builder.expirationPolicy(ExpirationPolicy.ACCESSED); }); return new ExpiringMapDelegate<>(builder.build()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy