sponsorblock: cache to prevent fetching same VideoSegment information again

This commit is contained in:
evermind 2023-02-17 23:53:28 +01:00
parent 5d222fce16
commit 5d746cd4e4

View file

@ -28,11 +28,14 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public final class SponsorBlockUtils { public final class SponsorBlockUtils {
private static final Application APP = App.getApp(); private static final Application APP = App.getApp();
private static final String TAG = SponsorBlockUtils.class.getSimpleName(); private static final String TAG = SponsorBlockUtils.class.getSimpleName();
private static final boolean DEBUG = MainActivity.DEBUG; private static final boolean DEBUG = MainActivity.DEBUG;
private static Map<String, VideoSegment[]> videoSegmentsCache = new HashMap<>();
private SponsorBlockUtils() { private SponsorBlockUtils() {
} }
@ -120,6 +123,11 @@ public final class SponsorBlockUtils {
final String params = "skipSegments/" + videoIdHash.substring(0, 4) final String params = "skipSegments/" + videoIdHash.substring(0, 4)
+ "?categories=" + categoryParams; + "?categories=" + categoryParams;
final VideoSegment[] alreadyFetchedVideoSegments = videoSegmentsCache.get(params);
if (alreadyFetchedVideoSegments != null) {
return alreadyFetchedVideoSegments;
}
if (!isConnected()) { if (!isConnected()) {
return null; return null;
} }
@ -177,8 +185,9 @@ public final class SponsorBlockUtils {
result.add(segment); result.add(segment);
} }
} }
final VideoSegment[] segments = result.toArray(new VideoSegment[0]);
return result.toArray(new VideoSegment[0]); videoSegmentsCache.put(params, segments);
return segments;
} }
private static boolean isConnected() { private static boolean isConnected() {