Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Caffeine's Expiry.creating for StorageCredentialCache #1142

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import com.github.benmanes.caffeine.cache.Expiry;
import com.github.benmanes.caffeine.cache.LoadingCache;
import jakarta.annotation.Nonnull;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.apache.iceberg.exceptions.UnprocessableEntityException;
import org.apache.polaris.core.PolarisCallContext;
Expand All @@ -51,39 +51,16 @@ public StorageCredentialCache() {
Caffeine.newBuilder()
.maximumSize(CACHE_MAX_NUMBER_OF_ENTRIES)
.expireAfter(
new Expiry<StorageCredentialCacheKey, StorageCredentialCacheEntry>() {
@Override
public long expireAfterCreate(
StorageCredentialCacheKey key,
StorageCredentialCacheEntry entry,
long currentTime) {
long expireAfterMillis =
Math.max(
0,
Math.min(
(entry.getExpirationTime() - System.currentTimeMillis()) / 2,
maxCacheDurationMs()));
return TimeUnit.MILLISECONDS.toNanos(expireAfterMillis);
}

@Override
public long expireAfterUpdate(
StorageCredentialCacheKey key,
StorageCredentialCacheEntry entry,
long currentTime,
long currentDuration) {
return currentDuration;
}

@Override
public long expireAfterRead(
StorageCredentialCacheKey key,
StorageCredentialCacheEntry entry,
long currentTime,
long currentDuration) {
return currentDuration;
}
})
Expiry.creating(
(StorageCredentialCacheKey key, StorageCredentialCacheEntry entry) -> {
long expireAfterMillis =
Math.max(
0,
Math.min(
(entry.getExpirationTime() - System.currentTimeMillis()) / 2,
maxCacheDurationMs()));
return Duration.ofMillis(expireAfterMillis);
}))
.build(
key -> {
// the load happen at getOrGenerateSubScopeCreds()
Expand Down