PINCache+PINRemoteImageCaching.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // PINCache+PINRemoteImageCaching.m
  3. // Pods
  4. //
  5. // Created by Aleksei Shevchenko on 7/28/16.
  6. //
  7. //
  8. #import "PINCache+PINRemoteImageCaching.h"
  9. @implementation PINCache (PINRemoteImageCaching)
  10. //******************************************************************************************************
  11. // Memory cache methods
  12. //******************************************************************************************************
  13. -(nullable id)objectFromMemoryForKey:(NSString *)key
  14. {
  15. return [self.memoryCache objectForKey:key];
  16. }
  17. -(void)setObjectInMemory:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost
  18. {
  19. [self.memoryCache setObject:object forKey:key withCost:cost];
  20. }
  21. - (void)removeObjectForKeyFromMemory:(NSString *)key
  22. {
  23. [self.memoryCache removeObjectForKey:key];
  24. }
  25. //******************************************************************************************************
  26. // Disk cache methods
  27. //******************************************************************************************************
  28. -(nullable id)objectFromDiskForKey:(NSString *)key
  29. {
  30. return [self.diskCache objectForKey:key];
  31. }
  32. -(void)objectFromDiskForKey:(NSString *)key completion:(PINRemoteImageCachingObjectBlock)completion
  33. {
  34. __weak typeof(self) weakSelf = self;
  35. [self.diskCache objectForKeyAsync:key completion:^(PINDiskCache * _Nonnull cache, NSString * _Nonnull key, id<NSCoding> _Nullable object) {
  36. if(completion) {
  37. typeof(self) strongSelf = weakSelf;
  38. completion(strongSelf, key, object);
  39. }
  40. }];
  41. }
  42. -(void)setObjectOnDisk:(id)object forKey:(NSString *)key
  43. {
  44. [self.diskCache setObject:object forKey:key];
  45. }
  46. - (BOOL)objectExistsForKey:(NSString *)key
  47. {
  48. return [self containsObjectForKey:key];
  49. }
  50. //******************************************************************************************************
  51. // Common cache methods
  52. //******************************************************************************************************
  53. - (void)removeObjectForKey:(NSString *)key completion:(PINRemoteImageCachingObjectBlock)completion
  54. {
  55. if (completion) {
  56. __weak typeof(self) weakSelf = self;
  57. [self removeObjectForKeyAsync:key completion:^(PINCache * _Nonnull cache, NSString * _Nonnull key, id _Nullable object) {
  58. typeof(self) strongSelf = weakSelf;
  59. completion(strongSelf, key, object);
  60. }];
  61. } else {
  62. [self removeObjectForKeyAsync:key completion:nil];
  63. }
  64. }
  65. @end
  66. @implementation PINRemoteImageManager (PINCache)
  67. - (PINCache <PINRemoteImageCaching> *)pinCache
  68. {
  69. static BOOL isCachePINCache = NO;
  70. static dispatch_once_t onceToken;
  71. dispatch_once(&onceToken, ^{
  72. isCachePINCache = [self.cache isKindOfClass:[PINCache class]];
  73. });
  74. if (isCachePINCache) {
  75. return (PINCache <PINRemoteImageCaching> *)self.cache;
  76. }
  77. return nil;
  78. }
  79. @end