RepositoryCache

public final class RepositoryCache<I, E extends Entity<I, ? extends Object>>

The cache of Entity objects for a certain Repository and selected identifiers.

Reduces the number of both read and write storage operations in cases if more than one message is dispatched to the target entity. The typical scenario looks like this:

  1. Several messages are being dispatched to the entity. The framework calls startCaching(entityId) method.
  2. Before dispatching the first message, the repository loads the entity via the cache; the cache executes the read operation, remembers the result and returns the entity.
  3. The first message is dispatched to the entity. store(Entity) method is called. Instead of executing the write operation right away, the cache stores the updated entity in its memory.
  4. More messages are dispatched to the same entity. Each read operation is served by the cache instead of executing the reads from the underlying storage. Upon entity updates, the changed entity is stored into the cache memory.
  5. The dispatching of the message batch is completed. The framework calls stopCaching(entityId) method. Then the cache pushes the updated entity to the underlying storage by executing the write operation.

The users of this class should keep the number of the simultaneously cached entities reasonable due to a potentially huge significant memory footprint.

Parameters

<I>

the type of Entity identifiers

<E>

the type of entity

Constructors

Link copied to clipboard
public void RepositoryCache(boolean multitenant, RepositoryCache.Load<I, E> loadFn, RepositoryCache.Store<E> storeFn)
Creates the instance of the cache considering the multi-tenancy setting, the function to load entities and the function to store the entity .

Types

Link copied to clipboard
public interface Load<I, E extends Entity<I, ? extends Object>> implements Function<T, R>
A function which loads an Entity by ID from its real repository.
Link copied to clipboard
public interface Store<E extends Entity> implements Consumer<T>
A function which stores the Entity to its real repository.

Functions

Link copied to clipboard
public synchronized E load(I id)
Loads the entity by its identifier.
Link copied to clipboard
public synchronized void startCaching(I id)
Starts caching the load and store operation results in memory for the given Entity identifier.
Link copied to clipboard
public synchronized void stopCaching(I id)
Stops caching the load and store operations for the Entity with the passed identifier.
Link copied to clipboard
public synchronized void store(E entity)
Stores the entity.