Skip to content

Commit

Permalink
feat(server): Remove premature caching optimizations (#454)
Browse files Browse the repository at this point in the history
This was done without any measurements. Can add back
when this is really proved to improve performance.
  • Loading branch information
fushar authored Jun 22, 2023
1 parent 1bc7f6c commit b2008d9
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 343 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,30 +27,19 @@ public class ChapterStore {
private final CourseChapterDao courseChapterDao;
private final CourseDao courseDao;

private final LoadingCache<String, Chapter> 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<Chapter> getChapters() {
return Lists.transform(chapterDao.select().all(), ChapterStore::fromModel);
}

public Optional<Chapter> 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<String, ChapterInfo> getChapterInfosByJids(Set<String> chapterJids) {
Expand Down Expand Up @@ -113,8 +98,6 @@ public Chapter createChapter(ChapterCreateData data) {

public Optional<Chapter> 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));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,37 +16,17 @@
public class CourseStore {
private final CourseDao courseDao;

private final LoadingCache<String, Course> courseByJidCache;
private final LoadingCache<String, Course> 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<Course> 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<Course> 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<Course> getCourses() {
Expand Down Expand Up @@ -84,14 +60,6 @@ public Optional<Course> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,6 +35,11 @@ public Optional<ProblemSetModel> selectBySlug(String problemSetSlug) {
.unique();
}

@Override
public List<ProblemSetModel> selectAllBySlugs(Set<String> contestSlugs) {
return select().where(columnIn(ProblemSetModel_.slug, contestSlugs)).all();
}

private static class ProblemSetHibernateQueryBuilder extends HibernateQueryBuilder<ProblemSetModel> implements ProblemSetQueryBuilder {
ProblemSetHibernateQueryBuilder(Session currentSession) {
super(currentSession, ProblemSetModel.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package judgels.jerahmeel.persistence;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import judgels.persistence.JudgelsDao;
import judgels.persistence.QueryBuilder;

public interface ProblemSetDao extends JudgelsDao<ProblemSetModel> {
ProblemSetQueryBuilder select();

Optional<ProblemSetModel> selectBySlug(String problemSetSlug);
List<ProblemSetModel> selectAllBySlugs(Set<String> contestSlugs);

interface ProblemSetQueryBuilder extends QueryBuilder<ProblemSetModel> {
ProblemSetQueryBuilder whereArchiveIs(String archiveJid);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -38,10 +35,6 @@ public class ProblemSetStore {
private final ProblemContestDao problemContestDao;
private final ArchiveDao archiveDao;

private final LoadingCache<String, ProblemSet> problemSetByJidCache;
private final LoadingCache<String, ProblemSet> problemSetBySlugCache;
private final LoadingCache<String, ProblemSet> problemSetByContestJidCache;

@Inject
public ProblemSetStore(
ProblemSetDao problemSetDao,
Expand All @@ -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<ProblemSet> 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<ProblemSet> getProblemSetBySlug(String problemSetSlug) {
return Optional.ofNullable(problemSetBySlugCache.get(problemSetSlug));
return problemSetDao.selectBySlug(problemSetSlug).map(ProblemSetStore::fromModel);
}

public Map<String, ProblemSet> getProblemSetsBySlugs(Set<String> 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<ProblemSet> 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<ProblemSetModel> 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<ProblemSet> getProblemSets(Optional<String> archiveJid, Optional<String> nameFilter, int pageNumber, int pageSize) {
Expand All @@ -128,7 +97,7 @@ public Map<String, String> getProblemSetNamesByJids(Set<String> problemSetJids)
return problemSetDao.selectByJids(problemSetJids)
.values()
.stream()
.collect(Collectors.toMap(
.collect(toMap(
c -> c.jid,
c -> c.name));
}
Expand Down Expand Up @@ -185,13 +154,6 @@ public Optional<ProblemSet> 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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,9 +50,6 @@ public class ContestStore {
private final ContestAnnouncementStore announcementStore;
private final ContestClarificationStore clarificationStore;

private final LoadingCache<String, Contest> contestByJidCache;
private final LoadingCache<String, Contest> contestBySlugCache;

@Inject
public ContestStore(
ContestDao contestDao,
Expand All @@ -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<Contest> getContestByJid(String contestJid) {
return Optional.ofNullable(contestByJidCache.get(contestJid));
return contestDao.selectByJid(contestJid).map(ContestStore::fromModel);
}

public Map<String, ContestInfo> getContestInfosByJids(Set<String> contestJids) {
Expand All @@ -103,16 +88,8 @@ public Map<String, ContestInfo> getContestInfosByJids(Set<String> contestJids) {
.build()));
}

private Contest getContestByJidUncached(String contestJid) {
return contestDao.selectByJid(contestJid).map(ContestStore::fromModel).orElse(null);
}

public Optional<Contest> 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<String, String> translateSlugsToJids(Set<String> slugs) {
Expand Down Expand Up @@ -205,14 +182,6 @@ public Optional<Contest> 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());
Expand Down Expand Up @@ -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());
Expand Down
Loading

0 comments on commit b2008d9

Please sign in to comment.