Skip to content

Commit

Permalink
Send the summary to the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Apr 19, 2024
1 parent 585b896 commit 48f6749
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package org.orcid.persistence.aop;
package org.orcid.core.aop;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.orcid.core.utils.cache.redis.RedisClient;
import org.orcid.persistence.dao.ProfileLastModifiedDao;
import org.orcid.persistence.jpa.entities.IndexingStatus;
import org.orcid.persistence.jpa.entities.OrcidAware;
import org.orcid.persistence.util.OrcidStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.PriorityOrdered;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
Expand Down Expand Up @@ -40,6 +44,12 @@ public class ProfileLastModifiedAspect implements PriorityOrdered {

private static final String UPDATE_PROFILE_LAST_MODIFIED_AND_INDEXING_STATUS = "@annotation(org.orcid.persistence.aop.UpdateProfileLastModifiedAndIndexingStatus)";

@Resource
private RedisClient redisClient;

@Value("${org.orcid.core.utils.cache.redis.summary.enabled:false}")
private boolean isSummaryCacheEnabled;

public boolean isEnabled() {
return enabled;
}
Expand Down Expand Up @@ -125,7 +135,10 @@ public void updateLastModifiedDateAndIndexingStatus(String orcid) {

ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (sra != null)
sra.setAttribute(sraKey(orcid), null, ServletRequestAttributes.SCOPE_REQUEST);
sra.setAttribute(sraKey(orcid), null, ServletRequestAttributes.SCOPE_REQUEST);

// Clear redis caches
evictCaches(orcid);
}

/** Updates the last modified date and clears the request-scope last modified cache.
Expand All @@ -144,7 +157,10 @@ public void updateLastModifiedDate(String orcid) {

ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (sra != null)
sra.setAttribute(sraKey(orcid), null, ServletRequestAttributes.SCOPE_REQUEST);
sra.setAttribute(sraKey(orcid), null, ServletRequestAttributes.SCOPE_REQUEST);

// Clear redis caches
evictCaches(orcid);
}

/** Fetches the last modified from the request-scope last modified cache
Expand All @@ -169,4 +185,9 @@ public Date retrieveLastModifiedDate(String orcid) {
private String sraKey(String orcid) {
return REQUEST_PROFILE_LAST_MODIFIED + '_' + name + '_' + orcid;
}

public void evictCaches(String orcid) {
// Evict the summary cache
redisClient.remove(orcid + "-summary");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.orcid.core.manager.v3.read_only.RecordManagerReadOnly;
import org.orcid.core.manager.v3.read_only.RecordNameManagerReadOnly;
import org.orcid.core.manager.v3.read_only.WorkManagerReadOnly;
import org.orcid.core.utils.JsonUtils;
import org.orcid.core.utils.cache.redis.RedisClient;
import org.orcid.jaxb.model.v3.release.common.FuzzyDate;
import org.orcid.jaxb.model.v3.release.common.Source;
import org.orcid.jaxb.model.v3.release.common.Visibility;
Expand All @@ -43,6 +45,7 @@
import org.orcid.pojo.summary.ExternalIdentifiersSummary;
import org.orcid.pojo.summary.RecordSummary;
import org.orcid.utils.DateUtils;
import org.springframework.beans.factory.annotation.Value;

public class SummaryManagerImpl implements SummaryManager {

Expand Down Expand Up @@ -72,9 +75,28 @@ public class SummaryManagerImpl implements SummaryManager {

@Resource
private WorksCacheManager worksCacheManager;

@Resource
private RedisClient redisClient;

@Value("${org.orcid.core.utils.cache.redis.summary.enabled:false}")
private boolean isSummaryCacheEnabled;

// Set the cache TTL for the summary, 1 day by default
@Value("${org.orcid.core.utils.cache.redis.summary.ttl:86400}")
private int summaryCacheTTL;

@Override
public RecordSummary getRecordSummary(String orcid) {
String cacheKey = getCacheKey(orcid);
// Check the cache
if(isSummaryCacheEnabled) {
String summaryString = redisClient.get(cacheKey);
if(StringUtils.isNotBlank(summaryString)) {
return JsonUtils.readObjectFromJsonString(summaryString, RecordSummary.class);
}
}

RecordSummary recordSummary = new RecordSummary();

// Set ORCID uri
Expand Down Expand Up @@ -102,13 +124,16 @@ public RecordSummary getRecordSummary(String orcid) {
// Generate the peer review summary
generatePeerReviewSummary(recordSummary, orcid);
recordSummary.setStatus("active");

// Set the summary in the cache
if(isSummaryCacheEnabled) {
redisClient.set(cacheKey, JsonUtils.convertToJsonString(recordSummary), summaryCacheTTL);
}
return recordSummary;
}

public void generateWorksSummary(RecordSummary recordSummary, String orcid) {
Works works = worksCacheManager.getGroupedWorks(orcid);
// TODO Remove non public elements
// TODO There should be a manager that does this, but, the one we have already returns a list of work summaries, so, we need to refactor it to return the same Works element
Iterator<WorkGroup> workGroupIt = works.getWorkGroup().iterator();
while (workGroupIt.hasNext()) {
WorkGroup workGroup = workGroupIt.next();
Expand Down Expand Up @@ -274,4 +299,8 @@ private void sortAffiliationsByEndDate(List<AffiliationSummary> affiliations) {
// Remove the end on the affiliations
summariesWithOutEndDate.forEach(s -> s.setEndDate(null));
}

private String getCacheKey(String orcid) {
return orcid + "-summary";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private String getCacheKey(String sourceId) {
}

private String getClientSourceName(String clientId) {
LOGGER.debug("Fetching client name from DB: " + clientId);
if (clientDetailsDao.existsAndIsNotPublicClient(clientId)) {
ClientDetailsEntity clientDetails = clientDetailsDao.find(clientId);
return clientDetails != null ? clientDetails.getClientName() : null;
Expand All @@ -110,6 +111,7 @@ private String getProfileSourceNameFromRequest(String orcid) {
}

private String getProfileSourceNameFromDb(String orcid) {
LOGGER.debug("Fetching user name from DB: " + orcid);
try {
if (!recordNameDao.exists(orcid)) {
throw new IllegalArgumentException("Unable to find source name for: " + orcid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Date;

import org.orcid.persistence.aop.ProfileLastModifiedAspect;
import org.orcid.core.aop.ProfileLastModifiedAspect;

public interface ManagerReadOnlyBase {
void setProfileLastModifiedAspect(ProfileLastModifiedAspect profileLastModifiedAspect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.Date;

import org.orcid.core.aop.ProfileLastModifiedAspect;
import org.orcid.core.manager.read_only.ManagerReadOnlyBase;
import org.orcid.persistence.aop.ProfileLastModifiedAspect;

public class ManagerReadOnlyBaseImpl implements ManagerReadOnlyBase {
protected ProfileLastModifiedAspect profileLastModifiedAspect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Date;

import org.orcid.persistence.aop.ProfileLastModifiedAspect;
import org.orcid.core.aop.ProfileLastModifiedAspect;

public interface ManagerReadOnlyBase {
void setProfileLastModifiedAspect(ProfileLastModifiedAspect profileLastModifiedAspect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.Date;

import org.orcid.core.aop.ProfileLastModifiedAspect;
import org.orcid.core.manager.v3.read_only.ManagerReadOnlyBase;
import org.orcid.persistence.aop.ProfileLastModifiedAspect;

public class ManagerReadOnlyBaseImpl implements ManagerReadOnlyBase {
protected ProfileLastModifiedAspect profileLastModifiedAspect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Iterables;
import org.apache.commons.lang3.StringUtils;
import org.ehcache.Cache;
import org.orcid.core.aop.ProfileLastModifiedAspect;
import org.orcid.core.contributors.roles.credit.CreditRole;
import org.orcid.core.manager.ActivityManager;
import org.orcid.core.manager.ProfileEntityManager;
Expand All @@ -14,7 +15,6 @@
import org.orcid.jaxb.model.record_v2.FundingContributor;
import org.orcid.jaxb.model.record_v2.Work;
import org.orcid.jaxb.model.record_v2.WorkBulk;
import org.orcid.persistence.aop.ProfileLastModifiedAspect;
import org.orcid.persistence.dao.RecordNameDao;
import org.orcid.persistence.jpa.entities.RecordNameEntity;
import org.orcid.pojo.ContributorsRolesAndSequencesV2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class RedisClient {
private final int cacheExpiryInSecs;
private final int clientTimeoutInMillis;
private JedisPool pool;
private SetParams setParams;
private SetParams defaultSetParams;

@Resource
private SlackManager slackManager;
Expand Down Expand Up @@ -70,7 +70,7 @@ private void init() {
JedisClientConfig config = DefaultJedisClientConfig.builder().connectionTimeoutMillis(this.clientTimeoutInMillis).timeoutMillis(this.clientTimeoutInMillis)
.socketTimeoutMillis(this.clientTimeoutInMillis).password(this.redisPassword).ssl(true).build();
pool = new JedisPool(new HostAndPort(this.redisHost, this.redisPort), config);
setParams = new SetParams().ex(this.cacheExpiryInSecs);
defaultSetParams = new SetParams().ex(this.cacheExpiryInSecs);
// Pool test
try(Jedis jedis = pool.getResource()) {
if(jedis.isConnected()) {
Expand Down Expand Up @@ -99,16 +99,25 @@ private void init() {
}

public boolean set(String key, String value) {
return set(key, value, defaultSetParams);
}

public boolean set(String key, String value, int cacheExpiryInSecs) {
SetParams params = new SetParams().ex(cacheExpiryInSecs);
return set(key, value, params);
}

private boolean set(String key, String value, SetParams params) {
if(enabled && pool != null) {
try (Jedis jedis = pool.getResource()) {
LOG.debug("Setting Key: {}", key);
String result = jedis.set(key, value, setParams);
String result = jedis.set(key, value, params);
return "OK".equalsIgnoreCase(result);
}
}
return false;
}

public String get(String key) {
if(enabled && pool != null) {
try (Jedis jedis = pool.getResource()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.apache.commons.lang3.StringUtils;
import org.ehcache.Cache;
import org.orcid.core.aop.ProfileLastModifiedAspect;
import org.orcid.core.contributors.roles.credit.CreditRole;
import org.orcid.core.manager.ClientDetailsEntityCacheManager;
import org.orcid.core.manager.SourceNameCacheManager;
Expand All @@ -25,7 +26,6 @@
import org.orcid.jaxb.model.v3.release.record.FundingContributor;
import org.orcid.jaxb.model.v3.release.record.Work;
import org.orcid.jaxb.model.v3.release.record.WorkBulk;
import org.orcid.persistence.aop.ProfileLastModifiedAspect;
import org.orcid.persistence.dao.RecordNameDao;
import org.orcid.persistence.dao.WorkDao;
import org.orcid.persistence.jpa.entities.ClientDetailsEntity;
Expand Down
12 changes: 12 additions & 0 deletions orcid-core/src/main/resources/orcid-core-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1221,4 +1221,16 @@

<bean id="summaryManager" class="org.orcid.core.common.manager.impl.SummaryManagerImpl" />

<!-- Profile last modified aspect -->
<bean id="profileLastModifiedAspect" class="org.orcid.core.aop.ProfileLastModifiedAspect">
<property name="profileLastModifiedDao" ref="profileLastModifiedDao" />
<property name="enabled" value="true" />
</bean>

<bean id="profileLastModifiedAspectReadOnly" class="org.orcid.core.aop.ProfileLastModifiedAspect">
<property name="profileLastModifiedDao" ref="profileLastModifiedDaoReadOnly" />
<property name="enabled" value="false" />
<property name="name" value="read_only" />
</bean>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.orcid.core.aop.ProfileLastModifiedAspect;
import org.orcid.core.manager.ActivityManager;
import org.orcid.core.manager.ProfileEntityCacheManager;
import org.orcid.core.manager.ProfileEntityManager;
Expand All @@ -36,7 +37,6 @@
import org.orcid.jaxb.model.record_v2.WorkBulk;
import org.orcid.jaxb.model.record_v2.WorkContributors;
import org.orcid.jaxb.model.record_v2.WorkTitle;
import org.orcid.persistence.aop.ProfileLastModifiedAspect;
import org.orcid.persistence.dao.RecordNameDao;
import org.orcid.persistence.jpa.entities.ProfileEntity;
import org.orcid.persistence.jpa.entities.RecordNameEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.orcid.core.aop.ProfileLastModifiedAspect;
import org.orcid.core.manager.ProfileEntityCacheManager;
import org.orcid.core.manager.v3.ActivityManager;
import org.orcid.core.manager.v3.ProfileEntityManager;
Expand All @@ -36,7 +37,6 @@
import org.orcid.jaxb.model.v3.release.record.WorkBulk;
import org.orcid.jaxb.model.v3.release.record.WorkContributors;
import org.orcid.jaxb.model.v3.release.record.WorkTitle;
import org.orcid.persistence.aop.ProfileLastModifiedAspect;
import org.orcid.persistence.dao.RecordNameDao;
import org.orcid.persistence.jpa.entities.ProfileEntity;
import org.orcid.persistence.jpa.entities.RecordNameEntity;
Expand Down
12 changes: 0 additions & 12 deletions orcid-persistence/src/main/resources/orcid-persistence-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@
<bean id="applicationContextProvder"
class="org.orcid.persistence.spring.ApplicationContextProvider"/>

<!-- Profile last modified aspect -->
<bean id="profileLastModifiedAspect" class="org.orcid.persistence.aop.ProfileLastModifiedAspect">
<property name="profileLastModifiedDao" ref="profileLastModifiedDao" />
<property name="enabled" value="true" />
</bean>

<bean id="profileLastModifiedAspectReadOnly" class="org.orcid.persistence.aop.ProfileLastModifiedAspect">
<property name="profileLastModifiedDao" ref="profileLastModifiedDaoReadOnly" />
<property name="enabled" value="false" />
<property name="name" value="read_only" />
</bean>

<!-- Person DAO's -->
<bean id="recordNameDao" class="org.orcid.persistence.dao.impl.RecordNameDaoImpl" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.orcid.core.aop.ProfileLastModifiedAspect;
import org.orcid.core.manager.CountryManager;
import org.orcid.core.manager.EncryptionManager;
import org.orcid.core.manager.v3.ActivityManager;
import org.orcid.core.manager.v3.ProfileEntityManager;
import org.orcid.core.security.visibility.filter.VisibilityFilter;
import org.orcid.frontend.web.util.NumberList;
import org.orcid.frontend.web.util.YearsList;
import org.orcid.persistence.aop.ProfileLastModifiedAspect;
import org.orcid.pojo.ajaxForm.Contributor;
import org.orcid.pojo.ajaxForm.Date;
import org.orcid.pojo.ajaxForm.PojoUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.orcid.core.aop.ProfileLastModifiedAspect;
import org.orcid.core.locale.LocaleManager;
import org.orcid.core.manager.EncryptionManager;
import org.orcid.core.manager.ProfileEntityCacheManager;
Expand Down Expand Up @@ -56,7 +57,6 @@
import org.orcid.jaxb.model.v3.release.record.FamilyName;
import org.orcid.jaxb.model.v3.release.record.GivenNames;
import org.orcid.jaxb.model.v3.release.record.Name;
import org.orcid.persistence.aop.ProfileLastModifiedAspect;
import org.orcid.persistence.jpa.entities.EmailEntity;
import org.orcid.persistence.jpa.entities.ProfileEntity;
import org.orcid.pojo.AddEmail;
Expand Down

0 comments on commit 48f6749

Please sign in to comment.