SDDiskCache

@protocol SDDiskCache <NSObject>

A protocol to allow custom disk cache used in SDImageCache.

  • Create a new disk cache based on the specified path. You can check maxDiskSize and maxDiskAge used for disk cache.

    Declaration

    Objective-C

    - (nullable instancetype)initWithCachePath:(nonnull NSString *)cachePath
                                        config:(nonnull SDImageCacheConfig *)config;

    Swift

    init?(cachePath: String, config: SDImageCacheConfig)

    Parameters

    cachePath

    Full path of a directory in which the cache will write data. Once initialized you should not read and write to this directory.

    config

    The cache config to be used to create the cache.

    Return Value

    A new cache object, or nil if an error occurs.

  • Returns a boolean value that indicates whether a given key is in cache. This method may blocks the calling thread until file read finished.

    Declaration

    Objective-C

    - (BOOL)containsDataForKey:(nonnull NSString *)key;

    Swift

    func containsData(forKey key: String) -> Bool

    Parameters

    key

    A string identifying the data. If nil, just return NO.

    Return Value

    Whether the key is in cache.

  • Returns the data associated with a given key. This method may blocks the calling thread until file read finished.

    Declaration

    Objective-C

    - (nullable NSData *)dataForKey:(nonnull NSString *)key;

    Swift

    func data(forKey key: String) -> Data?

    Parameters

    key

    A string identifying the data. 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. This method may blocks the calling thread until file write finished.

    Declaration

    Objective-C

    - (void)setData:(nullable NSData *)data forKey:(nonnull NSString *)key;

    Swift

    func setData(_ data: Data?, forKey key: String)

    Parameters

    data

    The data to be stored in the cache.

    key

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

  • Returns the extended data associated with a given key. This method may blocks the calling thread until file read finished.

    Declaration

    Objective-C

    - (nullable NSData *)extendedDataForKey:(nonnull NSString *)key;

    Swift

    func extendedData(forKey key: String) -> Data?

    Parameters

    key

    A string identifying the data. If nil, just return nil.

    Return Value

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

  • Set extended data with a given key.

    @discussion You can set any extended data to exist cache key. Without override the exist disk file data. on UNIX, the common way for this is to use the Extended file attributes (xattr)

    Declaration

    Objective-C

    - (void)setExtendedData:(nullable NSData *)extendedData
                     forKey:(nonnull NSString *)key;

    Swift

    func setExtendedData(_ extendedData: Data?, forKey key: String)

    Parameters

    extendedData

    The extended data (pass nil to remove).

    key

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

  • Removes the value of the specified key in the cache. This method may blocks the calling thread until file delete finished.

    Declaration

    Objective-C

    - (void)removeDataForKey:(nonnull NSString *)key;

    Swift

    func removeData(forKey key: String)

    Parameters

    key

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

  • Empties the cache. This method may blocks the calling thread until file delete finished.

    Declaration

    Objective-C

    - (void)removeAllData;

    Swift

    func removeAllData()
  • Removes the expired data from the cache. You can choose the data to remove base on ageLimit, countLimit and sizeLimit options.

    Declaration

    Objective-C

    - (void)removeExpiredData;

    Swift

    func removeExpiredData()
  • The cache path for key

    Declaration

    Objective-C

    - (nullable NSString *)cachePathForKey:(nonnull NSString *)key;

    Swift

    func cachePath(forKey key: String) -> String?

    Parameters

    key

    A string identifying the value

    Return Value

    The cache path for key. Or nil if the key can not associate to a path

  • Returns the number of data in this cache. This method may blocks the calling thread until file read finished.

    Declaration

    Objective-C

    - (NSUInteger)totalCount;

    Swift

    func totalCount() -> UInt

    Return Value

    The total data count.

  • Returns the total size (in bytes) of data in this cache. This method may blocks the calling thread until file read finished.

    Declaration

    Objective-C

    - (NSUInteger)totalSize;

    Swift

    func totalSize() -> UInt

    Return Value

    The total data size in bytes.