PINCaching.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // PINCaching.h
  3. // PINCache
  4. //
  5. // Created by Michael Schneider on 1/31/17.
  6. // Copyright © 2017 Pinterest. All rights reserved.
  7. //
  8. #pragma once
  9. #import <Foundation/Foundation.h>
  10. NS_ASSUME_NONNULL_BEGIN
  11. @protocol PINCaching;
  12. /**
  13. A callback block which provides only the cache as an argument
  14. */
  15. typedef void (^PINCacheBlock)(id<PINCaching> cache);
  16. /**
  17. A callback block which provides the cache, key and object as arguments
  18. */
  19. typedef void (^PINCacheObjectBlock)(id<PINCaching> cache, NSString *key, id _Nullable object);
  20. /**
  21. A callback block which provides a BOOL value as argument
  22. */
  23. typedef void (^PINCacheObjectContainmentBlock)(BOOL containsObject);
  24. @protocol PINCaching <NSObject>
  25. #pragma mark - Core
  26. /**
  27. The name of this cache, used to create a directory under Library/Caches and also appearing in stack traces.
  28. */
  29. @property (readonly) NSString *name;
  30. #pragma mark - Asynchronous Methods
  31. /// @name Asynchronous Methods
  32. /**
  33. This method determines whether an object is present for the given key in the cache. This method returns immediately
  34. and executes the passed block after the object is available, potentially in parallel with other blocks on the
  35. <concurrentQueue>.
  36. @see containsObjectForKey:
  37. @param key The key associated with the object.
  38. @param block A block to be executed concurrently after the containment check happened
  39. */
  40. - (void)containsObjectForKeyAsync:(NSString *)key completion:(PINCacheObjectContainmentBlock)block;
  41. /**
  42. Retrieves the object for the specified key. This method returns immediately and executes the passed
  43. block after the object is available, potentially in parallel with other blocks on the <concurrentQueue>.
  44. @param key The key associated with the requested object.
  45. @param block A block to be executed concurrently when the object is available.
  46. */
  47. - (void)objectForKeyAsync:(NSString *)key completion:(PINCacheObjectBlock)block;
  48. /**
  49. Stores an object in the cache for the specified key. This method returns immediately and executes the
  50. passed block after the object has been stored, potentially in parallel with other blocks on the <concurrentQueue>.
  51. @param object An object to store in the cache.
  52. @param key A key to associate with the object. This string will be copied.
  53. @param block A block to be executed concurrently after the object has been stored, or nil.
  54. */
  55. - (void)setObjectAsync:(id)object forKey:(NSString *)key completion:(nullable PINCacheObjectBlock)block;
  56. /**
  57. Stores an object in the cache for the specified key and the specified memory cost. If the cost causes the total
  58. to go over the <memoryCache.costLimit> the cache is trimmed (oldest objects first). This method returns immediately
  59. and executes the passed block after the object has been stored, potentially in parallel with other blocks
  60. on the <concurrentQueue>.
  61. @param object An object to store in the cache.
  62. @param key A key to associate with the object. This string will be copied.
  63. @param cost An amount to add to the <memoryCache.totalCost>.
  64. @param block A block to be executed concurrently after the object has been stored, or nil.
  65. */
  66. - (void)setObjectAsync:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost completion:(nullable PINCacheObjectBlock)block;
  67. /**
  68. Removes the object for the specified key. This method returns immediately and executes the passed
  69. block after the object has been removed, potentially in parallel with other blocks on the <concurrentQueue>.
  70. @param key The key associated with the object to be removed.
  71. @param block A block to be executed concurrently after the object has been removed, or nil.
  72. */
  73. - (void)removeObjectForKeyAsync:(NSString *)key completion:(nullable PINCacheObjectBlock)block;
  74. /**
  75. Removes all objects from the cache that have not been used since the specified date. This method returns immediately and
  76. executes the passed block after the cache has been trimmed, potentially in parallel with other blocks on the <concurrentQueue>.
  77. @param date Objects that haven't been accessed since this date are removed from the cache.
  78. @param block A block to be executed concurrently after the cache has been trimmed, or nil.
  79. */
  80. - (void)trimToDateAsync:(NSDate *)date completion:(nullable PINCacheBlock)block;
  81. /**
  82. Removes all objects from the cache.This method returns immediately and executes the passed block after the
  83. cache has been cleared, potentially in parallel with other blocks on the <concurrentQueue>.
  84. @param block A block to be executed concurrently after the cache has been cleared, or nil.
  85. */
  86. - (void)removeAllObjectsAsync:(nullable PINCacheBlock)block;
  87. #pragma mark - Synchronous Methods
  88. /// @name Synchronous Methods
  89. /**
  90. This method determines whether an object is present for the given key in the cache.
  91. @see containsObjectForKeyAsync:completion:
  92. @param key The key associated with the object.
  93. @result YES if an object is present for the given key in the cache, otherwise NO.
  94. */
  95. - (BOOL)containsObjectForKey:(NSString *)key;
  96. /**
  97. Retrieves the object for the specified key. This method blocks the calling thread until the object is available.
  98. Uses a lock to achieve synchronicity on the disk cache.
  99. @see objectForKeyAsync:completion:
  100. @param key The key associated with the object.
  101. @result The object for the specified key.
  102. */
  103. - (nullable id)objectForKey:(NSString *)key;
  104. /**
  105. Stores an object in the cache for the specified key. This method blocks the calling thread until the object has been set.
  106. Uses a lock to achieve synchronicity on the disk cache.
  107. @see setObjectAsync:forKey:completion:
  108. @param object An object to store in the cache.
  109. @param key A key to associate with the object. This string will be copied.
  110. */
  111. - (void)setObject:(nullable id)object forKey:(NSString *)key;
  112. /**
  113. Stores an object in the cache for the specified key and the specified memory cost. If the cost causes the total
  114. to go over the <memoryCache.costLimit> the cache is trimmed (oldest objects first). This method blocks the calling thread
  115. until the object has been stored.
  116. @param object An object to store in the cache.
  117. @param key A key to associate with the object. This string will be copied.
  118. @param cost An amount to add to the <memoryCache.totalCost>.
  119. */
  120. - (void)setObject:(nullable id)object forKey:(NSString *)key withCost:(NSUInteger)cost;
  121. /**
  122. Removes the object for the specified key. This method blocks the calling thread until the object
  123. has been removed.
  124. Uses a lock to achieve synchronicity on the disk cache.
  125. @see removeObjectForKeyAsync:completion:
  126. @param key The key associated with the object to be removed.
  127. */
  128. - (void)removeObjectForKey:(NSString *)key;
  129. /**
  130. Removes all objects from the cache that have not been used since the specified date.
  131. This method blocks the calling thread until the cache has been trimmed.
  132. Uses a lock to achieve synchronicity on the disk cache.
  133. @see trimToDateAsync:completion:
  134. @param date Objects that haven't been accessed since this date are removed from the cache.
  135. */
  136. - (void)trimToDate:(NSDate *)date;
  137. /**
  138. Removes all objects from the cache. This method blocks the calling thread until the cache has been cleared.
  139. Uses a lock to achieve synchronicity on the disk cache.
  140. @see removeAllObjectsAsync:
  141. */
  142. - (void)removeAllObjects;
  143. @end
  144. NS_ASSUME_NONNULL_END