Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Use ExpiringValue to avoid successive uncached calls to getVotingResu…
Browse files Browse the repository at this point in the history
…lts (#45)

* Use ExpiringValue to avoid successive uncached calls to getVotingResults

* Update conference-impl/src/main/java/org/tweetwallfx/conference/impl/ConferenceClientImpl.java
  • Loading branch information
mklaehn authored Oct 4, 2023
1 parent b3ab023 commit 7c49d94
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.random.RandomGenerator;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -62,14 +63,16 @@
import org.tweetwallfx.conference.spi.TrackImpl;
import org.tweetwallfx.conference.spi.util.RestCallHelper;
import org.tweetwallfx.config.Configuration;
import org.tweetwallfx.util.ExpiringValue;

public class ConferenceClientImpl implements ConferenceClient, RatingClient {
public final class ConferenceClientImpl implements ConferenceClient, RatingClient {

private static final Logger LOG = LoggerFactory.getLogger(ConferenceClientImpl.class);
private final ConferenceClientSettings config;
private final Map<String, SessionType> sessionTypes;
private final Map<String, Room> rooms;
private final Map<String, Track> tracks;
private final ExpiringValue<Map<WeekDay, List<RatedTalk>>> ratedTalks;

public ConferenceClientImpl() {
this.config = Configuration.getInstance().getConfigTyped(
Expand All @@ -93,6 +96,7 @@ public ConferenceClientImpl() {
.map(this::convertTrack)
.collect(Collectors.toMap(Identifiable::getId, Function.identity()));
LOG.info("Track IDs: {}", tracks.keySet());
this.ratedTalks = new ExpiringValue<>(this::getVotingResults, Duration.ofSeconds(20));
}

@Override
Expand Down Expand Up @@ -132,10 +136,11 @@ public List<ScheduleSlot> getSchedule(final String conferenceDay, final String r

@Override
public List<Speaker> getSpeakers() {
return RestCallHelper.readOptionalFrom(config.getEventBaseUri() + "speakers", listOfMaps(), (a, b) -> {
a.addAll(b);
return a;
})
return RestCallHelper
.readOptionalFrom(config.getEventBaseUri() + "speakers", listOfMaps(), (a, b) -> {
a.addAll(b);
return a;
})
.orElse(List.of())
.stream()
.map(this::convertSpeaker)
Expand Down Expand Up @@ -183,7 +188,7 @@ public List<RatedTalk> getRatedTalks(final String conferenceDay) {
LOG.debug("######## randomizedRatedTalksPerDay");
return randomizedRatedTalks();
} else {
final Map<WeekDay, List<RatedTalk>> votingResults = getVotingResults();
final Map<WeekDay, List<RatedTalk>> votingResults = ratedTalks.getValue();

return votingResults.entrySet().stream()
.filter(e -> e.getKey().dayId().equals(conferenceDay))
Expand All @@ -204,10 +209,10 @@ public List<RatedTalk> getRatedTalksOverall() {
LOG.debug("######## randomizedRatedTalksWeek");
return randomizedRatedTalks();
} else {
return getVotingResults().entrySet().stream()
.map(Map.Entry::getValue)
.flatMap(List::stream)
.toList();
return ratedTalks.getValue().entrySet().stream()
.map(Map.Entry::getValue)
.flatMap(List::stream)
.toList();
}
}

Expand Down

0 comments on commit 7c49d94

Please sign in to comment.