PINRemoteImageBasicCache.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // PINRemoteImageBasicCache.m
  3. // Pods
  4. //
  5. // Created by Aleksei Shevchenko on 7/28/16.
  6. //
  7. //
  8. #import "PINRemoteImageBasicCache.h"
  9. @interface PINRemoteImageBasicCache()
  10. @property (nonatomic, strong) NSCache *cache;
  11. @end
  12. @implementation PINRemoteImageBasicCache
  13. - (instancetype)init
  14. {
  15. self = [super init];
  16. if (self) {
  17. self.cache = [[NSCache alloc] init];
  18. }
  19. return self;
  20. }
  21. //******************************************************************************************************
  22. // Memory cache methods
  23. //******************************************************************************************************
  24. -(nullable id)objectFromMemoryForKey:(NSString *)key
  25. {
  26. return [self.cache objectForKey:key];
  27. }
  28. -(void)setObjectInMemory:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost
  29. {
  30. [self.cache setObject:object forKey:key cost:cost];
  31. }
  32. - (void)removeObjectForKeyFromMemory:(NSString *)key
  33. {
  34. [self.cache removeObjectForKey:key];
  35. }
  36. //******************************************************************************************************
  37. // Disk cache methods
  38. //******************************************************************************************************
  39. -(nullable id)objectFromDiskForKey:(NSString *)key
  40. {
  41. return [self.cache objectForKey:key];
  42. }
  43. -(void)objectFromDiskForKey:(NSString *)key completion:(PINRemoteImageCachingObjectBlock)completion
  44. {
  45. __weak typeof(self) weakSelf = self;
  46. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  47. if (completion) {
  48. typeof(self) strongSelf = weakSelf;
  49. completion(strongSelf, key, [strongSelf.cache objectForKey:key]);
  50. }
  51. });
  52. }
  53. -(void)setObjectOnDisk:(id)object forKey:(NSString *)key
  54. {
  55. [self.cache setObject:object forKey:key];
  56. }
  57. - (BOOL)objectExistsForKey:(NSString *)key
  58. {
  59. return [self.cache objectForKey:key] != nil;
  60. }
  61. //******************************************************************************************************
  62. // Common methods, should apply to both in-memory and disk storage
  63. //******************************************************************************************************
  64. - (void)removeObjectForKey:(NSString *)key
  65. {
  66. [self.cache removeObjectForKey:key];
  67. }
  68. - (void)removeObjectForKey:(NSString *)key completion:(PINRemoteImageCachingObjectBlock)completion
  69. {
  70. __weak typeof(self) weakSelf = self;
  71. id object = [self.cache objectForKey:key];
  72. [self.cache removeObjectForKey:key];
  73. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  74. if (completion) {
  75. typeof(self) strongSelf = weakSelf;
  76. completion(strongSelf, key, object);
  77. }
  78. });
  79. }
  80. - (void)removeAllObjects
  81. {
  82. [self.cache removeAllObjects];
  83. }
  84. @end