From 48c780ed90765418b4e79210be136fa3d853a21d Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 12 Aug 2022 19:50:53 +0530 Subject: [PATCH] Use AsyncLoadingCache for Downloader requests. --- .../java/me/kavin/piped/utils/DownloaderImpl.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/me/kavin/piped/utils/DownloaderImpl.java b/src/main/java/me/kavin/piped/utils/DownloaderImpl.java index 5928ad2..8953efc 100644 --- a/src/main/java/me/kavin/piped/utils/DownloaderImpl.java +++ b/src/main/java/me/kavin/piped/utils/DownloaderImpl.java @@ -1,7 +1,7 @@ package me.kavin.piped.utils; +import com.github.benmanes.caffeine.cache.AsyncLoadingCache; import com.github.benmanes.caffeine.cache.Caffeine; -import com.github.benmanes.caffeine.cache.LoadingCache; import com.github.benmanes.caffeine.cache.Scheduler; import com.grack.nanojson.JsonParserException; import me.kavin.piped.consts.Constants; @@ -27,14 +27,19 @@ public class DownloaderImpl extends Downloader { private static long cookie_received; private static final Object cookie_lock = new Object(); - final LoadingCache responseCache = Caffeine.newBuilder() + final AsyncLoadingCache responseCache = Caffeine.newBuilder() .expireAfterWrite(1, TimeUnit.MINUTES) .scheduler(Scheduler.systemScheduler()) - .maximumSize(100).build(this::executeRequest); + .maximumSize(100).buildAsync(this::executeRequest); @Override public Response execute(@NotNull Request request) { - return responseCache.get(request); + try { + System.out.println(responseCache.get(request).get()); + return responseCache.get(request).get(); + } catch (Exception e) { + throw new RuntimeException(e); + } } /**