From 5d746cd4e42deb24879b5cb4d5a05efcc3452601 Mon Sep 17 00:00:00 2001 From: evermind Date: Fri, 17 Feb 2023 23:53:28 +0100 Subject: [PATCH] sponsorblock: cache to prevent fetching same VideoSegment information again --- .../org/schabi/newpipe/util/SponsorBlockUtils.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java b/app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java index 35870e3d3..cff063ab8 100644 --- a/app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java @@ -28,11 +28,14 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; public final class SponsorBlockUtils { private static final Application APP = App.getApp(); private static final String TAG = SponsorBlockUtils.class.getSimpleName(); private static final boolean DEBUG = MainActivity.DEBUG; + private static Map videoSegmentsCache = new HashMap<>(); private SponsorBlockUtils() { } @@ -120,6 +123,11 @@ public final class SponsorBlockUtils { final String params = "skipSegments/" + videoIdHash.substring(0, 4) + "?categories=" + categoryParams; + final VideoSegment[] alreadyFetchedVideoSegments = videoSegmentsCache.get(params); + if (alreadyFetchedVideoSegments != null) { + return alreadyFetchedVideoSegments; + } + if (!isConnected()) { return null; } @@ -177,8 +185,9 @@ public final class SponsorBlockUtils { result.add(segment); } } - - return result.toArray(new VideoSegment[0]); + final VideoSegment[] segments = result.toArray(new VideoSegment[0]); + videoSegmentsCache.put(params, segments); + return segments; } private static boolean isConnected() {