public abstract class RaftBackedCache<V>
extends java.lang.Object
implements java.lang.Iterable<java.util.Map.Entry<java.lang.String,V>>, java.lang.AutoCloseable
| Modifier and Type | Class and Description |
|---|---|
static interface |
RaftBackedCache.ChangeListener<V>
This holds a locally registered callback that will be called whenever the cache changes.
|
| Modifier and Type | Field and Description |
|---|---|
Theseus |
raft
The implementing Raft for the cache.
|
| Modifier | Constructor and Description |
|---|---|
protected |
RaftBackedCache(Theseus raft,
java.time.Duration idleDuration,
java.time.Duration elementLifetime)
Create a Raft backed cache with the default Raft implementation.
|
protected |
RaftBackedCache(Theseus raft,
java.time.Duration idleDuration,
java.time.Duration elementLifetime,
int maxSizeBytes)
Create a Raft backed cache with a particular Raft implementation.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addChangeListener(RaftBackedCache.ChangeListener<V> changeListener) |
void |
addChangeListener(RaftBackedCache.ChangeListener<V> changeListener,
java.lang.Object owner)
This registers a listener that will be called whenever the key-value store changes.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
allOutstandingEvictionsFuture()
This creates a new CompletableFuture for all the currently outstanding eviction tasks.
|
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
clearCache()
This iterates through all the elements in the Raft backed cache and evicts them, using only a single Raft commit
to do it.
|
void |
close()
This cleans up the eviction timer.
|
abstract java.util.Optional<V> |
deserialize(byte[] serialized)
Read an object in our cache, written with
serialize(Object), into a value type. |
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
evictWithoutSaving(java.lang.String key)
Remove an element in Raft.
|
java.util.Optional<V> |
get(java.lang.String key)
Get an element from the cache.
|
java.util.Optional<V> |
getIfPresent(java.lang.String key)
Get an element from the cache, if it's present in the cache itself.
|
java.util.Iterator<java.util.Map.Entry<java.lang.String,V>> |
iterator() |
protected void |
onSet(V object)
Run every time an object is set in the cache.
|
abstract void |
persist(java.lang.String key,
V value,
boolean async)
Evict an element from our cache.
|
protected abstract java.lang.String |
prefix()
The raft prefix to use on
get(String) and put(String, Object, boolean) to translate from our
local namespace to Raft's global namespace |
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
put(java.lang.String key,
V value,
boolean persist)
Set an element in Raft.
|
void |
removeChangeListener(RaftBackedCache.ChangeListener changeListener)
This registers a listener that will be called whenever the key-value store changes.
|
abstract java.util.Optional<V> |
restore(java.lang.String key)
Get a value from wherever it was evicted to, if we can.
|
abstract byte[] |
serialize(V object)
Serialize an object of our value type into a byte array.
|
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
withElementAsync(java.lang.String key,
java.util.function.BiFunction<V,java.util.function.Consumer<V>,V> mutator) |
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
withElementAsync(java.lang.String key,
java.util.function.BiFunction<V,java.util.function.Consumer<V>,V> mutator,
java.util.function.Supplier<V> createNew,
boolean unlocked)
Perform a computation on the given element.
|
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
withElementAsync(java.lang.String key,
java.util.function.Function<V,V> mutator) |
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
withElementAsync(java.lang.String key,
java.util.function.Function<V,V> mutator,
java.util.function.Supplier<V> createNew) |
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
withElementAsync(java.lang.String key,
java.util.function.Function<V,V> mutator,
java.util.function.Supplier<V> createNew,
boolean unlocked) |
public final Theseus raft
protected RaftBackedCache(Theseus raft, java.time.Duration idleDuration, java.time.Duration elementLifetime, int maxSizeBytes)
protected RaftBackedCache(Theseus raft, java.time.Duration idleDuration, java.time.Duration elementLifetime)
protected void onSet(V object)
object - The object we're setting in the cache.public java.util.concurrent.CompletableFuture<java.lang.Void> allOutstandingEvictionsFuture()
public void close()
close in interface java.lang.AutoCloseableprotected abstract java.lang.String prefix()
get(String) and put(String, Object, boolean) to translate from our
local namespace to Raft's global namespacepublic abstract byte[] serialize(V object)
deserialize(byte[]).object - The object we are serializing.deserialize(byte[])public abstract java.util.Optional<V> deserialize(byte[] serialized)
serialize(Object), into a value type.serialized - The serialized blob we are reading.serialize(Object)public abstract java.util.Optional<V> restore(java.lang.String key)
persist(String, Object, boolean).key - The key of the element we're creating / retrieving.persist(String, Object, boolean)public abstract void persist(java.lang.String key,
V value,
boolean async)
restore(String) first try to retrieve the
element from this persistence store.key - The key we are evicting.value - The value we are evicting.async - If true, the save is allowed to be asynchronous.
This is often the case where, e.g., we're saving on a creation when we
should not be blocking the main thread.public java.util.concurrent.CompletableFuture<java.lang.Boolean> clearCache()
public void addChangeListener(RaftBackedCache.ChangeListener<V> changeListener, @Nullable java.lang.Object owner)
changeListener - the listener to registerowner - the owner of the change listener. This is a failsafe to make
sure we remove the listener when the owner dies, but is not required.public void addChangeListener(RaftBackedCache.ChangeListener<V> changeListener)
public void removeChangeListener(RaftBackedCache.ChangeListener changeListener)
changeListener - the listener to registerpublic java.util.concurrent.CompletableFuture<java.lang.Boolean> withElementAsync(java.lang.String key,
java.util.function.BiFunction<V,java.util.function.Consumer<V>,V> mutator,
@Nullable
java.util.function.Supplier<V> createNew,
boolean unlocked)
restore(String) to ensure that it
is created.key - The key of the element we're computing on.mutator - The function to call on the value for the given key.
This takes 2 arguments: the input item, and a function that can be called to sync intermediate
states. It should return the final state to save.
Note that saving intermediate states is done while this object still holds the lock.
So, other processes can only access the result read-only,createNew - An optional method for creating a brand new object of the relevant type - only gets called if
element cannot be retrieved from storageunlocked - If true, we are not locking the underlying Raft call.public java.util.concurrent.CompletableFuture<java.lang.Boolean> withElementAsync(java.lang.String key,
java.util.function.Function<V,V> mutator,
@Nullable
java.util.function.Supplier<V> createNew,
boolean unlocked)
public java.util.concurrent.CompletableFuture<java.lang.Boolean> withElementAsync(java.lang.String key,
java.util.function.Function<V,V> mutator,
java.util.function.Supplier<V> createNew)
public java.util.concurrent.CompletableFuture<java.lang.Boolean> withElementAsync(java.lang.String key,
java.util.function.Function<V,V> mutator)
public java.util.concurrent.CompletableFuture<java.lang.Boolean> withElementAsync(java.lang.String key,
java.util.function.BiFunction<V,java.util.function.Consumer<V>,V> mutator)
public java.util.Optional<V> get(java.lang.String key)
key - The key of the element to getpublic java.util.Optional<V> getIfPresent(java.lang.String key)
key - The key of the element to getpublic java.util.concurrent.CompletableFuture<java.lang.Boolean> put(java.lang.String key,
V value,
boolean persist)
Theseus.setElementAsync(String, byte[], boolean, Duration)
with permanent set to true and a 5 second timeout.public java.util.concurrent.CompletableFuture<java.lang.Boolean> evictWithoutSaving(java.lang.String key)
Theseus.removeElementAsync(String, Duration).