Add cache for the resolution

This makes the display of the resolution much faster and the information
is retained, even if the ThumbCell is "destroyed"
This commit is contained in:
0xboobface 2018-12-04 18:30:46 +01:00
parent 45e493a35a
commit b99e88d2c8
1 changed files with 43 additions and 26 deletions

View File

@ -7,11 +7,14 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.iheartradio.m3u8.ParseException; import com.iheartradio.m3u8.ParseException;
import ctbrec.Config; import ctbrec.Config;
@ -80,6 +83,10 @@ public class ThumbCell extends StackPane {
private boolean mouseHovering = false; private boolean mouseHovering = false;
private boolean recording = false; private boolean recording = false;
private static ExecutorService imageLoadingThreadPool = Executors.newFixedThreadPool(30); private static ExecutorService imageLoadingThreadPool = Executors.newFixedThreadPool(30);
private static Cache<Model, int[]> resolutionCache = CacheBuilder.newBuilder()
.expireAfterAccess(4, TimeUnit.HOURS)
.maximumSize(1000)
.build();
public ThumbCell(ThumbOverviewTab parent, Model model, Recorder recorder) { public ThumbCell(ThumbOverviewTab parent, Model model, Recorder recorder) {
this.thumbCellList = parent.grid.getChildren(); this.thumbCellList = parent.grid.getChildren();
@ -212,21 +219,30 @@ public class ThumbCell extends StackPane {
return; return;
} }
int[] resolution = resolutionCache.getIfPresent(model);
if(resolution != null) {
try {
updateResolutionTag(resolution);
} catch(Exception e) {
LOG.warn("Couldn't update resolution tag for model {}", model.getName(), e);
}
} else {
ThumbOverviewTab.threadPool.submit(() -> { ThumbOverviewTab.threadPool.submit(() -> {
try { try {
ThumbOverviewTab.resolutionProcessing.add(model); ThumbOverviewTab.resolutionProcessing.add(model);
int[] resolution = model.getStreamResolution(false); int[] _resolution = model.getStreamResolution(false);
updateResolutionTag(resolution); resolutionCache.put(model, _resolution);
updateResolutionTag(_resolution);
// the model is online, but the resolution is 0. probably something went wrong // the model is online, but the resolution is 0. probably something went wrong
// when we first requested the stream info, so we remove this invalid value from the "cache" // when we first requested the stream info, so we remove this invalid value from the "cache"
// so that it is requested again // so that it is requested again
if (model.isOnline() && resolution[1] == 0) { if (model.isOnline() && _resolution[1] == 0) {
LOG.trace("Removing invalid resolution value for {}", model.getName()); LOG.trace("Removing invalid resolution value for {}", model.getName());
model.invalidateCacheEntries(); model.invalidateCacheEntries();
} }
Thread.sleep(500); Thread.sleep(100);
} catch (IOException | InterruptedException e1) { } catch (IOException | InterruptedException e1) {
LOG.warn("Couldn't update resolution tag for model {}", model.getName(), e1); LOG.warn("Couldn't update resolution tag for model {}", model.getName(), e1);
} catch(ExecutionException e) { } catch(ExecutionException e) {
@ -242,6 +258,7 @@ public class ThumbCell extends StackPane {
} }
}); });
} }
}
private void updateResolutionTag(int[] resolution) throws IOException, ExecutionException, InterruptedException { private void updateResolutionTag(int[] resolution) throws IOException, ExecutionException, InterruptedException {
String _res = "n/a"; String _res = "n/a";