caching and local tests working

This commit is contained in:
anhefti 2021-05-17 09:03:22 +02:00
parent 9214719642
commit bb9d0b068d
3 changed files with 57 additions and 29 deletions

View file

@ -21,12 +21,12 @@ import ch.ethz.seb.sebserver.gbl.async.AsyncService;
import ch.ethz.seb.sebserver.gbl.model.exam.QuizData;
import ch.ethz.seb.sebserver.gbl.util.Result;
/** This implements an overal short time cache for QuizData object for all implementing
* classes. It uses EH-Cache with a short time to live */
public abstract class AbstractCachedCourseAccess extends AbstractCourseAccess {
public static final String CACHE_NAME_QUIZ_DATA = "QUIZ_DATA_CACHE";
private static final String NO_QUIZ_DATA_FOUND_ERROR = "NO_QUIZ_DATA_FOUND_ERROR";
private final Cache cache;
protected AbstractCachedCourseAccess(
@ -58,16 +58,6 @@ public abstract class AbstractCachedCourseAccess extends AbstractCourseAccess {
this.cache.evict(createCacheKey(id));
}
@Override
public Result<QuizData> getQuizFromCache(final String id) {
final QuizData fromCache = getFromCache(id);
if (fromCache != null) {
return Result.of(fromCache);
} else {
return Result.ofRuntimeError(NO_QUIZ_DATA_FOUND_ERROR);
}
}
@Override
public Result<Collection<Result<QuizData>>> getQuizzesFromCache(final Set<String> ids) {
return Result.of(ids.stream().map(this::getQuizFromCache).collect(Collectors.toList()));

View file

@ -11,6 +11,7 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.edx;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -194,19 +195,53 @@ final class OpenEdxCourseAccess extends AbstractCachedCourseAccess {
.getOrThrow();
}
@Override
public Result<QuizData> getQuizFromCache(final String id) {
return Result.tryCatch(() -> {
// first try to get it from short time cache
QuizData quizData = super.getFromCache(id);
if (quizData != null) {
return quizData;
}
// Otherwise get one course from LMS and cache
final LmsSetup lmsSetup = getApiTemplateDataSupplier().getLmsSetup();
final String externalStartURI = getExternalLMSServerAddress(lmsSetup);
quizData = quizDataOf(
lmsSetup,
this.getOneCourse(id, this.restTemplate, id),
externalStartURI);
if (quizData != null) {
super.putToCache(quizData);
}
return quizData;
});
}
@Override
protected Supplier<List<QuizData>> quizzesSupplier(final Set<String> ids) {
if (ids.size() == 1) {
return () -> {
final String id = ids.iterator().next();
// first try to get it from short time cache
final QuizData quizData = super.getFromCache(id);
if (quizData != null) {
return Arrays.asList(quizData);
}
final LmsSetup lmsSetup = getApiTemplateDataSupplier().getLmsSetup();
final String externalStartURI = getExternalLMSServerAddress(lmsSetup);
return Arrays.asList(quizDataOf(
lmsSetup,
getOneCourses(
getOneCourse(
lmsSetup.lmsApiUrl + OPEN_EDX_DEFAULT_COURSE_ENDPOINT,
getRestTemplate().getOrThrow(),
ids),
id),
externalStartURI));
};
} else {
@ -301,7 +336,7 @@ final class OpenEdxCourseAccess extends AbstractCachedCourseAccess {
private List<CourseData> collectCourses(
final String pageURI,
final OAuth2RestTemplate restTemplate,
final Set<String> ids) {
final Collection<String> ids) {
final List<CourseData> collector = new ArrayList<>();
EdXPage page = getEdxPage(pageURI, restTemplate).getBody();
@ -324,21 +359,21 @@ final class OpenEdxCourseAccess extends AbstractCachedCourseAccess {
return collector;
}
private CourseData getOneCourses(
private CourseData getOneCourse(
final String pageURI,
final OAuth2RestTemplate restTemplate,
final Set<String> ids) {
final String id) {
System.out.println("********************");
// NOTE: try first to get the course data by id. This seems to be possible
// NOTE: try to get the course data by id. This seems to be possible
// when the SEB restriction is not set. Once the SEB restriction is set,
// this gives a 403 response.
// We haven't found another way to get course data by id in this case so far
// Workaround is to search the course by paging (slow)
try {
final HttpHeaders httpHeaders = new HttpHeaders();
final String uri = pageURI + ids.iterator().next();
final String uri = pageURI + id;
final ResponseEntity<CourseData> exchange = restTemplate.exchange(
uri,
HttpMethod.GET,
@ -348,7 +383,10 @@ final class OpenEdxCourseAccess extends AbstractCachedCourseAccess {
return exchange.getBody();
} catch (final Exception e) {
// try with paging
final List<CourseData> collectCourses = collectCourses(pageURI, restTemplate, ids);
final List<CourseData> collectCourses = collectCourses(
pageURI,
restTemplate,
Arrays.asList(id));
if (collectCourses.isEmpty()) {
return null;
}

View file

@ -9,7 +9,7 @@
<key-type>java.lang.Long</key-type>
<value-type>ch.ethz.seb.sebserver.gbl.model.exam.Exam</value-type>
<expiry>
<tti unit="hours">24</tti>
<ttl unit="hours">24</ttl>
</expiry>
<resources>
<heap unit="entries">100</heap>
@ -20,7 +20,7 @@
<key-type>java.lang.String</key-type>
<value-type>ch.ethz.seb.sebserver.webservice.servicelayer.session.impl.ClientConnectionDataInternal</value-type>
<expiry>
<tti unit="hours">24</tti>
<ttl unit="hours">24</ttl>
</expiry>
<resources>
<heap unit="entries">3000</heap>
@ -31,7 +31,7 @@
<key-type>java.lang.Long</key-type>
<value-type>ch.ethz.seb.sebserver.webservice.servicelayer.session.impl.InMemorySEBConfig</value-type>
<expiry>
<tti unit="hours">24</tti>
<ttl unit="hours">24</ttl>
</expiry>
<resources>
<heap unit="entries">20</heap>
@ -42,7 +42,7 @@
<key-type>java.lang.String</key-type>
<value-type>ch.ethz.seb.sebserver.webservice.datalayer.batis.model.ClientEventRecord</value-type>
<expiry>
<tti unit="hours">24</tti>
<ttl unit="hours">24</ttl>
</expiry>
<resources>
<heap unit="entries">2000</heap>
@ -53,7 +53,7 @@
<key-type>java.lang.Long</key-type>
<value-type>ch.ethz.seb.sebserver.gbl.util.Result</value-type>
<expiry>
<tti unit="hours">24</tti>
<ttl unit="hours">24</ttl>
</expiry>
<resources>
<heap unit="entries">100</heap>
@ -64,7 +64,7 @@
<key-type>org.springframework.security.oauth2.common.OAuth2AccessToken</key-type>
<value-type>org.springframework.security.oauth2.provider.OAuth2Authentication</value-type>
<expiry>
<tti unit="hours">24</tti>
<ttl unit="hours">24</ttl>
</expiry>
<resources>
<heap unit="entries">100</heap>
@ -75,7 +75,7 @@
<key-type>java.lang.String</key-type>
<value-type>ch.ethz.seb.sebserver.gbl.util.Result</value-type>
<expiry>
<tti unit="hours">24</tti>
<ttl unit="hours">24</ttl>
</expiry>
<resources>
<heap unit="entries">2000</heap>
@ -86,7 +86,7 @@
<key-type>java.lang.Long</key-type>
<value-type>org.ehcache.impl.internal.concurrent.ConcurrentHashMap</value-type>
<expiry>
<tti unit="hours">24</tti>
<ttl unit="hours">24</ttl>
</expiry>
<resources>
<heap unit="entries">10</heap>
@ -97,7 +97,7 @@
<key-type>java.lang.String</key-type>
<value-type>ch.ethz.seb.sebserver.gbl.model.exam.QuizData</value-type>
<expiry>
<tti unit="minutes">10</tti>
<ttl unit="minutes">1</ttl>
</expiry>
<resources>
<heap unit="entries">10000</heap>