YYCache.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // YYCache.h
  3. // YYCache <https://github.com/ibireme/YYCache>
  4. //
  5. // Created by ibireme on 15/2/13.
  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. #if __has_include(<YYCache/YYCache.h>)
  13. FOUNDATION_EXPORT double YYCacheVersionNumber;
  14. FOUNDATION_EXPORT const unsigned char YYCacheVersionString[];
  15. #import <YYCache/YYMemoryCache.h>
  16. #import <YYCache/YYDiskCache.h>
  17. #import <YYCache/YYKVStorage.h>
  18. #elif __has_include(<YYWebImage/YYCache.h>)
  19. #import <YYWebImage/YYMemoryCache.h>
  20. #import <YYWebImage/YYDiskCache.h>
  21. #import <YYWebImage/YYKVStorage.h>
  22. #else
  23. #import "YYMemoryCache.h"
  24. #import "YYDiskCache.h"
  25. #import "YYKVStorage.h"
  26. #endif
  27. NS_ASSUME_NONNULL_BEGIN
  28. /**
  29. `YYCache` is a thread safe key-value cache.
  30. It use `YYMemoryCache` to store objects in a small and fast memory cache,
  31. and use `YYDiskCache` to persisting objects to a large and slow disk cache.
  32. See `YYMemoryCache` and `YYDiskCache` for more information.
  33. */
  34. @interface YYCache : NSObject
  35. /** The name of the cache, readonly. */
  36. @property (copy, readonly) NSString *name;
  37. /** The underlying memory cache. see `YYMemoryCache` for more information.*/
  38. @property (strong, readonly) YYMemoryCache *memoryCache;
  39. /** The underlying disk cache. see `YYDiskCache` for more information.*/
  40. @property (strong, readonly) YYDiskCache *diskCache;
  41. /**
  42. Create a new instance with the specified name.
  43. Multiple instances with the same name will make the cache unstable.
  44. @param name The name of the cache. It will create a dictionary with the name in
  45. the app's caches dictionary for disk cache. Once initialized you should not
  46. read and write to this directory.
  47. @result A new cache object, or nil if an error occurs.
  48. */
  49. - (nullable instancetype)initWithName:(NSString *)name;
  50. /**
  51. Create a new instance with the specified path.
  52. Multiple instances with the same name will make the cache unstable.
  53. @param path Full path of a directory in which the cache will write data.
  54. Once initialized you should not read and write to this directory.
  55. @result A new cache object, or nil if an error occurs.
  56. */
  57. - (nullable instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
  58. /**
  59. Convenience Initializers
  60. Create a new instance with the specified name.
  61. Multiple instances with the same name will make the cache unstable.
  62. @param name The name of the cache. It will create a dictionary with the name in
  63. the app's caches dictionary for disk cache. Once initialized you should not
  64. read and write to this directory.
  65. @result A new cache object, or nil if an error occurs.
  66. */
  67. + (nullable instancetype)cacheWithName:(NSString *)name;
  68. /**
  69. Convenience Initializers
  70. Create a new instance with the specified path.
  71. Multiple instances with the same name will make the cache unstable.
  72. @param path Full path of a directory in which the cache will write data.
  73. Once initialized you should not read and write to this directory.
  74. @result A new cache object, or nil if an error occurs.
  75. */
  76. + (nullable instancetype)cacheWithPath:(NSString *)path;
  77. - (instancetype)init UNAVAILABLE_ATTRIBUTE;
  78. + (instancetype)new UNAVAILABLE_ATTRIBUTE;
  79. #pragma mark - Access Methods
  80. ///=============================================================================
  81. /// @name Access Methods
  82. ///=============================================================================
  83. /**
  84. Returns a boolean value that indicates whether a given key is in cache.
  85. This method may blocks the calling thread until file read finished.
  86. @param key A string identifying the value. If nil, just return NO.
  87. @return Whether the key is in cache.
  88. */
  89. - (BOOL)containsObjectForKey:(NSString *)key;
  90. /**
  91. Returns a boolean value with the block that indicates whether a given key is in cache.
  92. This method returns immediately and invoke the passed block in background queue
  93. when the operation finished.
  94. @param key A string identifying the value. If nil, just return NO.
  95. @param block A block which will be invoked in background queue when finished.
  96. */
  97. - (void)containsObjectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key, BOOL contains))block;
  98. /**
  99. Returns the value associated with a given key.
  100. This method may blocks the calling thread until file read finished.
  101. @param key A string identifying the value. If nil, just return nil.
  102. @return The value associated with key, or nil if no value is associated with key.
  103. */
  104. - (nullable id<NSCoding>)objectForKey:(NSString *)key;
  105. /**
  106. Returns the value associated with a given key.
  107. This method returns immediately and invoke the passed block in background queue
  108. when the operation finished.
  109. @param key A string identifying the value. If nil, just return nil.
  110. @param block A block which will be invoked in background queue when finished.
  111. */
  112. - (void)objectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key, id<NSCoding> object))block;
  113. /**
  114. Sets the value of the specified key in the cache.
  115. This method may blocks the calling thread until file write finished.
  116. @param object The object to be stored in the cache. If nil, it calls `removeObjectForKey:`.
  117. @param key The key with which to associate the value. If nil, this method has no effect.
  118. */
  119. - (void)setObject:(nullable id<NSCoding>)object forKey:(NSString *)key;
  120. /**
  121. Sets the value of the specified key in the cache.
  122. This method returns immediately and invoke the passed block in background queue
  123. when the operation finished.
  124. @param object The object to be stored in the cache. If nil, it calls `removeObjectForKey:`.
  125. @param block A block which will be invoked in background queue when finished.
  126. */
  127. - (void)setObject:(nullable id<NSCoding>)object forKey:(NSString *)key withBlock:(nullable void(^)(void))block;
  128. /**
  129. Removes the value of the specified key in the cache.
  130. This method may blocks the calling thread until file delete finished.
  131. @param key The key identifying the value to be removed. If nil, this method has no effect.
  132. */
  133. - (void)removeObjectForKey:(NSString *)key;
  134. /**
  135. Removes the value of the specified key in the cache.
  136. This method returns immediately and invoke the passed block in background queue
  137. when the operation finished.
  138. @param key The key identifying the value to be removed. If nil, this method has no effect.
  139. @param block A block which will be invoked in background queue when finished.
  140. */
  141. - (void)removeObjectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key))block;
  142. /**
  143. Empties the cache.
  144. This method may blocks the calling thread until file delete finished.
  145. */
  146. - (void)removeAllObjects;
  147. /**
  148. Empties the cache.
  149. This method returns immediately and invoke the passed block in background queue
  150. when the operation finished.
  151. @param block A block which will be invoked in background queue when finished.
  152. */
  153. - (void)removeAllObjectsWithBlock:(void(^)(void))block;
  154. /**
  155. Empties the cache with block.
  156. This method returns immediately and executes the clear operation with block in background.
  157. @warning You should not send message to this instance in these blocks.
  158. @param progress This block will be invoked during removing, pass nil to ignore.
  159. @param end This block will be invoked at the end, pass nil to ignore.
  160. */
  161. - (void)removeAllObjectsWithProgressBlock:(nullable void(^)(int removedCount, int totalCount))progress
  162. endBlock:(nullable void(^)(BOOL error))end;
  163. @end
  164. NS_ASSUME_NONNULL_END