PINCache+PINRemoteImageCaching.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 objectForKey:key block:^(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 removeObjectForKey:key block:^(PINCache * _Nonnull cache, NSString * _Nonnull key, id _Nullable object) {
  58. typeof(self) strongSelf = weakSelf;
  59. completion(strongSelf, key, object);
  60. }];
  61. } else {
  62. [self removeObjectForKey:key block:nil];
  63. }
  64. }
  65. @end