1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 16:58:51 -05:00

Refactor media info cache synchronization

This commit is contained in:
Reinhard Pointner 2016-08-10 03:26:55 +08:00
parent 8f12961fe8
commit 53226d0809
2 changed files with 26 additions and 32 deletions

View File

@ -1013,7 +1013,7 @@ public class MediaBindingBean {
return getMediaFile(); return getMediaFile();
} }
private static final Map<File, MediaInfo> sharedMediaInfoObjects = new WeakValueHashMap<File, MediaInfo>(64); private static final Map<File, MediaInfo> sharedMediaInfoObjects = synchronizedMap(new WeakValueHashMap<File, MediaInfo>(64));
private synchronized MediaInfo getMediaInfo() { private synchronized MediaInfo getMediaInfo() {
// lazy initialize // lazy initialize
@ -1021,7 +1021,6 @@ public class MediaBindingBean {
// use inferred media file (e.g. actual movie file instead of subtitle file) // use inferred media file (e.g. actual movie file instead of subtitle file)
File inferredMediaFile = getInferredMediaFile(); File inferredMediaFile = getInferredMediaFile();
synchronized (sharedMediaInfoObjects) {
mediaInfo = sharedMediaInfoObjects.computeIfAbsent(inferredMediaFile, f -> { mediaInfo = sharedMediaInfoObjects.computeIfAbsent(inferredMediaFile, f -> {
try { try {
return new MediaInfo().open(f); return new MediaInfo().open(f);
@ -1030,7 +1029,6 @@ public class MediaBindingBean {
} }
}); });
} }
}
return mediaInfo; return mediaInfo;
} }

View File

@ -163,7 +163,7 @@ public enum SubtitleMetrics implements SimilarityMetric {
private Map<String, Object> getSubtitleProperties(OpenSubtitlesSubtitleDescriptor subtitle) { private Map<String, Object> getSubtitleProperties(OpenSubtitlesSubtitleDescriptor subtitle) {
try { try {
Map<String, Object> props = new HashMap<String, Object>(); Map<String, Object> props = new HashMap<String, Object>(2);
float fps = Math.round(subtitle.getMovieFPS()); // round because most FPS values in the database are bad anyway float fps = Math.round(subtitle.getMovieFPS()); // round because most FPS values in the database are bad anyway
if (fps > 0) { if (fps > 0) {
props.put(FPS, fps); props.put(FPS, fps);
@ -179,15 +179,12 @@ public enum SubtitleMetrics implements SimilarityMetric {
return emptyMap(); return emptyMap();
} }
private final Map<File, Map<String, Object>> mediaInfoCache = new WeakHashMap<File, Map<String, Object>>(64); private final Map<File, Map<String, Object>> mediaInfoCache = synchronizedMap(new WeakHashMap<File, Map<String, Object>>(64));
private Map<String, Object> getVideoProperties(File file) { private Map<String, Object> getVideoProperties(File file) {
synchronized (mediaInfoCache) { return mediaInfoCache.computeIfAbsent(file, key -> {
return mediaInfoCache.computeIfAbsent(file, (f) -> { try (MediaInfo mediaInfo = new MediaInfo().open(file)) {
try { Map<String, Object> props = new HashMap<String, Object>(2);
Map<String, Object> props = new HashMap<String, Object>();
MediaInfo mediaInfo = new MediaInfo().open(file);
float fps = Math.round(Float.parseFloat(mediaInfo.get(StreamKind.Video, 0, "FrameRate"))); float fps = Math.round(Float.parseFloat(mediaInfo.get(StreamKind.Video, 0, "FrameRate")));
if (fps > 0) { if (fps > 0) {
props.put(FPS, fps); props.put(FPS, fps);
@ -203,7 +200,6 @@ public enum SubtitleMetrics implements SimilarityMetric {
return emptyMap(); return emptyMap();
}); });
} }
}
}); });
// inner metric // inner metric