Improvements to caffeine caching. (#171)

This commit is contained in:
Kavin 2022-01-26 04:00:56 +00:00 committed by GitHub
parent de9f855fcc
commit 9aa16b3299
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 44 deletions

View file

@ -12,7 +12,9 @@ import java.net.http.HttpResponse.BodyHandlers;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.github.benmanes.caffeine.cache.Scheduler;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Downloader;
@ -34,13 +36,13 @@ public class DownloaderImpl extends Downloader {
private static long cookie_received; private static long cookie_received;
private static final Object cookie_lock = new Object(); private static final Object cookie_lock = new Object();
final LoadingCache<Request, Response> responseCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES) final LoadingCache<Request, Response> responseCache = Caffeine.newBuilder()
.maximumSize(1000).build(key -> { .expireAfterWrite(1, TimeUnit.MINUTES)
return executeRequest(key); .scheduler(Scheduler.systemScheduler())
}); .maximumSize(1000).build(this::executeRequest);
@Override @Override
public Response execute(Request request) throws IOException, ReCaptchaException { public Response execute(@NotNull Request request) {
return responseCache.get(request); return responseCache.get(request);
} }

View file

@ -29,6 +29,7 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import com.github.benmanes.caffeine.cache.Scheduler;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -104,8 +105,9 @@ import me.kavin.piped.utils.resp.SubscribeStatusResponse;
public class ResponseHelper { public class ResponseHelper {
public static final LoadingCache<String, CommentsInfo> commentsCache = Caffeine.newBuilder() public static final LoadingCache<String, CommentsInfo> commentsCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS).maximumSize(1000) .expireAfterWrite(1, TimeUnit.HOURS)
.build(key -> CommentsInfo.getInfo("https://www.youtube.com/watch?v=" + key)); .scheduler(Scheduler.systemScheduler())
.maximumSize(1000).build(key -> CommentsInfo.getInfo("https://www.youtube.com/watch?v=" + key));
public static final byte[] streamsResponse(String videoId) throws Exception { public static final byte[] streamsResponse(String videoId) throws Exception {