Skip to content

Commit

Permalink
[PINCache] Set a default .byteLimit to reduce disk usage & startup ti…
Browse files Browse the repository at this point in the history
…me. (TextureGroup#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.
  • Loading branch information
appleguy authored and bernieperez committed Apr 25, 2018
1 parent 1640735 commit 8d13f4e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
25 changes: 24 additions & 1 deletion Source/Details/ASPINRemoteImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -84,7 +93,21 @@ @implementation ASPINRemoteImageManager
//Share image cache with sharedImageManager image cache.
- (id <PINRemoteImageCaching>)defaultImageCache
{
return [[PINRemoteImageManager sharedImageManager] cache];
static dispatch_once_t onceToken;
static id <PINRemoteImageCaching> cache = nil;
dispatch_once(&onceToken, ^{
cache = [[PINRemoteImageManager sharedImageManager] cache];
if ([cache respondsToSelector:@selector(diskCache)]) {
id diskCache = [(id <ASPINCache>)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 <ASPINDiskCache>)diskCache).byteLimit = 20 * 1024 * 1024;
}
}
});
return cache;
}

@end
Expand Down

0 comments on commit 8d13f4e

Please sign in to comment.