From 7a07d9eb88273b9e7621b6e615bdc2f8f1d2cd59 Mon Sep 17 00:00:00 2001 From: appleguy Date: Tue, 3 Oct 2017 19:32:03 -0700 Subject: [PATCH] [PINCache] Set a default .byteLimit to reduce disk usage & startup time. (#595) * [PINCache] Set a default .byteLimit to reduce disk usage & startup time. This default is fairly low - only 20MB - but for most apps with images in the size range of 10-50KB, this is still 400-1000 images. Once some optimizations land to PINCache, we'll match the PINCache default of 50MB to ensure the default better serves users with larger objects in the cache. Apps should preferably set their own byteLimit to an optimal value. @garrettmoon - one interesting question for us is the best place to set .byteLimit as an app. Digging into the ASPINRemoteImageDownloader and doing this type cast is a bit complicated, so a passthrough API to get the PIN* objects directly might be worthwhile. * [PINCache] Declare necessary APIs to avoid a direct dependency. --- CHANGELOG.md | 1 + Source/Details/ASPINRemoteImageDownloader.m | 25 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd484096e..835422a94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## master * Add your own contributions to the next release on the line below this with your name. +- [PINCache] Set a default .byteLimit to reduce disk usage and startup time. [#595](https://github.com/TextureGroup/Texture/pull/595) [Scott Goodson](https://github.com/appleguy) - [ASNetworkImageNode] Fix deadlock in GIF handling. [#582](https://github.com/TextureGroup/Texture/pull/582) [Garrett Moon](https://github.com/garrettmoon) - [ASDisplayNode] Add attributed versions of a11y label, hint and value. [#554](https://github.com/TextureGroup/Texture/pull/554) [Alexander Hüllmandel](https://github.com/fruitcoder) - [ASCornerRounding] Introduce .cornerRoundingType: CALayer, Precomposited, or Clip Corners. [Scott Goodson](https://github.com/appleguy) [#465](https://github.com/TextureGroup/Texture/pull/465) diff --git a/Source/Details/ASPINRemoteImageDownloader.m b/Source/Details/ASPINRemoteImageDownloader.m index 0e6c6adff..601d00945 100644 --- a/Source/Details/ASPINRemoteImageDownloader.m +++ b/Source/Details/ASPINRemoteImageDownloader.m @@ -76,6 +76,15 @@ - (BOOL)isDataSupported:(NSData *)data @end #endif +// Declare two key methods on PINCache objects, avoiding a direct dependency on PINCache.h +@protocol ASPINCache +- (id)diskCache; +@end + +@protocol ASPINDiskCache +@property (assign) NSUInteger byteLimit; +@end + @interface ASPINRemoteImageManager : PINRemoteImageManager @end @@ -84,7 +93,21 @@ @implementation ASPINRemoteImageManager //Share image cache with sharedImageManager image cache. - (id )defaultImageCache { - return [[PINRemoteImageManager sharedImageManager] cache]; + static dispatch_once_t onceToken; + static id cache = nil; + dispatch_once(&onceToken, ^{ + cache = [[PINRemoteImageManager sharedImageManager] cache]; + if ([cache respondsToSelector:@selector(diskCache)]) { + id diskCache = [(id )cache diskCache]; + if ([diskCache respondsToSelector:@selector(setByteLimit:)]) { + // Set a default byteLimit. PINCache recently implemented a 50MB default (PR #201). + // Ensure that older versions of PINCache also have a byteLimit applied. + // NOTE: Using 20MB limit while large cache initialization is being optimized (Issue #144). + ((id )diskCache).byteLimit = 20 * 1024 * 1024; + } + } + }); + return cache; } @end