酷店

SDMemoryCache.h 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "SDWebImageCompat.h"
  9. @class SDImageCacheConfig;
  10. /**
  11. A protocol to allow custom memory cache used in SDImageCache.
  12. */
  13. @protocol SDMemoryCache <NSObject>
  14. @required
  15. /**
  16. Create a new memory cache instance with the specify cache config. You can check `maxMemoryCost` and `maxMemoryCount` used for memory cache.
  17. @param config The cache config to be used to create the cache.
  18. @return The new memory cache instance.
  19. */
  20. - (nonnull instancetype)initWithConfig:(nonnull SDImageCacheConfig *)config;
  21. /**
  22. Returns the value associated with a given key.
  23. @param key An object identifying the value. If nil, just return nil.
  24. @return The value associated with key, or nil if no value is associated with key.
  25. */
  26. - (nullable id)objectForKey:(nonnull id)key;
  27. /**
  28. Sets the value of the specified key in the cache (0 cost).
  29. @param object The object to be stored in the cache. If nil, it calls `removeObjectForKey:`.
  30. @param key The key with which to associate the value. If nil, this method has no effect.
  31. @discussion Unlike an NSMutableDictionary object, a cache does not copy the key
  32. objects that are put into it.
  33. */
  34. - (void)setObject:(nullable id)object forKey:(nonnull id)key;
  35. /**
  36. Sets the value of the specified key in the cache, and associates the key-value
  37. pair with the specified cost.
  38. @param object The object to store in the cache. If nil, it calls `removeObjectForKey`.
  39. @param key The key with which to associate the value. If nil, this method has no effect.
  40. @param cost The cost with which to associate the key-value pair.
  41. @discussion Unlike an NSMutableDictionary object, a cache does not copy the key
  42. objects that are put into it.
  43. */
  44. - (void)setObject:(nullable id)object forKey:(nonnull id)key cost:(NSUInteger)cost;
  45. /**
  46. Removes the value of the specified key in the cache.
  47. @param key The key identifying the value to be removed. If nil, this method has no effect.
  48. */
  49. - (void)removeObjectForKey:(nonnull id)key;
  50. /**
  51. Empties the cache immediately.
  52. */
  53. - (void)removeAllObjects;
  54. @end
  55. /**
  56. A memory cache which auto purge the cache on memory warning and support weak cache.
  57. */
  58. @interface SDMemoryCache <KeyType, ObjectType> : NSCache <KeyType, ObjectType> <SDMemoryCache>
  59. @property (nonatomic, strong, nonnull, readonly) SDImageCacheConfig *config;
  60. @end