BU_SDDiskCache.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "BU_SDWebImageCompat.h"
  9. @class BU_SDImageCacheConfig;
  10. /**
  11. A protocol to allow custom disk cache used in SDImageCache.
  12. */
  13. @protocol BU_SDDiskCache <NSObject>
  14. // All of these method are called from the same global queue to avoid blocking on main queue and thread-safe problem. But it's also recommend to ensure thread-safe yourself using lock or other ways.
  15. @required
  16. /**
  17. Create a new disk cache based on the specified path. You can check `maxDiskSize` and `maxDiskAge` used for disk cache.
  18. @param cachePath Full path of a directory in which the cache will write data.
  19. Once initialized you should not read and write to this directory.
  20. @param config The cache config to be used to create the cache.
  21. @return A new cache object, or nil if an error occurs.
  22. */
  23. - (nullable instancetype)initWithCachePath:(nonnull NSString *)cachePath config:(nonnull BU_SDImageCacheConfig *)config;
  24. /**
  25. Returns a boolean value that indicates whether a given key is in cache.
  26. This method may blocks the calling thread until file read finished.
  27. @param key A string identifying the data. If nil, just return NO.
  28. @return Whether the key is in cache.
  29. */
  30. - (BOOL)containsDataForKey:(nonnull NSString *)key;
  31. /**
  32. Returns the data associated with a given key.
  33. This method may blocks the calling thread until file read finished.
  34. @param key A string identifying the data. If nil, just return nil.
  35. @return The value associated with key, or nil if no value is associated with key.
  36. */
  37. - (nullable NSData *)dataForKey:(nonnull NSString *)key;
  38. /**
  39. Sets the value of the specified key in the cache.
  40. This method may blocks the calling thread until file write finished.
  41. @param data The data to be stored in the cache.
  42. @param key The key with which to associate the value. If nil, this method has no effect.
  43. */
  44. - (void)setData:(nullable NSData *)data forKey:(nonnull NSString *)key;
  45. /**
  46. Removes the value of the specified key in the cache.
  47. This method may blocks the calling thread until file delete finished.
  48. @param key The key identifying the value to be removed. If nil, this method has no effect.
  49. */
  50. - (void)removeDataForKey:(nonnull NSString *)key;
  51. /**
  52. Empties the cache.
  53. This method may blocks the calling thread until file delete finished.
  54. */
  55. - (void)removeAllData;
  56. /**
  57. Removes the expired data from the cache. You can choose the data to remove base on `ageLimit`, `countLimit` and `sizeLimit` options.
  58. */
  59. - (void)removeExpiredData;
  60. /**
  61. The cache path for key
  62. @param key A string identifying the value
  63. @return The cache path for key. Or nil if the key can not associate to a path
  64. */
  65. - (nullable NSString *)cachePathForKey:(nonnull NSString *)key;
  66. /**
  67. Returns the number of data in this cache.
  68. This method may blocks the calling thread until file read finished.
  69. @return The total data count.
  70. */
  71. - (NSUInteger)totalCount;
  72. /**
  73. Returns the total size (in bytes) of data in this cache.
  74. This method may blocks the calling thread until file read finished.
  75. @return The total data size in bytes.
  76. */
  77. - (NSUInteger)totalSize;
  78. @end
  79. /**
  80. The built-in disk cache.
  81. */
  82. @interface BU_SDDiskCache : NSObject <BU_SDDiskCache>
  83. /**
  84. Cache Config object - storing all kind of settings.
  85. */
  86. @property (nonatomic, strong, readonly, nonnull) BU_SDImageCacheConfig *config;
  87. - (nonnull instancetype)init NS_UNAVAILABLE;
  88. @end