diff --git a/pom.xml b/pom.xml
index 1aca807..abf4e16 100644
--- a/pom.xml
+++ b/pom.xml
@@ -139,6 +139,11 @@
1.6.5-1
compile
+
+ com.google.guava
+ guava
+ 21.0
+
org.apache.logging.log4j
log4j-core
diff --git a/src/main/java/musicplayer/model/Album.java b/src/main/java/musicplayer/model/Album.java
index 290ab60..3ddfcdc 100644
--- a/src/main/java/musicplayer/model/Album.java
+++ b/src/main/java/musicplayer/model/Album.java
@@ -1,5 +1,8 @@
package musicplayer.model;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
@@ -19,6 +22,7 @@ import java.nio.file.Path;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
+import java.util.concurrent.ExecutionException;
/**
* Representation of an Album.
@@ -41,6 +45,17 @@ public class Album implements Comparable, IDBType, HasSongs {
private static final String albumArtRegex = "*{Cover,Folder,cover,folder}\\.{jpg,png}";
@Transient
private static final int imageScaleToSize = 50;
+ @Transient
+ private static final LoadingCache> albumArtCache = CacheBuilder.newBuilder()
+ .maximumSize(1000)
+ .build(
+ new CacheLoader>() {
+ @Override
+ public Optional load(Album album) throws Exception {
+ return getAlbumArt(album);
+ }
+ }
+ );
protected Album() {
}
@@ -100,13 +115,26 @@ public class Album implements Comparable, IDBType, HasSongs {
}
/**
- * Get album art for a given album.
+ * Get album art for this given album.
* @return Album art.
*/
@Transient
public Optional getAlbumArt(){
+ try {
+ return albumArtCache.get(this);
+ } catch (ExecutionException ignored) {}
+ return Optional.empty();
+ }
+
+ /**
+ * Get album are for a given album.
+ * @param album Album to get art for.
+ * @return Album art.
+ */
+ @Transient
+ private static Optional getAlbumArt(Album album){
Tag audioTags;
- File targetFile = songs.iterator().next().getSongFile();
+ File targetFile = album.songs.iterator().next().getSongFile();
Path baseDir = targetFile.getParentFile().toPath();
try {
audioTags = AudioFileIO.read(targetFile).getTag();