From 7c49d9438197bb44fe7d0988941ed045531c2821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kl=C3=A4hn?= Date: Wed, 4 Oct 2023 10:59:42 +0200 Subject: [PATCH] Use ExpiringValue to avoid successive uncached calls to getVotingResults (#45) * Use ExpiringValue to avoid successive uncached calls to getVotingResults * Update conference-impl/src/main/java/org/tweetwallfx/conference/impl/ConferenceClientImpl.java --- .../conference/impl/ConferenceClientImpl.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/conference-impl/src/main/java/org/tweetwallfx/conference/impl/ConferenceClientImpl.java b/conference-impl/src/main/java/org/tweetwallfx/conference/impl/ConferenceClientImpl.java index a31491a..7e6ce68 100644 --- a/conference-impl/src/main/java/org/tweetwallfx/conference/impl/ConferenceClientImpl.java +++ b/conference-impl/src/main/java/org/tweetwallfx/conference/impl/ConferenceClientImpl.java @@ -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; @@ -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 sessionTypes; private final Map rooms; private final Map tracks; + private final ExpiringValue>> ratedTalks; public ConferenceClientImpl() { this.config = Configuration.getInstance().getConfigTyped( @@ -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 @@ -132,10 +136,11 @@ public List getSchedule(final String conferenceDay, final String r @Override public List 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) @@ -183,7 +188,7 @@ public List getRatedTalks(final String conferenceDay) { LOG.debug("######## randomizedRatedTalksPerDay"); return randomizedRatedTalks(); } else { - final Map> votingResults = getVotingResults(); + final Map> votingResults = ratedTalks.getValue(); return votingResults.entrySet().stream() .filter(e -> e.getKey().dayId().equals(conferenceDay)) @@ -204,10 +209,10 @@ public List 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(); } }