diff --git a/core/src/main/java/net/staticstudios/data/CachedValue.java b/core/src/main/java/net/staticstudios/data/CachedValue.java index 224d478..0aabb7c 100644 --- a/core/src/main/java/net/staticstudios/data/CachedValue.java +++ b/core/src/main/java/net/staticstudios/data/CachedValue.java @@ -32,12 +32,15 @@ default CachedValue withFallback(T fallback) { CachedValue supplyFallback(Supplier fallback); + CachedValue refresh(Supplier refresh); + class ProxyCachedValue implements CachedValue { protected final UniqueData holder; protected final Class dataType; private final List> updateHandlers = new ArrayList<>(); private @Nullable CachedValue delegate; private Supplier fallback = () -> null; + private @Nullable Supplier refresh = null; public ProxyCachedValue(UniqueData holder, Class dataType) { this.holder = holder; @@ -48,6 +51,7 @@ public void setDelegate(CachedValueMetadata metadata, AbstractCachedValue del Preconditions.checkNotNull(delegate, "Delegate cannot be null"); Preconditions.checkState(this.delegate == null, "Delegate is already set"); delegate.setFallback(this.fallback); + delegate.setRefresh(this.refresh); this.delegate = delegate; //since an update handler can be registered before the fallback is set, we need to convert them here @@ -88,6 +92,16 @@ public CachedValue supplyFallback(Supplier fallback) { return this; } + @Override + public CachedValue refresh(Supplier refresh) { + if (delegate != null) { + throw new UnsupportedOperationException("Cannot set fallback after initialization"); + } + Preconditions.checkNotNull(refresh, "Refresh supplier cannot be null"); + LambdaUtils.assertLambdaDoesntCapture(refresh, List.of(UniqueData.class), null); + return delegate.refresh(refresh); + } + @Override public T get() { if (delegate != null) { diff --git a/core/src/main/java/net/staticstudios/data/impl/data/AbstractCachedValue.java b/core/src/main/java/net/staticstudios/data/impl/data/AbstractCachedValue.java index 1a95d68..a57897b 100644 --- a/core/src/main/java/net/staticstudios/data/impl/data/AbstractCachedValue.java +++ b/core/src/main/java/net/staticstudios/data/impl/data/AbstractCachedValue.java @@ -1,11 +1,13 @@ package net.staticstudios.data.impl.data; +import com.google.common.base.Preconditions; import net.staticstudios.data.CachedValue; import java.util.function.Supplier; public abstract class AbstractCachedValue implements CachedValue { private Supplier fallbackSupplier; + private Supplier refreshSupplier; public void setFallback(Supplier fallbackSupplier) { this.fallbackSupplier = fallbackSupplier; @@ -17,4 +19,17 @@ protected T getFallback() { } return null; } + + public void setRefresh(Supplier refreshSupplier) { + this.refreshSupplier = refreshSupplier; + } + + protected T refresh() { + if (refreshSupplier == null) { + return null; + } + T value = refreshSupplier.get(); + set(value); + return value; + } } diff --git a/core/src/main/java/net/staticstudios/data/impl/data/CachedValueImpl.java b/core/src/main/java/net/staticstudios/data/impl/data/CachedValueImpl.java index 7d09804..1b0de8b 100644 --- a/core/src/main/java/net/staticstudios/data/impl/data/CachedValueImpl.java +++ b/core/src/main/java/net/staticstudios/data/impl/data/CachedValueImpl.java @@ -100,12 +100,18 @@ public CachedValue supplyFallback(Supplier fallback) { throw new UnsupportedOperationException("Cannot set fallback after initialization"); } + @Override + public CachedValue refresh(Supplier refresh) { + throw new UnsupportedOperationException("Cannot set refresh after initialization"); + } + @Override public T get() { Preconditions.checkArgument(!holder.isDeleted(), "Cannot get value from a deleted UniqueData instance"); T value = holder.getDataManager().getRedis(metadata.holderSchema(), metadata.holderTable(), metadata.identifier(), holder.getIdColumns(), dataType); if (value == null) { - return getFallback(); + value = refresh(); + value = value == null ? getFallback() : value; } return value; } diff --git a/core/src/main/java/net/staticstudios/data/impl/data/ReadOnlyCachedValue.java b/core/src/main/java/net/staticstudios/data/impl/data/ReadOnlyCachedValue.java index 0a27919..bfa3b80 100644 --- a/core/src/main/java/net/staticstudios/data/impl/data/ReadOnlyCachedValue.java +++ b/core/src/main/java/net/staticstudios/data/impl/data/ReadOnlyCachedValue.java @@ -68,6 +68,11 @@ public CachedValue supplyFallback(Supplier fallback) { throw new UnsupportedOperationException("Cannot set fallback on a read-only CachedValue"); } + @Override + public CachedValue refresh(Supplier refresh) { + throw new UnsupportedOperationException("Cannot refresh a read-only CachedValue"); + } + @Override public T get() { return value;