SDMemoryCache

@protocol SDMemoryCache <NSObject>

A protocol to allow custom memory cache used in SDImageCache.

  • Create a new memory cache instance with the specify cache config. You can check maxMemoryCost and maxMemoryCount used for memory cache.

    Declaration

    Objective-C

    - (nonnull instancetype)initWithConfig:(nonnull SDImageCacheConfig *)config;

    Swift

    init(config: SDImageCacheConfig)

    Parameters

    config

    The cache config to be used to create the cache.

    Return Value

    The new memory cache instance.

  • Returns the value associated with a given key.

    Declaration

    Objective-C

    - (nullable id)objectForKey:(nonnull id)key;

    Swift

    func object(forKey key: Any) -> Any?

    Parameters

    key

    An object identifying the value. If nil, just return nil.

    Return Value

    The value associated with key, or nil if no value is associated with key.

  • Sets the value of the specified key in the cache (0 cost).

    Declaration

    Objective-C

    - (void)setObject:(nullable id)object forKey:(nonnull id)key;

    Swift

    func setObject(_ object: Any?, forKey key: Any)

    Parameters

    object

    The object to be stored in the cache. If nil, it calls removeObjectForKey:.

    key

    The key with which to associate the value. If nil, this method has no effect. @discussion Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.

  • Sets the value of the specified key in the cache, and associates the key-value pair with the specified cost.

    Declaration

    Objective-C

    - (void)setObject:(nullable id)object
               forKey:(nonnull id)key
                 cost:(NSUInteger)cost;

    Swift

    func setObject(_ object: Any?, forKey key: Any, cost: UInt)

    Parameters

    object

    The object to store in the cache. If nil, it calls removeObjectForKey.

    key

    The key with which to associate the value. If nil, this method has no effect.

    cost

    The cost with which to associate the key-value pair. @discussion Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.

  • Removes the value of the specified key in the cache.

    Declaration

    Objective-C

    - (void)removeObjectForKey:(nonnull id)key;

    Swift

    func removeObject(forKey key: Any)

    Parameters

    key

    The key identifying the value to be removed. If nil, this method has no effect.

  • Empties the cache immediately.

    Declaration

    Objective-C

    - (void)removeAllObjects;

    Swift

    func removeAllObjects()