diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCachedCourseAccess.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCachedCourseAccess.java index 90d57739..7e2a186a 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCachedCourseAccess.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCachedCourseAccess.java @@ -9,6 +9,8 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl; import java.util.Collection; +import java.util.HashSet; +import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,10 +49,31 @@ public abstract class AbstractCachedCourseAccess extends AbstractCourseAccess { /** Used to clear the entire cache */ public void clearCache() { final Object nativeCache = this.cache.getNativeCache(); -// if (nativeCache instanceof Eh107Cache) { -// -// } - this.cache.clear(); + if (nativeCache instanceof javax.cache.Cache) { + try { + final String suffix = Constants.UNDERLINE.toString() + getLmsSetupId(); + final Set keysToRemove = new HashSet<>(); + @SuppressWarnings({ "unchecked" }) + final javax.cache.Cache _cache = + (javax.cache.Cache) this.cache.getNativeCache(); + for (final javax.cache.Cache.Entry entry : _cache) { + if (entry.getKey().endsWith(suffix)) { + keysToRemove.add(entry.getKey()); + } + } + + if (!keysToRemove.isEmpty()) { + synchronized (this.cache) { + _cache.removeAll(keysToRemove); + } + } + } catch (final Exception e) { + log.error("Failed to clear particular LMS Setup cache: ", e); + this.cache.clear(); + } + } else { + this.cache.clear(); + } } /** Get the for the given quiz id QuizData from cache . diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java index 7bd23c89..5f363f14 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java @@ -94,7 +94,9 @@ public class LmsAPIServiceImpl implements LmsAPIService { final LmsAPITemplate removedTemplate = this.cache .remove(new CacheKey(lmsSetup.getModelId(), 0)); - removedTemplate.clearCache(); + if (removedTemplate != null) { + removedTemplate.clearCache(); + } } @Override