Skip to content

fix(core): allow zero max size caches across providers #173

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

Merged
merged 2 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions core/src/main/java/io/github/xanthic/cache/core/CacheApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import io.github.xanthic.cache.api.ICacheSpec;
import io.github.xanthic.cache.api.exception.MisconfiguredCacheException;
import io.github.xanthic.cache.api.exception.NoDefaultCacheImplementationException;
import io.github.xanthic.cache.core.delegate.EmptyCache;

import java.time.Duration;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -32,7 +34,14 @@ private CacheApi() {
*/
public static <K, V> Cache<K, V> create(Consumer<CacheApiSpec<K, V>> spec) {
CacheApiSpec<K, V> finalSpec = CacheApiSpec.process(spec);
if (isPermanentlyEmpty(finalSpec)) return EmptyCache.get();
return finalSpec.provider().build(finalSpec);
}

private static boolean isPermanentlyEmpty(ICacheSpec<?, ?> spec) {
Long maxSize = spec.maxSize();
Duration expiryTime = spec.expiryTime();
return (maxSize != null && maxSize == 0) || (expiryTime != null && expiryTime.isZero());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package io.github.xanthic.cache.core.delegate;

import io.github.xanthic.cache.api.Cache;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

public final class EmptyCache<K, V> implements Cache<K, V> {

@SuppressWarnings("rawtypes")
private static final Cache INSTANCE = new EmptyCache<>();

@ApiStatus.Internal
@SuppressWarnings("unchecked")
public static <K, V> Cache<K, V> get() {
return INSTANCE;
}

private EmptyCache() {
// restrict instantiation
}

@Override
public @Nullable V get(@NotNull K key) {
return null;
}

@Override
public @Nullable V put(@NotNull K key, @NotNull V value) {
return null;
}

@Override
public @Nullable V remove(@NotNull K key) {
return null;
}

@Override
public void clear() {
// no-op
}

@Override
public long size() {
return 0L;
}

@Override
public @Nullable V compute(@NotNull K key, @NotNull BiFunction<? super K, ? super V, ? extends V> computeFunc) {
return computeFunc.apply(key, null);
}

@Override
public V computeIfAbsent(@NotNull K key, @NotNull Function<K, V> computeFunc) {
return computeFunc.apply(key);
}

@Override
public @Nullable V computeIfPresent(@NotNull K key, @NotNull BiFunction<? super K, ? super V, ? extends V> computeFunc) {
return null;
}

@Override
public @Nullable V putIfAbsent(@NotNull K key, @NotNull V value) {
return null;
}

@Override
public V merge(@NotNull K key, @NotNull V value, @NotNull BiFunction<V, V, V> mergeFunc) {
return value;
}

@Override
public boolean replace(@NotNull K key, @NotNull V value) {
return false;
}

@Override
public boolean replace(@NotNull K key, @NotNull V oldValue, @NotNull V newValue) {
return false;
}

@Override
public @NotNull V getOrDefault(@NotNull K key, @NotNull V defaultValue) {
return defaultValue;
}

@Override
public void putAll(@NotNull Map<? extends K, ? extends V> map) {
// no-op
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ public void replaceTest() {
Assertions.assertNull(cache.get("9"));
}

@Test
@DisplayName("Test that caches with zero maximum size remain empty")
public void zeroMaxSizeTest() {
Cache<String, Integer> cache = build(spec -> spec.maxSize(0L));
cache.put("1", 1);
Assertions.assertNull(cache.get("1"));
Assertions.assertEquals(0, cache.size());
}

@Test
@DisplayName("Test that caches with zero time-to-live for entries remain empty")
public void zeroExpiryTimeTest() {
Cache<String, Integer> cache = build(spec -> spec.expiryTime(Duration.ZERO));
cache.put("1", 1);
Assertions.assertNull(cache.get("1"));
Assertions.assertEquals(0, cache.size());
}

@Test
@DisplayName("Test that cache size constraint is respected")
public void sizeEvictionTest() {
Expand Down