Skip to content

Commit

Permalink
MODEXPW-545 - Improve caching
Browse files Browse the repository at this point in the history
  • Loading branch information
khandramai committed Dec 10, 2024
1 parent c89da5b commit 4305ac8
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 217 deletions.
10 changes: 0 additions & 10 deletions src/main/java/org/folio/dew/config/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.folio.dew.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.folio.dew.domain.dto.ErrorServiceArgs;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@Configuration
Expand All @@ -26,11 +23,4 @@ public CacheManager cacheManager(Caffeine caffeine) { //NOSONAR
caffeineCacheManager.setCaffeine(caffeine);
return caffeineCacheManager;
}

@Bean("defaultKeyGenerator")
public KeyGenerator defaultKeyGenerator() {
return (target, method, params) -> Arrays.stream(params)
.filter(param -> !param.getClass().equals(ErrorServiceArgs.class))
.toList();
}
}
56 changes: 7 additions & 49 deletions src/main/java/org/folio/dew/service/ElectronicAccessService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import static org.apache.commons.lang3.ObjectUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.folio.dew.utils.Constants.ARRAY_DELIMITER;
import static org.folio.dew.utils.Constants.ENTITY_TYPE_TO_ELECTRONIC_ACCESS_DATA_DELIMITER;
import static org.folio.dew.utils.Constants.ENTITY_TYPE_TO_ELECTRONIC_ACCESS_DELIMITER;
import static org.folio.dew.utils.Constants.ITEM_DELIMITER_PATTERN;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.folio.dew.client.ElectronicAccessRelationshipClient;
import org.folio.dew.domain.dto.ElectronicAccess;
import org.folio.dew.domain.dto.EntityType;
Expand All @@ -22,8 +18,6 @@
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand All @@ -36,13 +30,6 @@ public class ElectronicAccessService extends FolioExecutionContextManager {
private final BulkEditProcessingErrorsService errorsService;
private final FolioExecutionContext folioExecutionContext;

private static final int NUMBER_OF_ELECTRONIC_ACCESS_COMPONENTS = 6;
private static final int ELECTRONIC_ACCESS_URI_INDEX = 0;
private static final int ELECTRONIC_ACCESS_LINK_TEXT_INDEX = 1;
private static final int ELECTRONIC_ACCESS_MATERIAL_SPECIFICATION_INDEX = 2;
private static final int ELECTRONIC_ACCESS_PUBLIC_NOTE_INDEX = 3;


public String getElectronicAccessesToString(List<ElectronicAccess> electronicAccesses, ErrorServiceArgs errorServiceArgs, EntityType entityType, String tenantId) {
return isEmpty(electronicAccesses) ?
EMPTY :
Expand All @@ -64,49 +51,20 @@ private String electronicAccessToString(ElectronicAccess access, ErrorServiceArg
}

@Cacheable(cacheNames = "relationships")
public String getRelationshipNameById(String id, String tenantId) {
try (var context = new FolioExecutionContextSetter(refreshAndGetFolioExecutionContext(tenantId, folioExecutionContext))) {
return relationshipClient.getById(id).getName();
}
}

public String getRelationshipNameById(String id, ErrorServiceArgs errorServiceArgs, String tenantId) {
try {
try (var context = new FolioExecutionContextSetter(refreshAndGetFolioExecutionContext(tenantId, folioExecutionContext))) {
return relationshipClient.getById(id).getName();
}
return getRelationshipNameById(id, tenantId);
} catch (NotFoundException e) {
var errorMessage = String.format("Electronic access relationship not found by id=%s", id);
log.error(errorMessage);
errorsService.saveErrorInCSV(errorServiceArgs.getJobId(), errorServiceArgs.getIdentifier(), new BulkEditException(errorMessage), errorServiceArgs.getFileName());
return id;
}
}

@Cacheable(cacheNames = "relationships")
public String getElectronicAccessRelationshipIdByName(String name) {
var relationships = relationshipClient.getByQuery(String.format("name==\"%s\"", name));
if (relationships.getElectronicAccessRelationships().isEmpty()) {
return EMPTY;
}
return relationships.getElectronicAccessRelationships().get(0).getId();
}

public List<ElectronicAccess> restoreElectronicAccess(String s) {
return StringUtils.isEmpty(s) ? Collections.emptyList() :
Arrays.stream(s.split(ITEM_DELIMITER_PATTERN))
.map(this::restoreElectronicAccessItem)
.filter(Objects::nonNull)
.toList();
}

private ElectronicAccess restoreElectronicAccessItem(String s) {
if (isNotEmpty(s)) {
var tokens = s.split(ARRAY_DELIMITER, -1);
if (NUMBER_OF_ELECTRONIC_ACCESS_COMPONENTS == tokens.length) {
return new ElectronicAccess()
.uri(tokens[ELECTRONIC_ACCESS_URI_INDEX])
.linkText(tokens[ELECTRONIC_ACCESS_LINK_TEXT_INDEX])
.materialsSpecification(tokens[ELECTRONIC_ACCESS_MATERIAL_SPECIFICATION_INDEX])
.publicNote(tokens[ELECTRONIC_ACCESS_PUBLIC_NOTE_INDEX])
.relationshipId(tokens[tokens.length - 1]);
}
throw new BulkEditException(String.format("Illegal number of electronic access elements: %d, expected: %d", tokens.length, NUMBER_OF_ELECTRONIC_ACCESS_COMPONENTS));
}
return null;
}
}
86 changes: 68 additions & 18 deletions src/main/java/org/folio/dew/service/HoldingsReferenceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
@RequiredArgsConstructor
@Log4j2
public class HoldingsReferenceService extends FolioExecutionContextManager {
private static final String QUERY_PATTERN_NAME = "name==\"%s\"";
private static final String QUERY_PATTERN_HRID = "hrid==\"%s\"";
private static final String QUERY_PATTERN_BARCODE = "barcode==\"%s\"";

Expand Down Expand Up @@ -111,12 +110,18 @@ public String getHoldingsIdByItemBarcode(String itemBarcode) {
}

@Cacheable(cacheNames = "holdingsTypesNames")
public String getHoldingsTypeNameById(String id, ErrorServiceArgs args, String tenantId) {
if (isEmpty(id)) {
public String getHoldingsTypeNameById(String id, String tenantId) {
if (isEmpty(id)) {
return EMPTY;
}
try (var context = new FolioExecutionContextSetter(refreshAndGetFolioExecutionContext(tenantId, folioExecutionContext))) {
return isEmpty(id) ? EMPTY : holdingsTypeClient.getById(id).getName();
return holdingsTypeClient.getById(id).getName();
}
}

public String getHoldingsTypeNameById(String id, ErrorServiceArgs args, String tenantId) {
try {
return getHoldingsTypeNameById(id, tenantId);
} catch (NotFoundException e) {
var msg = "Holdings type not found by id=" + id;
log.error(msg);
Expand All @@ -137,9 +142,18 @@ public String getLocationNameById(String id, String tenantId) {
}

@Cacheable(cacheNames = "holdingsCallNumberTypesNames")
public String getCallNumberTypeNameById(String id, ErrorServiceArgs args, String tenantId) {
public String getCallNumberTypeNameById(String id, String tenantId) {
if (isEmpty(id)) {
return EMPTY;
}
try (var context = new FolioExecutionContextSetter(refreshAndGetFolioExecutionContext(tenantId, folioExecutionContext))) {
return isEmpty(id) ? EMPTY : callNumberTypeClient.getById(id).getName();
return callNumberTypeClient.getById(id).getName();
}
}

public String getCallNumberTypeNameById(String id, ErrorServiceArgs args, String tenantId) {
try {
return getCallNumberTypeNameById(id, tenantId);
} catch (NotFoundException e) {
var msg = "Call number type not found by id=" + id;
log.error(msg);
Expand All @@ -149,9 +163,18 @@ public String getCallNumberTypeNameById(String id, ErrorServiceArgs args, String
}

@Cacheable(cacheNames = "holdingsNoteTypesNames")
public String getNoteTypeNameById(String id, ErrorServiceArgs args, String tenantId) {
public String getNoteTypeNameById(String id, String tenantId) {
if (isEmpty(id)) {
return EMPTY;
}
try (var context = new FolioExecutionContextSetter(refreshAndGetFolioExecutionContext(tenantId, folioExecutionContext))) {
return isEmpty(id) ? EMPTY : holdingsNoteTypeClient.getById(id).getName();
return holdingsNoteTypeClient.getById(id).getName();
}
}

public String getNoteTypeNameById(String id, ErrorServiceArgs args, String tenantId) {
try {
return getNoteTypeNameById(id, tenantId);
} catch (NotFoundException e) {
var msg = "Note type not found by id=" + id;
log.error(msg);
Expand All @@ -161,21 +184,39 @@ public String getNoteTypeNameById(String id, ErrorServiceArgs args, String tenan
}

@Cacheable(cacheNames = "illPolicyNames")
public String getIllPolicyNameById(String id, ErrorServiceArgs args, String tenantId) {
public String getIllPolicyNameById(String id, String tenantId) {
if (isEmpty(id)) {
return EMPTY;
}
try (var context = new FolioExecutionContextSetter(refreshAndGetFolioExecutionContext(tenantId, folioExecutionContext))) {
return isEmpty(id) ? EMPTY : illPolicyClient.getById(id).getName();
return illPolicyClient.getById(id).getName();
}
}

public String getIllPolicyNameById(String id, ErrorServiceArgs args, String tenantId) {
try {
return getIllPolicyNameById(id, tenantId);
} catch (NotFoundException e) {
var msg = "Ill policy not found by id=" + id;
log.error(msg);
errorsService.saveErrorInCSV(args.getJobId(), args.getIdentifier(), new BulkEditException(msg), args.getFileName());
return id;
var msg = "Ill policy not found by id=" + id;
log.error(msg);
errorsService.saveErrorInCSV(args.getJobId(), args.getIdentifier(), new BulkEditException(msg), args.getFileName());
return id;
}
}

@Cacheable(cacheNames = "holdingsSourceNames")
public String getSourceNameById(String id, ErrorServiceArgs args, String tenantId) {
public String getSourceNameById(String id, String tenantId) {
if (isEmpty(id)) {
return EMPTY;
}
try (var context = new FolioExecutionContextSetter(refreshAndGetFolioExecutionContext(tenantId, folioExecutionContext))) {
return isEmpty(id) ? EMPTY : sourceClient.getById(id).getName();
return sourceClient.getById(id).getName();
}
}

public String getSourceNameById(String id, ErrorServiceArgs args, String tenantId) {
try {
return getSourceNameById(id, tenantId);
} catch (NotFoundException e) {
var msg = "Holdings record source not found by id=" + id;
log.error(msg);
Expand All @@ -185,9 +226,18 @@ public String getSourceNameById(String id, ErrorServiceArgs args, String tenantI
}

@Cacheable(cacheNames = "holdingsStatisticalCodeNames")
public String getStatisticalCodeNameById(String id, ErrorServiceArgs args, String tenantId) {
public String getStatisticalCodeNameById(String id, String tenantId) {
if (isEmpty(id)) {
return EMPTY;
}
try (var context = new FolioExecutionContextSetter(refreshAndGetFolioExecutionContext(tenantId, folioExecutionContext))) {
return isEmpty(id) ? EMPTY : statisticalCodeClient.getById(id).getName();
return statisticalCodeClient.getById(id).getName();
}
}

public String getStatisticalCodeNameById(String id, ErrorServiceArgs args, String tenantId) {
try {
return getStatisticalCodeNameById(id, tenantId);
} catch (NotFoundException e) {
var msg = "Statistical code not found by id=" + id;
log.error(msg);
Expand Down
40 changes: 34 additions & 6 deletions src/main/java/org/folio/dew/service/InstanceReferenceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,69 @@ public class InstanceReferenceService {


@Cacheable(cacheNames = "instanceStatusNames")
public String getInstanceStatusNameById(String instanceStatusId) {
return isEmpty(instanceStatusId) ? EMPTY : instanceStatusesClient.getById(instanceStatusId).getName();
}

public String getInstanceStatusNameById(String instanceStatusId, ErrorServiceArgs args) {
try {
return isEmpty(instanceStatusId) ? EMPTY : instanceStatusesClient.getById(instanceStatusId).getName();
return getInstanceStatusNameById(instanceStatusId);
} catch (NotFoundException e) {
errorsService.saveErrorInCSV(args.getJobId(), args.getIdentifier(), new BulkEditException(String.format("Instance status was not found by id: [%s]", instanceStatusId)), args.getFileName());
return instanceStatusId;
}
}

@Cacheable(cacheNames = "issuanceModeNames")
public String getModeOfIssuanceNameById(String issuanceModeId) {
return isEmpty(issuanceModeId) ? EMPTY : instanceModeOfIssuanceClient.getById(issuanceModeId).getName();
}

public String getModeOfIssuanceNameById(String issuanceModeId, ErrorServiceArgs args) {
try {
return isEmpty(issuanceModeId) ? EMPTY : instanceModeOfIssuanceClient.getById(issuanceModeId).getName();
return getModeOfIssuanceNameById(issuanceModeId);
} catch (NotFoundException e) {
errorsService.saveErrorInCSV(args.getJobId(), args.getIdentifier(), new BulkEditException(String.format("Issuance mode was not found by id: [%s]", issuanceModeId)), args.getFileName());
return issuanceModeId;
}
}

@Cacheable(cacheNames = "instanceTypes")
public String getInstanceTypeNameById(String instanceTypeId) {
return isEmpty(instanceTypeId) ? EMPTY : instanceTypesClient.getById(instanceTypeId).getName();
}

public String getInstanceTypeNameById(String instanceTypeId, ErrorServiceArgs args) {
try {
return isEmpty(instanceTypeId) ? EMPTY : instanceTypesClient.getById(instanceTypeId).getName();
return getInstanceTypeNameById(instanceTypeId);
} catch (NotFoundException e) {
errorsService.saveErrorInCSV(args.getJobId(), args.getIdentifier(), new BulkEditException(String.format("Instance type was not found by id: [%s]", instanceTypeId)), args.getFileName());
return instanceTypeId;
}
}

@Cacheable(cacheNames = "natureOfContentTermIds")
public String getNatureOfContentTermNameById(String natureOfContentTermId) {
return isEmpty(natureOfContentTermId) ? EMPTY : natureOfContentTermsClient.getById(natureOfContentTermId).getName();
}

public String getNatureOfContentTermNameById(String natureOfContentTermId, ErrorServiceArgs args) {
try {
return isEmpty(natureOfContentTermId) ? EMPTY : natureOfContentTermsClient.getById(natureOfContentTermId).getName();
return getNatureOfContentTermNameById(natureOfContentTermId);
} catch (NotFoundException e) {
errorsService.saveErrorInCSV(args.getJobId(), args.getIdentifier(), new BulkEditException(String.format("Nature of content term was not found by id: [%s]", natureOfContentTermId)), args.getFileName());
return natureOfContentTermId;
}
}

@Cacheable(cacheNames = "instanceFormatIds")
public String getFormatOfInstanceNameById(String instanceFormatId) {
return isEmpty(instanceFormatId) ? EMPTY : instanceFormatsClient.getById(instanceFormatId).getName();
}

public String getFormatOfInstanceNameById(String instanceFormatId, ErrorServiceArgs args) {
try {
return isEmpty(instanceFormatId) ? EMPTY : instanceFormatsClient.getById(instanceFormatId).getName();
return getFormatOfInstanceNameById(instanceFormatId);
} catch (NotFoundException e) {
errorsService.saveErrorInCSV(args.getJobId(), args.getIdentifier(), new BulkEditException(String.format("Instance format was not found by id: [%s]", instanceFormatId)), args.getFileName());
return instanceFormatId;
Expand All @@ -98,9 +122,13 @@ public String getTypeOfIdentifiersIdByName(String identifierName) {
}

@Cacheable(cacheNames = "instanceNoteTypes")
public String getInstanceNoteTypeNameById(String noteTypeId) {
return isEmpty(noteTypeId) ? EMPTY : instanceNoteTypesClient.getNoteTypeById(noteTypeId).getName();
}

public String getInstanceNoteTypeNameById(String noteTypeId, ErrorServiceArgs args) {
try {
return isEmpty(noteTypeId) ? EMPTY : instanceNoteTypesClient.getNoteTypeById(noteTypeId).getName();
return getInstanceNoteTypeNameById(noteTypeId);
} catch (NotFoundException e) {
errorsService.saveErrorInCSV(args.getJobId(), args.getIdentifier(), new BulkEditException(String.format("Instance note type was not found by id: [%s]", noteTypeId)), args.getFileName());
return noteTypeId;
Expand Down
Loading

0 comments on commit 4305ac8

Please sign in to comment.