酷店

YYMemoryCache.h 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // YYMemoryCache.h
  3. // YYCache <https://github.com/ibireme/YYCache>
  4. //
  5. // Created by ibireme on 15/2/7.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <Foundation/Foundation.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. YYMemoryCache is a fast in-memory cache that stores key-value pairs.
  15. In contrast to NSDictionary, keys are retained and not copied.
  16. The API and performance is similar to `NSCache`, all methods are thread-safe.
  17. YYMemoryCache objects differ from NSCache in a few ways:
  18. * It uses LRU (least-recently-used) to remove objects; NSCache's eviction method
  19. is non-deterministic.
  20. * It can be controlled by cost, count and age; NSCache's limits are imprecise.
  21. * It can be configured to automatically evict objects when receive memory
  22. warning or app enter background.
  23. The time of `Access Methods` in YYMemoryCache is typically in constant time (O(1)).
  24. */
  25. @interface YYMemoryCache : NSObject
  26. #pragma mark - Attribute
  27. ///=============================================================================
  28. /// @name Attribute
  29. ///=============================================================================
  30. /** The name of the cache. Default is nil. */
  31. @property (nullable, copy) NSString *name;
  32. /** The number of objects in the cache (read-only) */
  33. @property (readonly) NSUInteger totalCount;
  34. /** The total cost of objects in the cache (read-only). */
  35. @property (readonly) NSUInteger totalCost;
  36. #pragma mark - Limit
  37. ///=============================================================================
  38. /// @name Limit
  39. ///=============================================================================
  40. /**
  41. The maximum number of objects the cache should hold.
  42. @discussion The default value is NSUIntegerMax, which means no limit.
  43. This is not a strict limit—if the cache goes over the limit, some objects in the
  44. cache could be evicted later in backgound thread.
  45. */
  46. @property NSUInteger countLimit;
  47. /**
  48. The maximum total cost that the cache can hold before it starts evicting objects.
  49. @discussion The default value is NSUIntegerMax, which means no limit.
  50. This is not a strict limit—if the cache goes over the limit, some objects in the
  51. cache could be evicted later in backgound thread.
  52. */
  53. @property NSUInteger costLimit;
  54. /**
  55. The maximum expiry time of objects in cache.
  56. @discussion The default value is DBL_MAX, which means no limit.
  57. This is not a strict limit—if an object goes over the limit, the object could
  58. be evicted later in backgound thread.
  59. */
  60. @property NSTimeInterval ageLimit;
  61. /**
  62. The auto trim check time interval in seconds. Default is 5.0.
  63. @discussion The cache holds an internal timer to check whether the cache reaches
  64. its limits, and if the limit is reached, it begins to evict objects.
  65. */
  66. @property NSTimeInterval autoTrimInterval;
  67. /**
  68. If `YES`, the cache will remove all objects when the app receives a memory warning.
  69. The default value is `YES`.
  70. */
  71. @property BOOL shouldRemoveAllObjectsOnMemoryWarning;
  72. /**
  73. If `YES`, The cache will remove all objects when the app enter background.
  74. The default value is `YES`.
  75. */
  76. @property BOOL shouldRemoveAllObjectsWhenEnteringBackground;
  77. /**
  78. A block to be executed when the app receives a memory warning.
  79. The default value is nil.
  80. */
  81. @property (nullable, copy) void(^didReceiveMemoryWarningBlock)(YYMemoryCache *cache);
  82. /**
  83. A block to be executed when the app enter background.
  84. The default value is nil.
  85. */
  86. @property (nullable, copy) void(^didEnterBackgroundBlock)(YYMemoryCache *cache);
  87. /**
  88. If `YES`, the key-value pair will be released on main thread, otherwise on
  89. background thread. Default is NO.
  90. @discussion You may set this value to `YES` if the key-value object contains
  91. the instance which should be released in main thread (such as UIView/CALayer).
  92. */
  93. @property BOOL releaseOnMainThread;
  94. /**
  95. If `YES`, the key-value pair will be released asynchronously to avoid blocking
  96. the access methods, otherwise it will be released in the access method
  97. (such as removeObjectForKey:). Default is YES.
  98. */
  99. @property BOOL releaseAsynchronously;
  100. #pragma mark - Access Methods
  101. ///=============================================================================
  102. /// @name Access Methods
  103. ///=============================================================================
  104. /**
  105. Returns a Boolean value that indicates whether a given key is in cache.
  106. @param key An object identifying the value. If nil, just return `NO`.
  107. @return Whether the key is in cache.
  108. */
  109. - (BOOL)containsObjectForKey:(id)key;
  110. /**
  111. Returns the value associated with a given key.
  112. @param key An object identifying the value. If nil, just return nil.
  113. @return The value associated with key, or nil if no value is associated with key.
  114. */
  115. - (nullable id)objectForKey:(id)key;
  116. /**
  117. Sets the value of the specified key in the cache (0 cost).
  118. @param object The object to be stored in the cache. If nil, it calls `removeObjectForKey:`.
  119. @param key The key with which to associate the value. If nil, this method has no effect.
  120. @discussion Unlike an NSMutableDictionary object, a cache does not copy the key
  121. objects that are put into it.
  122. */
  123. - (void)setObject:(nullable id)object forKey:(id)key;
  124. /**
  125. Sets the value of the specified key in the cache, and associates the key-value
  126. pair with the specified cost.
  127. @param object The object to store in the cache. If nil, it calls `removeObjectForKey`.
  128. @param key The key with which to associate the value. If nil, this method has no effect.
  129. @param cost The cost with which to associate the key-value pair.
  130. @discussion Unlike an NSMutableDictionary object, a cache does not copy the key
  131. objects that are put into it.
  132. */
  133. - (void)setObject:(nullable id)object forKey:(id)key withCost:(NSUInteger)cost;
  134. /**
  135. Removes the value of the specified key in the cache.
  136. @param key The key identifying the value to be removed. If nil, this method has no effect.
  137. */
  138. - (void)removeObjectForKey:(id)key;
  139. /**
  140. Empties the cache immediately.
  141. */
  142. - (void)removeAllObjects;
  143. #pragma mark - Trim
  144. ///=============================================================================
  145. /// @name Trim
  146. ///=============================================================================
  147. /**
  148. Removes objects from the cache with LRU, until the `totalCount` is below or equal to
  149. the specified value.
  150. @param count The total count allowed to remain after the cache has been trimmed.
  151. */
  152. - (void)trimToCount:(NSUInteger)count;
  153. /**
  154. Removes objects from the cache with LRU, until the `totalCost` is or equal to
  155. the specified value.
  156. @param cost The total cost allowed to remain after the cache has been trimmed.
  157. */
  158. - (void)trimToCost:(NSUInteger)cost;
  159. /**
  160. Removes objects from the cache with LRU, until all expiry objects removed by the
  161. specified value.
  162. @param age The maximum age (in seconds) of objects.
  163. */
  164. - (void)trimToAge:(NSTimeInterval)age;
  165. @end
  166. NS_ASSUME_NONNULL_END