No Description

AWSTMMemoryCache.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. `TMMemoryCache` is a fast, thread safe key/value store similar to `NSCache`. On iOS it will clear itself
  3. automatically to reduce memory usage when the app receives a memory warning or goes into the background.
  4. Access is natively asynchronous. Every method accepts a callback block that runs on a concurrent
  5. <queue>, with cache writes protected by GCD barriers. Synchronous variations are provided.
  6. All access to the cache is dated so the that the least-used objects can be trimmed first. Setting an
  7. optional <ageLimit> will trigger a GCD timer to periodically to trim the cache to that age.
  8. Objects can optionally be set with a "cost", which could be a byte count or any other meaningful integer.
  9. Setting a <costLimit> will automatically keep the cache below that value with <trimToCostByDate:>.
  10. Values will not persist after application relaunch or returning from the background. See <TMCache> for
  11. a memory cache backed by a disk cache.
  12. */
  13. #import <Foundation/Foundation.h>
  14. @class AWSTMMemoryCache;
  15. typedef void (^AWSTMMemoryCacheBlock)(AWSTMMemoryCache *cache);
  16. typedef void (^AWSTMMemoryCacheObjectBlock)(AWSTMMemoryCache *cache, NSString *key, id object);
  17. @interface AWSTMMemoryCache : NSObject
  18. #pragma mark -
  19. /// @name Core
  20. /**
  21. A concurrent queue on which all work is done. It is exposed here so that it can be set to target some
  22. other queue, such as a global concurrent queue with a priority other than the default.
  23. */
  24. @property (readonly) dispatch_queue_t queue;
  25. /**
  26. The total accumulated cost.
  27. */
  28. @property (readonly) NSUInteger totalCost;
  29. /**
  30. The maximum cost allowed to accumulate before objects begin to be removed with <trimToCostByDate:>.
  31. */
  32. @property (assign) NSUInteger costLimit;
  33. /**
  34. The maximum number of seconds an object is allowed to exist in the cache. Setting this to a value
  35. greater than `0.0` will start a recurring GCD timer with the same period that calls <trimToDate:>.
  36. Setting it back to `0.0` will stop the timer. Defaults to `0.0`.
  37. */
  38. @property (assign) NSTimeInterval ageLimit;
  39. /**
  40. When `YES` on iOS the cache will remove all objects when the app receives a memory warning.
  41. Defaults to `YES`.
  42. */
  43. @property (assign) BOOL removeAllObjectsOnMemoryWarning;
  44. /**
  45. When `YES` on iOS the cache will remove all objects when the app enters the background.
  46. Defaults to `YES`.
  47. */
  48. @property (assign) BOOL removeAllObjectsOnEnteringBackground;
  49. #pragma mark -
  50. /// @name Event Blocks
  51. /**
  52. A block to be executed just before an object is added to the cache. This block will be excuted within
  53. a barrier, i.e. all reads and writes are suspended for the duration of the block.
  54. */
  55. @property (copy) AWSTMMemoryCacheObjectBlock willAddObjectBlock;
  56. /**
  57. A block to be executed just before an object is removed from the cache. This block will be excuted
  58. within a barrier, i.e. all reads and writes are suspended for the duration of the block.
  59. */
  60. @property (copy) AWSTMMemoryCacheObjectBlock willRemoveObjectBlock;
  61. /**
  62. A block to be executed just before all objects are removed from the cache as a result of <removeAllObjects:>.
  63. This block will be excuted within a barrier, i.e. all reads and writes are suspended for the duration of the block.
  64. */
  65. @property (copy) AWSTMMemoryCacheBlock willRemoveAllObjectsBlock;
  66. /**
  67. A block to be executed just after an object is added to the cache. This block will be excuted within
  68. a barrier, i.e. all reads and writes are suspended for the duration of the block.
  69. */
  70. @property (copy) AWSTMMemoryCacheObjectBlock didAddObjectBlock;
  71. /**
  72. A block to be executed just after an object is removed from the cache. This block will be excuted
  73. within a barrier, i.e. all reads and writes are suspended for the duration of the block.
  74. */
  75. @property (copy) AWSTMMemoryCacheObjectBlock didRemoveObjectBlock;
  76. /**
  77. A block to be executed just after all objects are removed from the cache as a result of <removeAllObjects:>.
  78. This block will be excuted within a barrier, i.e. all reads and writes are suspended for the duration of the block.
  79. */
  80. @property (copy) AWSTMMemoryCacheBlock didRemoveAllObjectsBlock;
  81. /**
  82. A block to be executed upon receiving a memory warning (iOS only) potentially in parallel with other blocks on the <queue>.
  83. This block will be executed regardless of the value of <removeAllObjectsOnMemoryWarning>. Defaults to `nil`.
  84. */
  85. @property (copy) AWSTMMemoryCacheBlock didReceiveMemoryWarningBlock;
  86. /**
  87. A block to be executed when the app enters the background (iOS only) potentially in parallel with other blocks on the <queue>.
  88. This block will be executed regardless of the value of <removeAllObjectsOnEnteringBackground>. Defaults to `nil`.
  89. */
  90. @property (copy) AWSTMMemoryCacheBlock didEnterBackgroundBlock;
  91. #pragma mark -
  92. /// @name Shared Cache
  93. /**
  94. A shared cache.
  95. @result The shared singleton cache instance.
  96. */
  97. + (instancetype)sharedCache;
  98. #pragma mark -
  99. /// @name Asynchronous Methods
  100. /**
  101. Retrieves the object for the specified key. This method returns immediately and executes the passed
  102. block after the object is available, potentially in parallel with other blocks on the <queue>.
  103. @param key The key associated with the requested object.
  104. @param block A block to be executed concurrently when the object is available.
  105. */
  106. - (void)objectForKey:(NSString *)key block:(AWSTMMemoryCacheObjectBlock)block;
  107. /**
  108. Stores an object in the cache for the specified key. This method returns immediately and executes the
  109. passed block after the object has been stored, potentially in parallel with other blocks on the <queue>.
  110. @param object An object to store in the cache.
  111. @param key A key to associate with the object. This string will be copied.
  112. @param block A block to be executed concurrently after the object has been stored, or nil.
  113. */
  114. - (void)setObject:(id)object forKey:(NSString *)key block:(AWSTMMemoryCacheObjectBlock)block;
  115. /**
  116. Stores an object in the cache for the specified key and the specified cost. If the cost causes the total
  117. to go over the <costLimit> the cache is trimmed (oldest objects first). This method returns immediately
  118. and executes the passed block after the object has been stored, potentially in parallel with other blocks
  119. on the <queue>.
  120. @param object An object to store in the cache.
  121. @param key A key to associate with the object. This string will be copied.
  122. @param cost An amount to add to the <totalCost>.
  123. @param block A block to be executed concurrently after the object has been stored, or nil.
  124. */
  125. - (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost block:(AWSTMMemoryCacheObjectBlock)block;
  126. /**
  127. Removes the object for the specified key. This method returns immediately and executes the passed
  128. block after the object has been removed, potentially in parallel with other blocks on the <queue>.
  129. @param key The key associated with the object to be removed.
  130. @param block A block to be executed concurrently after the object has been removed, or nil.
  131. */
  132. - (void)removeObjectForKey:(NSString *)key block:(AWSTMMemoryCacheObjectBlock)block;
  133. /**
  134. Removes all objects from the cache that have not been used since the specified date.
  135. This method returns immediately and executes the passed block after the cache has been trimmed,
  136. potentially in parallel with other blocks on the <queue>.
  137. @param date Objects that haven't been accessed since this date are removed from the cache.
  138. @param block A block to be executed concurrently after the cache has been trimmed, or nil.
  139. */
  140. - (void)trimToDate:(NSDate *)date block:(AWSTMMemoryCacheBlock)block;
  141. /**
  142. Removes objects from the cache, costliest objects first, until the <totalCost> is below the specified
  143. value. This method returns immediately and executes the passed block after the cache has been trimmed,
  144. potentially in parallel with other blocks on the <queue>.
  145. @param cost The total accumulation allowed to remain after the cache has been trimmed.
  146. @param block A block to be executed concurrently after the cache has been trimmed, or nil.
  147. */
  148. - (void)trimToCost:(NSUInteger)cost block:(AWSTMMemoryCacheBlock)block;
  149. /**
  150. Removes objects from the cache, ordered by date (least recently used first), until the <totalCost> is below
  151. the specified value. This method returns immediately and executes the passed block after the cache has been
  152. trimmed, potentially in parallel with other blocks on the <queue>.
  153. @param cost The total accumulation allowed to remain after the cache has been trimmed.
  154. @param block A block to be executed concurrently after the cache has been trimmed, or nil.
  155. */
  156. - (void)trimToCostByDate:(NSUInteger)cost block:(AWSTMMemoryCacheBlock)block;
  157. /**
  158. Removes all objects from the cache. This method returns immediately and executes the passed block after
  159. the cache has been cleared, potentially in parallel with other blocks on the <queue>.
  160. @param block A block to be executed concurrently after the cache has been cleared, or nil.
  161. */
  162. - (void)removeAllObjects:(AWSTMMemoryCacheBlock)block;
  163. /**
  164. Loops through all objects in the cache within a memory barrier (reads and writes are suspended during the enumeration).
  165. This method returns immediately.
  166. @param block A block to be executed for every object in the cache.
  167. @param completionBlock An optional block to be executed concurrently when the enumeration is complete.
  168. */
  169. - (void)enumerateObjectsWithBlock:(AWSTMMemoryCacheObjectBlock)block completionBlock:(AWSTMMemoryCacheBlock)completionBlock;
  170. #pragma mark -
  171. /// @name Synchronous Methods
  172. /**
  173. Retrieves the object for the specified key. This method blocks the calling thread until the
  174. object is available.
  175. @see objectForKey:block:
  176. @param key The key associated with the object.
  177. @result The object for the specified key.
  178. */
  179. - (id)objectForKey:(NSString *)key;
  180. /**
  181. Stores an object in the cache for the specified key. This method blocks the calling thread until the object
  182. has been set.
  183. @see setObject:forKey:block:
  184. @param object An object to store in the cache.
  185. @param key A key to associate with the object. This string will be copied.
  186. */
  187. - (void)setObject:(id)object forKey:(NSString *)key;
  188. /**
  189. Stores an object in the cache for the specified key and the specified cost. If the cost causes the total
  190. to go over the <costLimit> the cache is trimmed (oldest objects first). This method blocks the calling thread
  191. until the object has been stored.
  192. @param object An object to store in the cache.
  193. @param key A key to associate with the object. This string will be copied.
  194. @param cost An amount to add to the <totalCost>.
  195. */
  196. - (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost;
  197. /**
  198. Removes the object for the specified key. This method blocks the calling thread until the object
  199. has been removed.
  200. @param key The key associated with the object to be removed.
  201. */
  202. - (void)removeObjectForKey:(NSString *)key;
  203. /**
  204. Removes all objects from the cache that have not been used since the specified date.
  205. This method blocks the calling thread until the cache has been trimmed.
  206. @param date Objects that haven't been accessed since this date are removed from the cache.
  207. */
  208. - (void)trimToDate:(NSDate *)date;
  209. /**
  210. Removes objects from the cache, costliest objects first, until the <totalCost> is below the specified
  211. value. This method blocks the calling thread until the cache has been trimmed.
  212. @param cost The total accumulation allowed to remain after the cache has been trimmed.
  213. */
  214. - (void)trimToCost:(NSUInteger)cost;
  215. /**
  216. Removes objects from the cache, ordered by date (least recently used first), until the <totalCost> is below
  217. the specified value. This method blocks the calling thread until the cache has been trimmed.
  218. @param cost The total accumulation allowed to remain after the cache has been trimmed.
  219. */
  220. - (void)trimToCostByDate:(NSUInteger)cost;
  221. /**
  222. Removes all objects from the cache. This method blocks the calling thread until the cache has been cleared.
  223. */
  224. - (void)removeAllObjects;
  225. /**
  226. Loops through all objects in the cache within a memory barrier (reads and writes are suspended during the enumeration).
  227. This method blocks the calling thread until all objects have been enumerated.
  228. @param block A block to be executed for every object in the cache.
  229. @warning Do not call this method within the event blocks (<didReceiveMemoryWarningBlock>, etc.)
  230. Instead use the asynchronous version, <enumerateObjectsWithBlock:completionBlock:>.
  231. */
  232. - (void)enumerateObjectsWithBlock:(AWSTMMemoryCacheObjectBlock)block;
  233. /**
  234. Handle a memory warning.
  235. */
  236. - (void)handleMemoryWarning __deprecated_msg("This happens automatically in TMCache 2.1. There’s no longer a need to call it directly.");
  237. /**
  238. Handle the application having been backgrounded.
  239. */
  240. - (void)handleApplicationBackgrounding __deprecated_msg("This happens automatically in TMCache 2.1. There’s no longer a need to call it directly.");
  241. @end