[vlc-commits] macosx/library: improve artwork caching strategy

Felix Paul Kühne git at videolan.org
Sun Jan 26 21:52:38 CET 2020


vlc | branch: master | Felix Paul Kühne <felix at feepk.net> | Sun Jan 26 21:22:11 2020 +0100| [f4ba432113cb5a4dd75e2821a80d4ee92d6008ce] | committer: Felix Paul Kühne

macosx/library: improve artwork caching strategy

Instead of caching by library ID, cache by MRL as it may not be unique across multiple library items, for instance regarding album tracks.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=f4ba432113cb5a4dd75e2821a80d4ee92d6008ce
---

 modules/gui/macosx/library/VLCLibraryImageCache.h |  3 +--
 modules/gui/macosx/library/VLCLibraryImageCache.m | 29 +++++------------------
 2 files changed, 7 insertions(+), 25 deletions(-)

diff --git a/modules/gui/macosx/library/VLCLibraryImageCache.h b/modules/gui/macosx/library/VLCLibraryImageCache.h
index 18e6cef04d..763bd400c6 100644
--- a/modules/gui/macosx/library/VLCLibraryImageCache.h
+++ b/modules/gui/macosx/library/VLCLibraryImageCache.h
@@ -28,8 +28,7 @@ NS_ASSUME_NONNULL_BEGIN
 
 @interface VLCLibraryImageCache : NSObject
 
-+ (NSImage *)thumbnailForMediaItemWithID:(int64_t)libraryID;
-+ (NSImage *)thumbnailForMediaItem:(VLCMediaLibraryMediaItem *)mediaItem;
++ (nullable NSImage *)thumbnailForMediaItem:(VLCMediaLibraryMediaItem *)mediaItem;
 
 @end
 
diff --git a/modules/gui/macosx/library/VLCLibraryImageCache.m b/modules/gui/macosx/library/VLCLibraryImageCache.m
index 29255f2873..d71693c18b 100644
--- a/modules/gui/macosx/library/VLCLibraryImageCache.m
+++ b/modules/gui/macosx/library/VLCLibraryImageCache.m
@@ -20,17 +20,15 @@
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
 
-
 #import "VLCLibraryImageCache.h"
 #import "library/VLCLibraryDataTypes.h"
 #import "main/VLCMain.h"
 
-NSUInteger kVLCMaximumImageCacheSize = 50;
+NSUInteger kVLCMaximumLibraryImageCacheSize = 50;
 uint32_t kVLCDesiredThumbnailWidth = 512;
 uint32_t kVLCDesiredThumbnailHeight = 320;
 float kVLCDefaultThumbnailPosition = .15;
 
-
 @interface VLCLibraryImageCache()
 {
     NSCache *_imageCache;
@@ -46,7 +44,7 @@ float kVLCDefaultThumbnailPosition = .15;
     self = [super init];
     if (self) {
         _imageCache = [[NSCache alloc] init];
-        _imageCache.countLimit = kVLCMaximumImageCacheSize;
+        _imageCache.countLimit = kVLCMaximumLibraryImageCacheSize;
     }
     return self;
 }
@@ -61,11 +59,6 @@ float kVLCDefaultThumbnailPosition = .15;
     return sharedImageCache;
 }
 
-+ (NSImage *)thumbnailForMediaItemWithID:(int64_t)libraryID
-{
-    return [[VLCLibraryImageCache sharedImageCache] imageForMediaItemWithID:libraryID];
-}
-
 + (NSImage *)thumbnailForMediaItem:(VLCMediaLibraryMediaItem *)mediaItem
 {
     return [[VLCLibraryImageCache sharedImageCache] imageForMediaItem:mediaItem];
@@ -73,36 +66,26 @@ float kVLCDefaultThumbnailPosition = .15;
 
 - (NSImage *)imageForMediaItem:(VLCMediaLibraryMediaItem *)mediaItem
 {
-    NSImage *cachedImage = [_imageCache objectForKey:@(mediaItem.libraryID)];
-    if (cachedImage) {
-        return cachedImage;
-    }
-    return [self smallThumbnailForMediaItem:mediaItem];
-}
-
-- (NSImage *)imageForMediaItemWithID:(int64_t)libraryID
-{
-    NSNumber *libraryIDnumber = @(libraryID);
-    NSImage *cachedImage = [_imageCache objectForKey:libraryIDnumber];
+    NSImage *cachedImage = [_imageCache objectForKey:mediaItem.smallArtworkMRL];
     if (cachedImage) {
         return cachedImage;
     }
-    VLCMediaLibraryMediaItem *mediaItem = [VLCMediaLibraryMediaItem mediaItemForLibraryID:libraryID];
     return [self smallThumbnailForMediaItem:mediaItem];
 }
 
 - (NSImage *)smallThumbnailForMediaItem:(VLCMediaLibraryMediaItem *)mediaItem
 {
     NSImage *image;
+    NSString *artworkMRL = mediaItem.smallArtworkMRL;
     if (mediaItem.smallArtworkGenerated) {
-        image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:mediaItem.smallArtworkMRL]];
+        image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:artworkMRL]];
     } else {
         if (mediaItem.mediaType != VLC_ML_MEDIA_TYPE_AUDIO) {
             [self generateThumbnailForMediaItem:mediaItem.libraryID];
         }
     }
     if (image) {
-        [_imageCache setObject:image forKey:@(mediaItem.libraryID)];
+        [_imageCache setObject:image forKey:artworkMRL];
     }
     return image;
 }



More information about the vlc-commits mailing list