From 376e0682643fb9a20fe4f3a3604f31d6e17ef287 Mon Sep 17 00:00:00 2001 From: FireMaskterK <20838718+FireMasterK@users.noreply.github.com> Date: Wed, 17 Nov 2021 09:54:46 +0000 Subject: [PATCH] Add DownloaderImpl caching. --- .../java/me/kavin/piped/utils/DownloaderImpl.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/kavin/piped/utils/DownloaderImpl.java b/src/main/java/me/kavin/piped/utils/DownloaderImpl.java index fa5eb80..d0a8858 100644 --- a/src/main/java/me/kavin/piped/utils/DownloaderImpl.java +++ b/src/main/java/me/kavin/piped/utils/DownloaderImpl.java @@ -20,6 +20,8 @@ import org.schabi.newpipe.extractor.downloader.Request; import org.schabi.newpipe.extractor.downloader.Response; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; import com.grack.nanojson.JsonParserException; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; @@ -32,11 +34,20 @@ public class DownloaderImpl extends Downloader { private static long cookie_received; private static final Object cookie_lock = new Object(); + final LoadingCache responseCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES) + .maximumSize(1000).build(key -> { + return executeRequest(key); + }); + + @Override + public Response execute(Request request) throws IOException, ReCaptchaException { + return responseCache.get(request); + } + /** * Executes a request with HTTP/2. */ - @Override - public Response execute(Request request) throws IOException, ReCaptchaException { + public Response executeRequest(Request request) throws IOException, ReCaptchaException { // TODO: HTTP/3 aka QUIC Builder builder = HttpRequest.newBuilder(URI.create(request.url()));