From b2008d92019607d9eb3025650608335eb5d94e4b Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Thu, 22 Jun 2023 19:41:02 +0700 Subject: [PATCH] feat(server): Remove premature caching optimizations (#454) This was done without any measurements. Can add back when this is really proved to improve performance. --- .../jerahmeel/JerahmeelCacheUtils.java | 13 ---- .../jerahmeel/chapter/ChapterStore.java | 19 +----- .../judgels/jerahmeel/course/CourseStore.java | 36 +--------- .../hibernate/ProblemSetHibernateDao.java | 7 ++ .../jerahmeel/persistence/ProblemSetDao.java | 3 + .../jerahmeel/problemset/ProblemSetStore.java | 54 +++------------ .../java/judgels/uriel/UrielCacheUtils.java | 20 ------ .../judgels/uriel/contest/ContestStore.java | 37 +---------- .../contestant/ContestContestantStore.java | 39 +---------- .../contest/module/ContestModuleStore.java | 30 --------- .../scoreboard/ContestScoreboardStore.java | 20 ------ .../supervisor/ContestSupervisorStore.java | 23 +------ .../hibernate/ContestRoleHibernateDao.java | 66 ------------------- .../uriel/persistence/ContestRoleDao.java | 2 - 14 files changed, 26 insertions(+), 343 deletions(-) delete mode 100755 judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/JerahmeelCacheUtils.java delete mode 100644 judgels-backends/judgels-server-app/src/main/java/judgels/uriel/UrielCacheUtils.java diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/JerahmeelCacheUtils.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/JerahmeelCacheUtils.java deleted file mode 100755 index 4d1a68eae..000000000 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/JerahmeelCacheUtils.java +++ /dev/null @@ -1,13 +0,0 @@ -package judgels.jerahmeel; - -import java.time.Duration; - -public class JerahmeelCacheUtils { - private static Duration shortDuration = Duration.ofSeconds(5); - - private JerahmeelCacheUtils() {} - - public static Duration getShortDuration() { - return shortDuration; - } -} diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/chapter/ChapterStore.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/chapter/ChapterStore.java index abfee6101..e329f61e5 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/chapter/ChapterStore.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/chapter/ChapterStore.java @@ -1,9 +1,5 @@ package judgels.jerahmeel.chapter; -import static judgels.jerahmeel.JerahmeelCacheUtils.getShortDuration; - -import com.github.benmanes.caffeine.cache.Caffeine; -import com.github.benmanes.caffeine.cache.LoadingCache; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -31,18 +27,11 @@ public class ChapterStore { private final CourseChapterDao courseChapterDao; private final CourseDao courseDao; - private final LoadingCache chapterByJidCache; - @Inject public ChapterStore(ChapterDao chapterDao, CourseChapterDao courseChapterDao, CourseDao courseDao) { this.chapterDao = chapterDao; this.courseChapterDao = courseChapterDao; this.courseDao = courseDao; - - this.chapterByJidCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(this::getChapterByJidUncached); } public List getChapters() { @@ -50,11 +39,7 @@ public List getChapters() { } public Optional getChapterByJid(String chapterJid) { - return Optional.ofNullable(chapterByJidCache.get(chapterJid)); - } - - private Chapter getChapterByJidUncached(String chapterJid) { - return chapterDao.selectByJid(chapterJid).map(ChapterStore::fromModel).orElse(null); + return chapterDao.selectByJid(chapterJid).map(ChapterStore::fromModel); } public Map getChapterInfosByJids(Set chapterJids) { @@ -113,8 +98,6 @@ public Chapter createChapter(ChapterCreateData data) { public Optional updateChapter(String chapterJid, ChapterUpdateData data) { return chapterDao.selectByJid(chapterJid).map(model -> { - chapterByJidCache.invalidate(chapterJid); - data.getName().ifPresent(name -> model.name = name); return fromModel(chapterDao.update(model)); }); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/course/CourseStore.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/course/CourseStore.java index ef2610e9a..f061707a0 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/course/CourseStore.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/course/CourseStore.java @@ -1,9 +1,5 @@ package judgels.jerahmeel.course; -import static judgels.jerahmeel.JerahmeelCacheUtils.getShortDuration; - -import com.github.benmanes.caffeine.cache.Caffeine; -import com.github.benmanes.caffeine.cache.LoadingCache; import com.google.common.collect.Lists; import java.util.List; import java.util.Optional; @@ -20,37 +16,17 @@ public class CourseStore { private final CourseDao courseDao; - private final LoadingCache courseByJidCache; - private final LoadingCache courseBySlugCache; - @Inject public CourseStore(CourseDao courseDao) { this.courseDao = courseDao; - - this.courseByJidCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(this::getCourseByJidUncached); - this.courseBySlugCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(this::getCourseBySlugUncached); } public Optional getCourseByJid(String courseJid) { - return Optional.ofNullable(courseByJidCache.get(courseJid)); - } - - private Course getCourseByJidUncached(String courseJid) { - return courseDao.selectByJid(courseJid).map(CourseStore::fromModel).orElse(null); + return courseDao.selectByJid(courseJid).map(CourseStore::fromModel); } public Optional getCourseBySlug(String courseSlug) { - return Optional.ofNullable(courseBySlugCache.get(courseSlug)); - } - - private Course getCourseBySlugUncached(String courseSlug) { - return courseDao.selectBySlug(courseSlug).map(CourseStore::fromModel).orElse(null); + return courseDao.selectBySlug(courseSlug).map(CourseStore::fromModel); } public List getCourses() { @@ -84,14 +60,6 @@ public Optional updateCourse(String courseJid, CourseUpdateData data) { } } - courseByJidCache.invalidate(courseJid); - if (model.slug != null) { - courseBySlugCache.invalidate(model.slug); - } - if (data.getSlug().isPresent()) { - courseBySlugCache.invalidate(data.getSlug().get()); - } - data.getSlug().ifPresent(slug -> model.slug = slug); data.getName().ifPresent(name -> model.name = name); data.getDescription().ifPresent(description -> model.description = description); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/hibernate/ProblemSetHibernateDao.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/hibernate/ProblemSetHibernateDao.java index b5b5bbf2a..31b58a3e6 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/hibernate/ProblemSetHibernateDao.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/hibernate/ProblemSetHibernateDao.java @@ -1,6 +1,8 @@ package judgels.jerahmeel.hibernate; +import java.util.List; import java.util.Optional; +import java.util.Set; import javax.inject.Inject; import judgels.jerahmeel.persistence.ProblemSetDao; import judgels.jerahmeel.persistence.ProblemSetModel; @@ -33,6 +35,11 @@ public Optional selectBySlug(String problemSetSlug) { .unique(); } + @Override + public List selectAllBySlugs(Set contestSlugs) { + return select().where(columnIn(ProblemSetModel_.slug, contestSlugs)).all(); + } + private static class ProblemSetHibernateQueryBuilder extends HibernateQueryBuilder implements ProblemSetQueryBuilder { ProblemSetHibernateQueryBuilder(Session currentSession) { super(currentSession, ProblemSetModel.class); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/persistence/ProblemSetDao.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/persistence/ProblemSetDao.java index 3155d5016..cfaea28b1 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/persistence/ProblemSetDao.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/persistence/ProblemSetDao.java @@ -1,6 +1,8 @@ package judgels.jerahmeel.persistence; +import java.util.List; import java.util.Optional; +import java.util.Set; import judgels.persistence.JudgelsDao; import judgels.persistence.QueryBuilder; @@ -8,6 +10,7 @@ public interface ProblemSetDao extends JudgelsDao { ProblemSetQueryBuilder select(); Optional selectBySlug(String problemSetSlug); + List selectAllBySlugs(Set contestSlugs); interface ProblemSetQueryBuilder extends QueryBuilder { ProblemSetQueryBuilder whereArchiveIs(String archiveJid); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problemset/ProblemSetStore.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problemset/ProblemSetStore.java index ec991b826..d4d77429d 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problemset/ProblemSetStore.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problemset/ProblemSetStore.java @@ -1,9 +1,7 @@ package judgels.jerahmeel.problemset; -import static judgels.jerahmeel.JerahmeelCacheUtils.getShortDuration; +import static java.util.stream.Collectors.toMap; -import com.github.benmanes.caffeine.cache.Caffeine; -import com.github.benmanes.caffeine.cache.LoadingCache; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -13,7 +11,6 @@ import java.util.Map; import java.util.Optional; import java.util.Set; -import java.util.stream.Collectors; import javax.inject.Inject; import judgels.jerahmeel.api.problemset.ProblemSet; import judgels.jerahmeel.api.problemset.ProblemSetCreateData; @@ -38,10 +35,6 @@ public class ProblemSetStore { private final ProblemContestDao problemContestDao; private final ArchiveDao archiveDao; - private final LoadingCache problemSetByJidCache; - private final LoadingCache problemSetBySlugCache; - private final LoadingCache problemSetByContestJidCache; - @Inject public ProblemSetStore( ProblemSetDao problemSetDao, @@ -53,55 +46,31 @@ public ProblemSetStore( this.problemSetProblemDao = problemSetProblemDao; this.problemContestDao = problemContestDao; this.archiveDao = archiveDao; - - this.problemSetByJidCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(this::getProblemSetByJidUncached); - this.problemSetBySlugCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(this::getProblemSetBySlugUncached); - this.problemSetByContestJidCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(this::getProblemSetByContestJidUncached); } public Optional getProblemSetByJid(String problemSetJid) { - return Optional.ofNullable(problemSetByJidCache.get(problemSetJid)); - } - - private ProblemSet getProblemSetByJidUncached(String problemSetJid) { - return problemSetDao.selectByJid(problemSetJid).map(ProblemSetStore::fromModel).orElse(null); + return problemSetDao.selectByJid(problemSetJid).map(ProblemSetStore::fromModel); } public Optional getProblemSetBySlug(String problemSetSlug) { - return Optional.ofNullable(problemSetBySlugCache.get(problemSetSlug)); + return problemSetDao.selectBySlug(problemSetSlug).map(ProblemSetStore::fromModel); } public Map getProblemSetsBySlugs(Set problemSetSlugs) { - return problemSetBySlugCache.getAll(problemSetSlugs); - } - - private ProblemSet getProblemSetBySlugUncached(String problemSetSlug) { - return problemSetDao.selectBySlug(problemSetSlug).map(ProblemSetStore::fromModel).orElse(null); + return problemSetDao.selectAllBySlugs(problemSetSlugs).stream().collect(toMap( + m -> m.slug, ProblemSetStore::fromModel)); } public Optional getProblemSetByContestJid(String contestJid) { - return Optional.ofNullable(problemSetByContestJidCache.get(contestJid)); - } - - private ProblemSet getProblemSetByContestJidUncached(String contestJid) { for (ProblemContestModel pcm : problemContestDao.selectAllByContestJid(contestJid)) { for (ProblemSetProblemModel pspm : problemSetProblemDao.selectAllByProblemJid(pcm.problemJid)) { Optional m = problemSetDao.selectByJid(pspm.problemSetJid); if (m.isPresent()) { - return fromModel(m.get()); + return Optional.of(fromModel(m.get())); } } } - return null; + return Optional.empty(); } public Page getProblemSets(Optional archiveJid, Optional nameFilter, int pageNumber, int pageSize) { @@ -128,7 +97,7 @@ public Map getProblemSetNamesByJids(Set problemSetJids) return problemSetDao.selectByJids(problemSetJids) .values() .stream() - .collect(Collectors.toMap( + .collect(toMap( c -> c.jid, c -> c.name)); } @@ -185,13 +154,6 @@ public Optional updateProblemSet(String problemSetJid, ProblemSetUpd model.archiveJid = archiveModel.get().jid; } - problemSetByJidCache.invalidate(problemSetJid); - if (model.slug != null) { - problemSetBySlugCache.invalidate(model.slug); - } - if (data.getSlug().isPresent()) { - problemSetBySlugCache.invalidate(data.getSlug().get()); - } data.getSlug().ifPresent(slug -> model.slug = slug); data.getName().ifPresent(name -> model.name = name); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/UrielCacheUtils.java b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/UrielCacheUtils.java deleted file mode 100644 index bd6c93e5e..000000000 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/UrielCacheUtils.java +++ /dev/null @@ -1,20 +0,0 @@ -package judgels.uriel; - -import java.time.Duration; - -public class UrielCacheUtils { - public static final String SEPARATOR = "#"; - - private static Duration shortDuration = Duration.ofSeconds(5); - - private UrielCacheUtils() {} - - public static Duration getShortDuration() { - return shortDuration; - } - - // Visible for testing - public static void removeDurations() { - shortDuration = Duration.ZERO; - } -} diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/ContestStore.java b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/ContestStore.java index ab056f63b..de8a580cc 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/ContestStore.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/ContestStore.java @@ -2,10 +2,7 @@ import static java.time.temporal.ChronoUnit.MILLIS; import static java.util.stream.Collectors.toMap; -import static judgels.uriel.UrielCacheUtils.getShortDuration; -import com.github.benmanes.caffeine.cache.Caffeine; -import com.github.benmanes.caffeine.cache.LoadingCache; import com.google.common.collect.Lists; import java.time.Duration; import java.time.Instant; @@ -53,9 +50,6 @@ public class ContestStore { private final ContestAnnouncementStore announcementStore; private final ContestClarificationStore clarificationStore; - private final LoadingCache contestByJidCache; - private final LoadingCache contestBySlugCache; - @Inject public ContestStore( ContestDao contestDao, @@ -75,19 +69,10 @@ public ContestStore( this.managerStore = managerStore; this.announcementStore = announcementStore; this.clarificationStore = clarificationStore; - - this.contestByJidCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(this::getContestByJidUncached); - this.contestBySlugCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(this::getContestBySlugUncached); } public Optional getContestByJid(String contestJid) { - return Optional.ofNullable(contestByJidCache.get(contestJid)); + return contestDao.selectByJid(contestJid).map(ContestStore::fromModel); } public Map getContestInfosByJids(Set contestJids) { @@ -103,16 +88,8 @@ public Map getContestInfosByJids(Set contestJids) { .build())); } - private Contest getContestByJidUncached(String contestJid) { - return contestDao.selectByJid(contestJid).map(ContestStore::fromModel).orElse(null); - } - public Optional getContestBySlug(String contestSlug) { - return Optional.ofNullable(contestBySlugCache.get(contestSlug)); - } - - private Contest getContestBySlugUncached(String contestSlug) { - return contestDao.selectBySlug(contestSlug).map(ContestStore::fromModel).orElse(null); + return contestDao.selectBySlug(contestSlug).map(ContestStore::fromModel); } public Map translateSlugsToJids(Set slugs) { @@ -205,14 +182,6 @@ public Optional updateContest(String contestJid, ContestUpdateData cont } } - contestByJidCache.invalidate(contestJid); - if (model.slug != null) { - contestBySlugCache.invalidate(model.slug); - } - if (contestUpdateData.getSlug().isPresent()) { - contestBySlugCache.invalidate(contestUpdateData.getSlug().get()); - } - contestUpdateData.getSlug().ifPresent(slug -> model.slug = slug); contestUpdateData.getName().ifPresent(name -> model.name = name); contestUpdateData.getStyle().ifPresent(style -> model.style = style.name()); @@ -251,8 +220,6 @@ public String importDump(ContestDump dump) { model.description = dump.getDescription(); contestDao.setModelMetadataFromDump(model, dump); model = contestDao.persist(model); - contestByJidCache.invalidate(model.jid); - contestBySlugCache.invalidate(model.slug); String contestJid = model.jid; moduleStore.importStyleDump(contestJid, dump.getStyle()); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/contestant/ContestContestantStore.java b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/contestant/ContestContestantStore.java index 81838c3e7..d3e5c34ab 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/contestant/ContestContestantStore.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/contestant/ContestContestantStore.java @@ -1,11 +1,7 @@ package judgels.uriel.contest.contestant; -import static judgels.uriel.UrielCacheUtils.SEPARATOR; -import static judgels.uriel.UrielCacheUtils.getShortDuration; import static judgels.uriel.api.contest.contestant.ContestContestantStatus.APPROVED; -import com.github.benmanes.caffeine.cache.Cache; -import com.github.benmanes.caffeine.cache.Caffeine; import com.google.common.collect.Lists; import java.time.Clock; import java.util.Map; @@ -21,26 +17,16 @@ import judgels.uriel.api.contest.dump.ContestContestantDump; import judgels.uriel.persistence.ContestContestantDao; import judgels.uriel.persistence.ContestContestantModel; -import judgels.uriel.persistence.ContestRoleDao; @Singleton public class ContestContestantStore { private final ContestContestantDao contestantDao; - private final ContestRoleDao roleDao; private final Clock clock; - private final Cache contestantCache; - @Inject - public ContestContestantStore(ContestContestantDao contestantDao, ContestRoleDao roleDao, Clock clock) { + public ContestContestantStore(ContestContestantDao contestantDao, Clock clock) { this.contestantDao = contestantDao; - this.roleDao = roleDao; this.clock = clock; - - this.contestantCache = Caffeine.newBuilder() - .maximumSize(1_000) - .expireAfterWrite(getShortDuration()) - .build(); } public boolean upsertContestant(String contestJid, String userJid) { @@ -54,10 +40,6 @@ public boolean upsertContestant(String contestJid, String userJid) { toModel(contestJid, userJid, model); contestantDao.insert(model); } - - contestantCache.invalidate(contestJid + SEPARATOR + userJid); - roleDao.invalidateCaches(userJid, contestJid); - return !maybeModel.isPresent(); } @@ -74,17 +56,13 @@ public boolean deleteContestant(String contestJid, String userJid) { Optional maybeModel = contestantDao.selectByContestJidAndUserJid(contestJid, userJid); if (maybeModel.isPresent()) { contestantDao.delete(maybeModel.get()); - contestantCache.invalidate(contestJid + SEPARATOR + userJid); - roleDao.invalidateCaches(userJid, contestJid); return true; } return false; } public Optional getContestant(String contestJid, String userJid) { - return Optional.ofNullable(contestantCache.get( - contestJid + SEPARATOR + userJid, - $ -> getContestantUncached(contestJid, userJid))); + return contestantDao.selectByContestJidAndUserJid(contestJid, userJid).map(ContestContestantStore::fromModel); } public Map getContestantFinalRanks(String userJid) { @@ -96,19 +74,10 @@ public Map getContestantFinalRanks(String userJid) { .collect(Collectors.toMap(m -> m.contestJid, m -> m.finalRank)); } - private ContestContestant getContestantUncached(String contestJid, String userJid) { - return contestantDao.selectByContestJidAndUserJid(contestJid, userJid) - .map(ContestContestantStore::fromModel) - .orElse(null); - } - public void startVirtualContest(String contestJid, String userJid) { contestantDao.selectByContestJidAndUserJid(contestJid, userJid).ifPresent(model -> { model.contestStartTime = clock.instant(); contestantDao.update(model); - - contestantCache.invalidate(contestJid + SEPARATOR + userJid); - roleDao.invalidateCaches(userJid, contestJid); }); } @@ -116,9 +85,6 @@ public void resetVirtualContest(String contestJid) { contestantDao.selectByContestJid(contestJid).all().forEach(model -> { model.contestStartTime = null; contestantDao.update(model); - - contestantCache.invalidate(contestJid + SEPARATOR + model.userJid); - roleDao.invalidateCaches(model.userJid, contestJid); }); } @@ -160,7 +126,6 @@ public void importDump(String contestJid, ContestContestantDump dump) { model.finalRank = dump.getFinalRank().orElse(null); contestantDao.setModelMetadataFromDump(model, dump); contestantDao.persist(model); - contestantCache.invalidate(contestJid + SEPARATOR + model.userJid); } public Set exportDumps(String contestJid, DumpImportMode mode) { diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/module/ContestModuleStore.java b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/module/ContestModuleStore.java index 707e72c41..d41ef1ca3 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/module/ContestModuleStore.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/module/ContestModuleStore.java @@ -1,7 +1,5 @@ package judgels.uriel.contest.module; -import static judgels.uriel.UrielCacheUtils.SEPARATOR; -import static judgels.uriel.UrielCacheUtils.getShortDuration; import static judgels.uriel.api.contest.module.ContestModuleType.CLARIFICATION; import static judgels.uriel.api.contest.module.ContestModuleType.CLARIFICATION_TIME_LIMIT; import static judgels.uriel.api.contest.module.ContestModuleType.DIVISION; @@ -18,8 +16,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.benmanes.caffeine.cache.Cache; -import com.github.benmanes.caffeine.cache.Caffeine; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; @@ -54,7 +50,6 @@ import judgels.uriel.api.contest.module.VirtualModuleConfig; import judgels.uriel.persistence.ContestModuleDao; import judgels.uriel.persistence.ContestModuleModel; -import judgels.uriel.persistence.ContestRoleDao; import judgels.uriel.persistence.ContestStyleDao; import judgels.uriel.persistence.ContestStyleModel; @@ -76,27 +71,17 @@ public class ContestModuleStore { private final ContestStyleDao styleDao; // TODO(fushar): put style config in module store as well private final ContestModuleDao moduleDao; - private final ContestRoleDao roleDao; private final ObjectMapper mapper; - private final Cache> moduleCache; - @Inject public ContestModuleStore( ContestStyleDao styleDao, ContestModuleDao moduleDao, - ContestRoleDao roleDao, ObjectMapper mapper) { this.styleDao = styleDao; this.moduleDao = moduleDao; - this.roleDao = roleDao; this.mapper = mapper; - - this.moduleCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(); } public Set getEnabledModules(String contestJid) { @@ -116,9 +101,6 @@ public void enableModule(String contestJid, ContestModuleType type) { ContestModuleModel model = maybeModel.get(); model.enabled = true; moduleDao.update(model); - - moduleCache.invalidate(contestJid + SEPARATOR + type.name()); - roleDao.invalidateCaches(); } else { upsertModule(contestJid, type, DEFAULT_CONFIGS.getOrDefault(type, Collections.emptyMap())); } @@ -130,9 +112,6 @@ public void disableModule(String contestJid, ContestModuleType type) { ContestModuleModel model = maybeModel.get(); model.enabled = false; moduleDao.update(model); - - moduleCache.invalidate(contestJid + SEPARATOR + type.name()); - roleDao.invalidateCaches(); } } @@ -485,9 +464,6 @@ private void upsertModule(String contestJid, ContestModuleType type, Object conf toModel(contestJid, type, config, model); moduleDao.insert(model); } - - moduleCache.invalidate(contestJid + SEPARATOR + type.name()); - roleDao.invalidateCaches(); } private void toModel(String contestJid, ContestModuleType type, Object config, ContestModuleModel model) { @@ -531,12 +507,6 @@ private Optional getStyleConfig(String contestJid, Class configClass) @SuppressWarnings("unchecked") private Optional getModuleConfig(String contestJid, ContestModuleType module, Class configClass) { - return (Optional) moduleCache.get( - contestJid + SEPARATOR + module.name(), - $ -> getModuleConfigUncached(contestJid, module, configClass)); - } - - private Optional getModuleConfigUncached(String contestJid, ContestModuleType module, Class configClass) { return moduleDao.selectEnabledByContestJidAndType(contestJid, module) .map(model -> parseConfig(model.config, configClass)); } diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/scoreboard/ContestScoreboardStore.java b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/scoreboard/ContestScoreboardStore.java index ff1f2e8a8..a6641544f 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/scoreboard/ContestScoreboardStore.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/scoreboard/ContestScoreboardStore.java @@ -1,10 +1,5 @@ package judgels.uriel.contest.scoreboard; -import static judgels.uriel.UrielCacheUtils.SEPARATOR; -import static judgels.uriel.UrielCacheUtils.getShortDuration; - -import com.github.benmanes.caffeine.cache.Cache; -import com.github.benmanes.caffeine.cache.Caffeine; import java.util.Optional; import javax.inject.Inject; import judgels.uriel.api.contest.scoreboard.ContestScoreboardType; @@ -14,25 +9,12 @@ public class ContestScoreboardStore { private final ContestScoreboardDao scoreboardDao; - private final Cache scoreboardCache; - @Inject public ContestScoreboardStore(ContestScoreboardDao scoreboardDao) { this.scoreboardDao = scoreboardDao; - - this.scoreboardCache = Caffeine.newBuilder() - .maximumSize(10) - .expireAfterWrite(getShortDuration()) - .build(); } public Optional getScoreboard(String contestJid, ContestScoreboardType type) { - return Optional.ofNullable(scoreboardCache.get( - contestJid + SEPARATOR + type.name(), - $ -> getScoreboardUncached(contestJid, type).orElse(null))); - } - - private Optional getScoreboardUncached(String contestJid, ContestScoreboardType type) { return scoreboardDao.selectByContestJidAndType(contestJid, type).map(this::fromModel); } @@ -50,8 +32,6 @@ public RawContestScoreboard upsertScoreboard(String contestJid, ContestScoreboar toModel(contestJid, data, model); scoreboard = fromModel(scoreboardDao.insert(model)); } - - scoreboardCache.invalidate(contestJid + SEPARATOR + data.getType().name()); return scoreboard; } diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/supervisor/ContestSupervisorStore.java b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/supervisor/ContestSupervisorStore.java index 272dd09fd..77982ef91 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/supervisor/ContestSupervisorStore.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/supervisor/ContestSupervisorStore.java @@ -1,12 +1,7 @@ package judgels.uriel.contest.supervisor; -import static judgels.uriel.UrielCacheUtils.SEPARATOR; -import static judgels.uriel.UrielCacheUtils.getShortDuration; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.benmanes.caffeine.cache.Cache; -import com.github.benmanes.caffeine.cache.Caffeine; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import java.io.IOException; @@ -28,17 +23,10 @@ public class ContestSupervisorStore { private final ContestSupervisorDao supervisorDao; private final ObjectMapper mapper; - private final Cache supervisorCache; - @Inject public ContestSupervisorStore(ContestSupervisorDao supervisorDao, ObjectMapper mapper) { this.supervisorDao = supervisorDao; this.mapper = mapper; - - this.supervisorCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(); } public boolean isSupervisorWithManagementPermission( @@ -55,15 +43,7 @@ public boolean isSupervisorWithManagementPermission( } public Optional getSupervisor(String contestJid, String userJid) { - return Optional.ofNullable(supervisorCache.get( - contestJid + SEPARATOR + userJid, - $ -> getSupervisorUncached(contestJid, userJid))); - } - - private ContestSupervisor getSupervisorUncached(String contestJid, String userJid) { - return supervisorDao.selectByContestJidAndUserJid(contestJid, userJid) - .map(this::fromModel) - .orElse(null); + return supervisorDao.selectByContestJidAndUserJid(contestJid, userJid).map(this::fromModel); } public ContestSupervisor upsertSupervisor( @@ -83,7 +63,6 @@ public ContestSupervisor upsertSupervisor( toModel(contestJid, userJid, managementPermissions, model); supervisor = fromModel(supervisorDao.insert(model)); } - supervisorCache.invalidate(contestJid + SEPARATOR + userJid); return supervisor; } diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/hibernate/ContestRoleHibernateDao.java b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/hibernate/ContestRoleHibernateDao.java index c0fe5694a..9c6247c18 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/hibernate/ContestRoleHibernateDao.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/hibernate/ContestRoleHibernateDao.java @@ -3,12 +3,8 @@ import static judgels.persistence.CriteriaPredicate.and; import static judgels.persistence.CriteriaPredicate.not; import static judgels.persistence.CriteriaPredicate.or; -import static judgels.uriel.UrielCacheUtils.SEPARATOR; -import static judgels.uriel.UrielCacheUtils.getShortDuration; import static judgels.uriel.api.contest.contestant.ContestContestantStatus.APPROVED; -import com.github.benmanes.caffeine.cache.Cache; -import com.github.benmanes.caffeine.cache.Caffeine; import javax.inject.Inject; import javax.inject.Singleton; import javax.persistence.criteria.Root; @@ -31,41 +27,13 @@ @Singleton public class ContestRoleHibernateDao extends JudgelsHibernateDao implements ContestRoleDao { - private final Cache viewerOrAboveCache; - private final Cache contestantCache; - private final Cache supervisorOrAboveCache; - private final Cache managerCache; - @Inject public ContestRoleHibernateDao(HibernateDaoData data) { super(data); - - this.viewerOrAboveCache = Caffeine.newBuilder() - .maximumSize(1_000) - .expireAfterWrite(getShortDuration()) - .build(); - this.contestantCache = Caffeine.newBuilder() - .maximumSize(1_000) - .expireAfterWrite(getShortDuration()) - .build(); - this.supervisorOrAboveCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(); - this.managerCache = Caffeine.newBuilder() - .maximumSize(100) - .expireAfterWrite(getShortDuration()) - .build(); } @Override public boolean isViewerOrAbove(String userJid, String contestJid) { - return viewerOrAboveCache.get( - userJid + SEPARATOR + contestJid, - $ -> isViewerOrAboveUncached(userJid, contestJid)); - } - - private boolean isViewerOrAboveUncached(String userJid, String contestJid) { return select() .where(contestIs(contestJid)) .where(userCanView(userJid)) @@ -75,12 +43,6 @@ private boolean isViewerOrAboveUncached(String userJid, String contestJid) { @Override public boolean isContestant(String userJid, String contestJid) { - return contestantCache.get( - userJid + SEPARATOR + contestJid, - $ -> isContestantUncached(userJid, contestJid)); - } - - private boolean isContestantUncached(String userJid, String contestJid) { return select() .where(contestIs(contestJid)) .where(userCanViewAsContestant(userJid)) @@ -90,12 +52,6 @@ private boolean isContestantUncached(String userJid, String contestJid) { @Override public boolean isSupervisorOrAbove(String userJid, String contestJid) { - return supervisorOrAboveCache.get( - userJid + SEPARATOR + contestJid, - $ -> isSupervisorOrAboveUncached(userJid, contestJid)); - } - - private boolean isSupervisorOrAboveUncached(String userJid, String contestJid) { return select() .where(contestIs(contestJid)) .where(userCanViewAsSupervisorOrAbove(userJid)) @@ -105,12 +61,6 @@ private boolean isSupervisorOrAboveUncached(String userJid, String contestJid) { @Override public boolean isManager(String userJid, String contestJid) { - return managerCache.get( - userJid + SEPARATOR + contestJid, - $ -> isManagerUncached(userJid, contestJid)); - } - - private boolean isManagerUncached(String userJid, String contestJid) { return select() .where(contestIs(contestJid)) .where(userIsManager(userJid)) @@ -118,22 +68,6 @@ private boolean isManagerUncached(String userJid, String contestJid) { .isPresent(); } - @Override - public void invalidateCaches(String userJid, String contestJid) { - viewerOrAboveCache.invalidate(userJid + SEPARATOR + contestJid); - contestantCache.invalidate(userJid + SEPARATOR + contestJid); - supervisorOrAboveCache.invalidate(userJid + SEPARATOR + contestJid); - managerCache.invalidate(userJid + SEPARATOR + contestJid); - } - - @Override - public void invalidateCaches() { - viewerOrAboveCache.invalidateAll(); - contestantCache.invalidateAll(); - supervisorOrAboveCache.invalidateAll(); - managerCache.invalidateAll(); - } - static CriteriaPredicate contestIs(String contestJid) { return (cb, cq, root) -> cb.equal(root.get(ContestModel_.jid), contestJid); } diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/persistence/ContestRoleDao.java b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/persistence/ContestRoleDao.java index 411803719..34d2ac884 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/persistence/ContestRoleDao.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/persistence/ContestRoleDao.java @@ -5,6 +5,4 @@ public interface ContestRoleDao { boolean isContestant(String userJid, String contestJid); boolean isSupervisorOrAbove(String userJid, String contestJid); boolean isManager(String userJid, String contestJid); - void invalidateCaches(String userJid, String contestJid); - void invalidateCaches(); }