Skip to content
This repository was archived by the owner on Jan 21, 2024. It is now read-only.

Commit 5f701c8

Browse files
Merge pull request #308 from bravenewpipe/cache-VideoSegments
sponsorblock: cache to prevent fetching same VideoSegment information…
2 parents fa86c26 + 5d746cd commit 5f701c8

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/SponsorBlockUtils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
import java.nio.charset.StandardCharsets;
2929
import java.security.MessageDigest;
3030
import java.util.ArrayList;
31+
import java.util.HashMap;
32+
import java.util.Map;
3133

3234
public final class SponsorBlockUtils {
3335
private static final Application APP = App.getApp();
3436
private static final String TAG = SponsorBlockUtils.class.getSimpleName();
3537
private static final boolean DEBUG = MainActivity.DEBUG;
38+
private static Map<String, VideoSegment[]> videoSegmentsCache = new HashMap<>();
3639

3740
private SponsorBlockUtils() {
3841
}
@@ -120,6 +123,11 @@ public static VideoSegment[] getYouTubeVideoSegments(final Context context,
120123
final String params = "skipSegments/" + videoIdHash.substring(0, 4)
121124
+ "?categories=" + categoryParams;
122125

126+
final VideoSegment[] alreadyFetchedVideoSegments = videoSegmentsCache.get(params);
127+
if (alreadyFetchedVideoSegments != null) {
128+
return alreadyFetchedVideoSegments;
129+
}
130+
123131
if (!isConnected()) {
124132
return null;
125133
}
@@ -177,8 +185,9 @@ public static VideoSegment[] getYouTubeVideoSegments(final Context context,
177185
result.add(segment);
178186
}
179187
}
180-
181-
return result.toArray(new VideoSegment[0]);
188+
final VideoSegment[] segments = result.toArray(new VideoSegment[0]);
189+
videoSegmentsCache.put(params, segments);
190+
return segments;
182191
}
183192

184193
private static boolean isConnected() {

0 commit comments

Comments
 (0)