PINCache.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <PINCache/PINCacheMacros.h>
  6. #import <PINCache/PINCaching.h>
  7. #import <PINCache/PINDiskCache.h>
  8. #import <PINCache/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 allowed and can safely access
  50. the same data on disk thanks to the magic of seriality. 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 allowed and can safely access
  58. the same data on disk thanks to the magic of seriality. Also used to create the <diskCache>.
  59. @see name
  60. @param name The name of the cache.
  61. @param fileExtension The file extension for files on disk.
  62. @result A new cache with the specified name.
  63. */
  64. - (instancetype)initWithName:(nonnull NSString *)name fileExtension:(nullable NSString *)fileExtension;
  65. /**
  66. Multiple instances with the same name are allowed and can safely access
  67. the same data on disk thanks to the magic of seriality. Also used to create the <diskCache>.
  68. @see name
  69. @param name The name of the cache.
  70. @param rootPath The path of the cache on disk.
  71. @param fileExtension The file extension for files on disk.
  72. @result A new cache with the specified name.
  73. */
  74. - (instancetype)initWithName:(nonnull NSString *)name rootPath:(nonnull NSString *)rootPath fileExtension:(nullable NSString *)fileExtension;
  75. /**
  76. Multiple instances with the same name are allowed and can safely access
  77. the same data on disk thanks to the magic of seriality. Also used to create the <diskCache>.
  78. Initializer allows you to override default NSKeyedArchiver/NSKeyedUnarchiver serialization for <diskCache>.
  79. You must provide both serializer and deserializer, or opt-out to default implementation providing nil values.
  80. @see name
  81. @param name The name of the cache.
  82. @param rootPath The path of the cache on disk.
  83. @param serializer A block used to serialize object before writing to disk. If nil provided, default NSKeyedArchiver serialized will be used.
  84. @param deserializer A block used to deserialize object read from disk. If nil provided, default NSKeyedUnarchiver serialized will be used.
  85. @param fileExtension The file extension for files on disk.
  86. @result A new cache with the specified name.
  87. */
  88. - (instancetype)initWithName:(nonnull NSString *)name rootPath:(nonnull NSString *)rootPath serializer:(nullable PINDiskCacheSerializerBlock)serializer deserializer:(nullable PINDiskCacheDeserializerBlock)deserializer fileExtension:(nullable NSString *)fileExtension NS_DESIGNATED_INITIALIZER;
  89. @end
  90. @interface PINCache (Deprecated)
  91. - (void)containsObjectForKey:(NSString *)key block:(PINCacheObjectContainmentBlock)block __attribute__((deprecated));
  92. - (void)objectForKey:(NSString *)key block:(PINCacheObjectBlock)block __attribute__((deprecated));
  93. - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key block:(nullable PINCacheObjectBlock)block __attribute__((deprecated));
  94. - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key withCost:(NSUInteger)cost block:(nullable PINCacheObjectBlock)block __attribute__((deprecated));
  95. - (void)removeObjectForKey:(NSString *)key block:(nullable PINCacheObjectBlock)block __attribute__((deprecated));
  96. - (void)trimToDate:(NSDate *)date block:(nullable PINCacheBlock)block __attribute__((deprecated));
  97. - (void)removeAllObjects:(nullable PINCacheBlock)block __attribute__((deprecated));
  98. @end
  99. NS_ASSUME_NONNULL_END