PINCache.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // PINCache is a modified version of TMCache
  2. // Modifications by Garrett Moon
  3. // Copyright (c) 2015 Pinterest. All rights reserved.
  4. #import <Foundation/Foundation.h>
  5. #import <PINCacheMacros.h>
  6. #import <PINCaching.h>
  7. #import <PINDiskCache.h>
  8. #import <PINMemoryCache.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. @class PINCache;
  11. /**
  12. `PINCache` is a thread safe key/value store designed for persisting temporary objects that are expensive to
  13. reproduce, such as downloaded data or the results of slow processing. It is comprised of two self-similar
  14. stores, one in memory (<PINMemoryCache>) and one on disk (<PINDiskCache>).
  15. `PINCache` itself actually does very little; its main function is providing a front end for a common use case:
  16. a small, fast memory cache that asynchronously persists itself to a large, slow disk cache. When objects are
  17. removed from the memory cache in response to an "apocalyptic" event they remain in the disk cache and are
  18. repopulated in memory the next time they are accessed. `PINCache` also does the tedious work of creating a
  19. dispatch group to wait for both caches to finish their operations without blocking each other.
  20. The parallel caches are accessible as public properties (<memoryCache> and <diskCache>) and can be manipulated
  21. separately if necessary. See the docs for <PINMemoryCache> and <PINDiskCache> for more details.
  22. @warning when using in extension or watch extension, define PIN_APP_EXTENSIONS=1
  23. */
  24. PIN_SUBCLASSING_RESTRICTED
  25. @interface PINCache : NSObject <PINCaching, PINCacheObjectSubscripting>
  26. #pragma mark -
  27. /// @name Core
  28. /**
  29. Synchronously retrieves the total byte count of the <diskCache> on the shared disk queue.
  30. */
  31. @property (readonly) NSUInteger diskByteCount;
  32. /**
  33. The underlying disk cache, see <PINDiskCache> for additional configuration and trimming options.
  34. */
  35. @property (readonly) PINDiskCache *diskCache;
  36. /**
  37. The underlying memory cache, see <PINMemoryCache> for additional configuration and trimming options.
  38. */
  39. @property (readonly) PINMemoryCache *memoryCache;
  40. #pragma mark - Lifecycle
  41. /// @name Initialization
  42. /**
  43. A shared cache.
  44. @result The shared singleton cache instance.
  45. */
  46. @property (class, strong, readonly) PINCache *sharedCache;
  47. - (instancetype)init NS_UNAVAILABLE;
  48. /**
  49. Multiple instances with the same name are *not* allowed and can *not* safely
  50. access the same data on disk. Also used to create the <diskCache>.
  51. @see name
  52. @param name The name of the cache.
  53. @result A new cache with the specified name.
  54. */
  55. - (instancetype)initWithName:(nonnull NSString *)name;
  56. /**
  57. Multiple instances with the same name are *not* allowed and can *not* safely
  58. access the same data on disk. Also used to create the <diskCache>.
  59. @see name
  60. @param name The name of the cache.
  61. @param rootPath The path of the cache on disk.
  62. @result A new cache with the specified name.
  63. */
  64. - (instancetype)initWithName:(nonnull NSString *)name rootPath:(nonnull NSString *)rootPath;
  65. /**
  66. Multiple instances with the same name are *not* allowed and can *not* safely
  67. access the same data on disk.. Also used to create the <diskCache>.
  68. Initializer allows you to override default NSKeyedArchiver/NSKeyedUnarchiver serialization for <diskCache>.
  69. You must provide both serializer and deserializer, or opt-out to default implementation providing nil values.
  70. @see name
  71. @param name The name of the cache.
  72. @param rootPath The path of the cache on disk.
  73. @param serializer A block used to serialize object before writing to disk. If nil provided, default NSKeyedArchiver serialized will be used.
  74. @param deserializer A block used to deserialize object read from disk. If nil provided, default NSKeyedUnarchiver serialized will be used.
  75. @result A new cache with the specified name.
  76. */
  77. - (instancetype)initWithName:(NSString *)name
  78. rootPath:(NSString *)rootPath
  79. serializer:(nullable PINDiskCacheSerializerBlock)serializer
  80. deserializer:(nullable PINDiskCacheDeserializerBlock)deserializer;
  81. /**
  82. Multiple instances with the same name are *not* allowed and can *not* safely
  83. access the same data on disk. Also used to create the <diskCache>.
  84. Initializer allows you to override default NSKeyedArchiver/NSKeyedUnarchiver serialization for <diskCache>.
  85. You must provide both serializer and deserializer, or opt-out to default implementation providing nil values.
  86. @see name
  87. @param name The name of the cache.
  88. @param rootPath The path of the cache on disk.
  89. @param serializer A block used to serialize object before writing to disk. If nil provided, default NSKeyedArchiver serialized will be used.
  90. @param deserializer A block used to deserialize object read from disk. If nil provided, default NSKeyedUnarchiver serialized will be used.
  91. @param keyEncoder A block used to encode key(filename). If nil provided, default url encoder will be used
  92. @param keyDecoder A block used to decode key(filename). If nil provided, default url decoder will be used
  93. @result A new cache with the specified name.
  94. */
  95. - (instancetype)initWithName:(nonnull NSString *)name
  96. rootPath:(nonnull NSString *)rootPath
  97. serializer:(nullable PINDiskCacheSerializerBlock)serializer
  98. deserializer:(nullable PINDiskCacheDeserializerBlock)deserializer
  99. keyEncoder:(nullable PINDiskCacheKeyEncoderBlock)keyEncoder
  100. keyDecoder:(nullable PINDiskCacheKeyDecoderBlock)keyDecoder NS_DESIGNATED_INITIALIZER;
  101. @end
  102. @interface PINCache (Deprecated)
  103. - (void)containsObjectForKey:(NSString *)key block:(PINCacheObjectContainmentBlock)block __attribute__((deprecated));
  104. - (void)objectForKey:(NSString *)key block:(PINCacheObjectBlock)block __attribute__((deprecated));
  105. - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key block:(nullable PINCacheObjectBlock)block __attribute__((deprecated));
  106. - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key withCost:(NSUInteger)cost block:(nullable PINCacheObjectBlock)block __attribute__((deprecated));
  107. - (void)removeObjectForKey:(NSString *)key block:(nullable PINCacheObjectBlock)block __attribute__((deprecated));
  108. - (void)trimToDate:(NSDate *)date block:(nullable PINCacheBlock)block __attribute__((deprecated));
  109. - (void)removeAllObjects:(nullable PINCacheBlock)block __attribute__((deprecated));
  110. @end
  111. NS_ASSUME_NONNULL_END