Files
PINRemoteImage/Pod/Classes/PINRemoteImageBasicCache.m
Garrett Moon 0d5437cab6 Don't bother archiving / unarchiving data. (#229)
* Don't bother archiving / unarchiving data.

This change upgrades to PINCache 3.0.1-beta which supports
custom archiving and unarchiving.

This allows us to avoid using NSArchiver/Archiver which adds a
small amount of disk space overhead and between .5 and 10 ms (on an iPhone 4)
for each encode / decode.

To support this, we *do* have to add versioning for the cache and
destroy it for the old versions. I considered adding a process to migrate
the images, but because that would have to occur on startup and process
all existing images, it could cause issues for very large image cache
databases, so I decided against that.

* Fix warnings

* Fix invalid object test

* Cleanup, thanks @chrisdanford!
2016-08-20 12:39:34 -07:00

99 lines
2.8 KiB
Objective-C

//
// PINRemoteImageBasicCache.m
// Pods
//
// Created by Aleksei Shevchenko on 7/28/16.
//
//
#import "PINRemoteImageBasicCache.h"
@interface PINRemoteImageBasicCache()
@property (nonatomic, strong) NSCache *cache;
@end
@implementation PINRemoteImageBasicCache
- (instancetype)init
{
self = [super init];
if (self) {
self.cache = [[NSCache alloc] init];
}
return self;
}
//******************************************************************************************************
// Memory cache methods
//******************************************************************************************************
-(nullable id)objectFromMemoryCacheForKey:(NSString *)key
{
return [self.cache objectForKey:key];
}
-(void)cacheObjectInMemory:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost
{
[self.cache setObject:object forKey:key cost:cost];
}
- (void)removeCachedObjectForKeyFromMemoryCache:(NSString *)key
{
[self.cache removeObjectForKey:key];
}
//******************************************************************************************************
// Disk cache methods
//******************************************************************************************************
-(nullable id)objectFromDiskCacheForKey:(NSString *)key
{
return [self.cache objectForKey:key];
}
-(void)objectFromDiskCacheForKey:(NSString *)key completion:(PINRemoteImageCachingObjectBlock)completion
{
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (completion) {
typeof(self) strongSelf = weakSelf;
completion(strongSelf, key, [strongSelf.cache objectForKey:key]);
}
});
}
-(void)cacheObjectOnDisk:(id)object forKey:(NSString *)key
{
[self.cache setObject:object forKey:key];
}
- (BOOL)objectExistsInCacheForKey:(NSString *)key
{
return [self.cache objectForKey:key] != nil;
}
//******************************************************************************************************
// Common methods, should apply to both in-memory and disk storage
//******************************************************************************************************
- (void)removeCachedObjectForKey:(NSString *)key
{
[self.cache removeObjectForKey:key];
}
- (void)removeCachedObjectForKey:(NSString *)key completion:(PINRemoteImageCachingObjectBlock)completion
{
__weak typeof(self) weakSelf = self;
id object = [self.cache objectForKey:key];
[self.cache removeObjectForKey:key];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (completion) {
typeof(self) strongSelf = weakSelf;
completion(strongSelf, key, object);
}
});
}
- (void)removeAllCachedObjects
{
[self.cache removeAllObjects];
}
@end