Skip to content

Commit

Permalink
Merge pull request #668 from scireum/ili/SIRI-1039
Browse files Browse the repository at this point in the history
Improves the BaseEntityCache and BaseEntityRef
  • Loading branch information
idlira authored Dec 16, 2024
2 parents 1be0695 + dc8f9b9 commit fa6b319
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected Object getValueFromField(Object target) {

@Override
public String getValueForUserMessage(Object entity) {
return NLS.toUserString(getEntityRef(accessPath.apply(entity)).fetchValue());
return NLS.toUserString(getEntityRef(accessPath.apply(entity)).fetchCachedValue());
}

@Override
Expand Down
72 changes: 60 additions & 12 deletions src/main/java/sirius/db/mixing/types/BaseEntityRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,32 +167,80 @@ public String getUniqueObjectName() {
* Note, this might cause a database lookup if the entity is not prefetched.
*
* @return the entity being referenced or <tt>null</tt> if no entity is referenced.
* @deprecated use {@link #fetchCachedValue()} or {@link #forceFetchValue()} instead
*/
@Deprecated
public E fetchValue() {
if (value == null && id != null) {
Optional<E> entity = find(type, id);
if (entity.isPresent()) {
value = entity.get();
} else {
id = null;
}
return fetchCachedValue();
}

/**
* Returns the effective entity object which is referenced.
* <p>
* Note, this might cause a database lookup if the entity is not prefetched.
*
* @return the entity being referenced or <tt>null</tt> if no entity is referenced.
*/
public E fetchCachedValue() {
if (value != null) {
return value;
}
return forceFetchValue();
}

/**
* Returns the effective entity object which is referenced by forcing a fresh database lookup.
*
* @return the entity being referenced or <tt>null</tt> if no entity is referenced.
*/
public E forceFetchValue() {
Optional<E> entity = find(type, id);
if (entity.isPresent()) {
value = entity.get();
} else {
id = null;
value = null;
}
return value;
}

/**
* Returns the effective entity object which is referenced using a secondary node of a DB cluster.
* <p>
* If a value has already been loaded via {@link #forceFetchValue()} or indirectly via {@link #fetchCachedValue()},
* it will be delivered. Otherwise, a lookup is performed using secondary nodes, but the value retrieved
* id will not be cached and subsequent calls will always perform a fresh lookup.
*
* @return the entity being referenced or <tt>null</tt> if no entity is referenced.
*/
public E fetchCachedValueFromSecondary() {
if (value != null) {
return value;
}
return forceFetchValueFromSecondary();
}

/**
* Returns the effective entity object which is referenced by forcing a fresh database lookup using a secondary node
* of a DB cluster.
*
* @return the entity being referenced or <tt>null</tt> if no entity is referenced.
*/
public E forceFetchValueFromSecondary() {
return findInSecondary(type, id).orElse(null);
}

/**
* Returns the effective entity object which is referenced using a secondary node of a DB cluster.
* <p>
* Note, this values returned by this method will never be cached and a new lookup is always performed.
*
* @return the entity being referenced or <tt>null</tt> if no entity is referenced.
* @deprecated use {@link #fetchCachedValueFromSecondary()} or {@link #forceFetchValueFromSecondary()} instead
*/
@Deprecated
public E fetchValueFromSecondary() {
if (value == null && id != null) {
Optional<E> entity = findInSecondary(type, id);
return entity.orElse(null);
}
return value;
return fetchCachedValueFromSecondary();
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/sirius/db/util/BaseEntityCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,13 @@ public E fetchRequiredById(@Nonnull String id) {
}

/**
* Fetches the entity in the given ref from the cache and returns it.
* Fetches the entity with {@link BaseEntity#ID id} stored in the given ref from the cache and returns it.
*
* @param entityRef the ref pointing to the entity to fetch
* @return the cached entity wrapped in an Optional or an empty Optional
*/
@Nonnull
public Optional<E> fetch(@Nonnull BaseEntityRef<I, E> entityRef) {
if (entityRef.isValueLoaded()) {
return entityRef.getValueIfPresent();
}
return fetchById(entityRef.getIdAsString());
}

Expand Down
Loading

0 comments on commit fa6b319

Please sign in to comment.