BU_SDImageCache.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import <Foundation/Foundation.h>
  9. #import "BU_SDWebImageCompat.h"
  10. #import "BU_SDWebImageDefine.h"
  11. #import "BU_SDImageCacheConfig.h"
  12. #import "BU_SDImageCacheDefine.h"
  13. #import "BU_SDMemoryCache.h"
  14. #import "BU_SDDiskCache.h"
  15. /// Image Cache Options
  16. typedef NS_OPTIONS(NSUInteger, BU_SDImageCacheOptions) {
  17. /**
  18. * By default, we do not query image data when the image is already cached in memory. This mask can force to query image data at the same time. However, this query is asynchronously unless you specify `SDImageCacheQueryMemoryDataSync`
  19. */
  20. BU_SDImageCacheQueryMemoryData = 1 << 0,
  21. /**
  22. * By default, when you only specify `SDImageCacheQueryMemoryData`, we query the memory image data asynchronously. Combined this mask as well to query the memory image data synchronously.
  23. */
  24. BU_SDImageCacheQueryMemoryDataSync = 1 << 1,
  25. /**
  26. * By default, when the memory cache miss, we query the disk cache asynchronously. This mask can force to query disk cache (when memory cache miss) synchronously.
  27. @note These 3 query options can be combined together. For the full list about these masks combination, see wiki page.
  28. */
  29. BU_SDImageCacheQueryDiskDataSync = 1 << 2,
  30. /**
  31. * By default, images are decoded respecting their original size. On iOS, this flag will scale down the
  32. * images to a size compatible with the constrained memory of devices.
  33. */
  34. BU_SDImageCacheScaleDownLargeImages = 1 << 3,
  35. /**
  36. * By default, we will decode the image in the background during cache query and download from the network. This can help to improve performance because when rendering image on the screen, it need to be firstly decoded. But this happen on the main queue by Core Animation.
  37. * However, this process may increase the memory usage as well. If you are experiencing a issue due to excessive memory consumption, This flag can prevent decode the image.
  38. */
  39. BU_SDImageCacheAvoidDecodeImage = 1 << 4,
  40. /**
  41. * By default, we decode the animated image. This flag can force decode the first frame only and produece the static image.
  42. */
  43. BU_SDImageCacheDecodeFirstFrameOnly = 1 << 5,
  44. /**
  45. * By default, for `BU_SDAnimatedImage`, we decode the animated image frame during rendering to reduce memory usage. This flag actually trigger `preloadAllAnimatedImageFrames = YES` after image load from disk cache
  46. */
  47. BU_SDImageCachePreloadAllFrames = 1 << 6,
  48. /**
  49. * By default, when you use `BU_SDWebImageContextAnimatedImageClass` context option (like using `SDAnimatedImageView` which designed to use `BU_SDAnimatedImage`), we may still use `UIImage` when the memory cache hit, or image decoder is not available, to behave as a fallback solution.
  50. * Using this option, can ensure we always produce image with your provided class. If failed, a error with code `SDWebImageErrorBadImageData` will been used.
  51. * Note this options is not compatible with `SDImageCacheDecodeFirstFrameOnly`, which always produce a UIImage/NSImage.
  52. */
  53. BU_SDImageCacheMatchAnimatedImageClass = 1 << 7,
  54. };
  55. /**
  56. * SDImageCache maintains a memory cache and a disk cache. Disk cache write operations are performed
  57. * asynchronous so it doesn’t add unnecessary latency to the UI.
  58. */
  59. @interface BU_SDImageCache : NSObject
  60. #pragma mark - Properties
  61. /**
  62. * Cache Config object - storing all kind of settings.
  63. * The property is copy so change of currrent config will not accidentally affect other cache's config.
  64. */
  65. @property (nonatomic, copy, nonnull, readonly) BU_SDImageCacheConfig *config;
  66. /**
  67. * The memory cache implementation object used for current image cache.
  68. * By default we use `SDMemoryCache` class, you can also use this to call your own implementation class method.
  69. * @note To customize this class, check `SDImageCacheConfig.memoryCacheClass` property.
  70. */
  71. @property (nonatomic, strong, readonly, nonnull) id<BU_SDMemoryCache> memoryCache;
  72. /**
  73. * The disk cache implementation object used for current image cache.
  74. * By default we use `SDMemoryCache` class, you can also use this to call your own implementation class method.
  75. * @note To customize this class, check `SDImageCacheConfig.diskCacheClass` property.
  76. * @warning When calling method about read/write in disk cache, be sure to either make your disk cache implementation IO-safe or using the same access queue to avoid issues.
  77. */
  78. @property (nonatomic, strong, readonly, nonnull) id<BU_SDDiskCache> diskCache;
  79. /**
  80. * The disk cache's root path
  81. */
  82. @property (nonatomic, copy, nonnull, readonly) NSString *diskCachePath;
  83. /**
  84. * The additional disk cache path to check if the query from disk cache not exist;
  85. * The `key` param is the image cache key. The returned file path will be used to load the disk cache. If return nil, ignore it.
  86. * Useful if you want to bundle pre-loaded images with your app
  87. */
  88. @property (nonatomic, copy, nullable) SDImageCacheAdditionalCachePathBlock additionalCachePathBlock;
  89. #pragma mark - Singleton and initialization
  90. /**
  91. * Returns global shared cache instance
  92. */
  93. @property (nonatomic, class, readonly, nonnull) BU_SDImageCache *sharedImageCache;
  94. /**
  95. * Init a new cache store with a specific namespace
  96. *
  97. * @param ns The namespace to use for this cache store
  98. */
  99. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns;
  100. /**
  101. * Init a new cache store with a specific namespace and directory.
  102. * If you don't provide the disk cache directory, we will use the User Cache directory with prefix (~/Library/Caches/com.hackemist.SDImageCache/).
  103. *
  104. * @param ns The namespace to use for this cache store
  105. * @param directory Directory to cache disk images in
  106. */
  107. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
  108. diskCacheDirectory:(nullable NSString *)directory;
  109. /**
  110. * Init a new cache store with a specific namespace, directory and file manager
  111. * The final disk cache directory should looks like ($directory/$namespace). And the default config of shared cache, should result in (~/Library/Caches/com.hackemist.SDImageCache/default/)
  112. *
  113. * @param ns The namespace to use for this cache store
  114. * @param directory Directory to cache disk images in
  115. * @param config The cache config to be used to create the cache. You can provide custom memory cache or disk cache class in the cache config
  116. */
  117. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
  118. diskCacheDirectory:(nullable NSString *)directory
  119. config:(nullable BU_SDImageCacheConfig *)config NS_DESIGNATED_INITIALIZER;
  120. #pragma mark - Cache paths
  121. /**
  122. Get the cache path for a certain key
  123. @param key The unique image cache key
  124. @return The cache path. You can check `lastPathComponent` to grab the file name.
  125. */
  126. - (nullable NSString *)cachePathForKey:(nullable NSString *)key;
  127. #pragma mark - Store Ops
  128. /**
  129. * Asynchronously store an image into memory and disk cache at the given key.
  130. *
  131. * @param image The image to store
  132. * @param key The unique image cache key, usually it's image absolute URL
  133. * @param completionBlock A block executed after the operation is finished
  134. */
  135. //- (void)storeImage:(nullable UIImage *)image
  136. // forKey:(nullable NSString *)key
  137. // completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  138. /**
  139. * Asynchronously store an image into memory and disk cache at the given key.
  140. *
  141. * @param image The image to store
  142. * @param key The unique image cache key, usually it's image absolute URL
  143. * @param toDisk Store the image to disk cache if YES. If NO, the completion block is called synchronously
  144. * @param completionBlock A block executed after the operation is finished
  145. */
  146. //- (void)storeImage:(nullable UIImage *)image
  147. // forKey:(nullable NSString *)key
  148. // toDisk:(BOOL)toDisk
  149. // completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  150. /**
  151. * Asynchronously store an image into memory and disk cache at the given key.
  152. *
  153. * @param image The image to store
  154. * @param imageData The image data as returned by the server, this representation will be used for disk storage
  155. * instead of converting the given image object into a storable/compressed image format in order
  156. * to save quality and CPU
  157. * @param key The unique image cache key, usually it's image absolute URL
  158. * @param toDisk Store the image to disk cache if YES. If NO, the completion block is called synchronously
  159. * @param completionBlock A block executed after the operation is finished
  160. */
  161. - (void)storeImage:(nullable UIImage *)image
  162. imageData:(nullable NSData *)imageData
  163. forKey:(nullable NSString *)key
  164. toDisk:(BOOL)toDisk
  165. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  166. /**
  167. * Synchronously store image into memory cache at the given key.
  168. *
  169. * @param image The image to store
  170. * @param key The unique image cache key, usually it's image absolute URL
  171. */
  172. //- (void)storeImageToMemory:(nullable UIImage*)image
  173. // forKey:(nullable NSString *)key;
  174. /**
  175. * Synchronously store image data into disk cache at the given key.
  176. *
  177. * @param imageData The image data to store
  178. * @param key The unique image cache key, usually it's image absolute URL
  179. */
  180. //- (void)storeImageDataToDisk:(nullable NSData *)imageData
  181. // forKey:(nullable NSString *)key;
  182. #pragma mark - Contains and Check Ops
  183. /**
  184. * Asynchronously check if image exists in disk cache already (does not load the image)
  185. *
  186. * @param key the key describing the url
  187. * @param completionBlock the block to be executed when the check is done.
  188. * @note the completion block will be always executed on the main queue
  189. */
  190. - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDImageCacheCheckCompletionBlock)completionBlock;
  191. /**
  192. * Synchronously check if image data exists in disk cache already (does not load the image)
  193. *
  194. * @param key the key describing the url
  195. */
  196. //- (BOOL)diskImageDataExistsWithKey:(nullable NSString *)key;
  197. #pragma mark - Query and Retrieve Ops
  198. /**
  199. * Asynchronously queries the cache with operation and call the completion when done.
  200. * Query the image data for the given key synchronously.
  201. *
  202. * @param key The unique key used to store the wanted image
  203. * @return The image data for the given key, or nil if not found.
  204. */
  205. - (nullable NSData *)diskImageDataForKey:(nullable NSString *)key;
  206. /**
  207. * Operation that queries the cache asynchronously and call the completion when done.
  208. *
  209. * @param key The unique key used to store the wanted image
  210. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  211. *
  212. * @return a NSOperation instance containing the cache op
  213. */
  214. //- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable BU_SDImageCacheQueryCompletionBlock)doneBlock;
  215. /**
  216. * Asynchronously queries the cache with operation and call the completion when done.
  217. *
  218. * @param key The unique key used to store the wanted image
  219. * @param options A mask to specify options to use for this cache query
  220. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  221. *
  222. * @return a NSOperation instance containing the cache op
  223. */
  224. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(BU_SDImageCacheOptions)options done:(nullable BU_SDImageCacheQueryCompletionBlock)doneBlock;
  225. /**
  226. * Asynchronously queries the cache with operation and call the completion when done.
  227. *
  228. * @param key The unique key used to store the wanted image
  229. * @param options A mask to specify options to use for this cache query
  230. * @param context A context contains different options to perform specify changes or processes, see `BU_SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
  231. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  232. *
  233. * @return a NSOperation instance containing the cache op
  234. */
  235. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(BU_SDImageCacheOptions)options context:(nullable SDWebImageContext *)context done:(nullable BU_SDImageCacheQueryCompletionBlock)doneBlock;
  236. /**
  237. * Synchronously query the memory cache.
  238. *
  239. * @param key The unique key used to store the image
  240. * @return The image for the given key, or nil if not found.
  241. */
  242. - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key;
  243. /**
  244. * Synchronously query the disk cache.
  245. *
  246. * @param key The unique key used to store the image
  247. * @return The image for the given key, or nil if not found.
  248. */
  249. - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key;
  250. /**
  251. * Synchronously query the cache (memory and or disk) after checking the memory cache.
  252. *
  253. * @param key The unique key used to store the image
  254. * @return The image for the given key, or nil if not found.
  255. */
  256. - (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key;
  257. #pragma mark - Remove Ops
  258. /**
  259. * Asynchronously remove the image from memory and disk cache
  260. *
  261. * @param key The unique image cache key
  262. * @param completion A block that should be executed after the image has been removed (optional)
  263. */
  264. //- (void)removeImageForKey:(nullable NSString *)key withCompletion:(nullable SDWebImageNoParamsBlock)completion;
  265. /**
  266. * Asynchronously remove the image from memory and optionally disk cache
  267. *
  268. * @param key The unique image cache key
  269. * @param fromDisk Also remove cache entry from disk if YES. If NO, the completion block is called synchronously
  270. * @param completion A block that should be executed after the image has been removed (optional)
  271. */
  272. - (void)removeImageForKey:(nullable NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(nullable SDWebImageNoParamsBlock)completion;
  273. /**
  274. Synchronously remove the image from memory cache.
  275. @param key The unique image cache key
  276. */
  277. //- (void)removeImageFromMemoryForKey:(nullable NSString *)key;
  278. /**
  279. Synchronously remove the image from disk cache.
  280. @param key The unique image cache key
  281. */
  282. //- (void)removeImageFromDiskForKey:(nullable NSString *)key;
  283. #pragma mark - Cache clean Ops
  284. /**
  285. * Synchronously Clear all memory cached images
  286. */
  287. - (void)clearMemory;
  288. /**
  289. * Asynchronously clear all disk cached images. Non-blocking method - returns immediately.
  290. * @param completion A block that should be executed after cache expiration completes (optional)
  291. */
  292. - (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion;
  293. /**
  294. * Asynchronously remove all expired cached image from disk. Non-blocking method - returns immediately.
  295. * @param completionBlock A block that should be executed after cache expiration completes (optional)
  296. */
  297. - (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock;
  298. #pragma mark - Cache Info
  299. /**
  300. * Get the total bytes size of images in the disk cache
  301. */
  302. - (NSUInteger)totalDiskSize;
  303. /**
  304. * Get the number of images in the disk cache
  305. */
  306. - (NSUInteger)totalDiskCount;
  307. /**
  308. * Asynchronously calculate the disk cache's size.
  309. */
  310. //- (void)calculateSizeWithCompletionBlock:(nullable SDImageCacheCalculateSizeBlock)completionBlock;
  311. @end
  312. /**
  313. * SDImageCache is the built-in image cache implementation for web image manager. It adopts `SDImageCache` protocol to provide the function for web image manager to use for image loading process.
  314. */
  315. @interface BU_SDImageCache (BU_SDImageCache) <BU_SDImageCache>
  316. @end