Kingfisher Documentation

Class Memory​Storage.​Backend

public class Backend<T: CacheCostCalculable>  

Represents a storage which stores a certain type of value in memory. It provides fast access, but limited storing size. The stored value type needs to conform to CacheCostCalculable, and its cacheCost will be used to determine the cost of size for the cache item.

You can config a MemoryStorage.Backend in its initializer by passing a MemoryStorage.Config value. or modifying the config property after it being created. The backend of MemoryStorage has upper limitation on cost size in memory and item count. All items in the storage has an expiration date. When retrieved, if the target item is already expired, it will be recognized as it does not exist in the storage. The MemoryStorage also contains a scheduled self clean task, to evict expired items from memory.

Member Of


Represents a set of conception related to storage which stores a certain type of value in memory. This is a namespace for the memory storage types. A Backend with a certain Config will be used to describe the storage. See these composed types for more information.



public init(config: Config)  

Creates a MemoryStorage with a given config.


config Config

The config used to create the storage. It determines the max size limitation, default expiration setting and more.



public var config: Config  

The config used in this storage. It is a value you can set and use to config the storage in air.



public func removeExpired()  

Removes the expired values from the storage.


public func store(
            value: T,
            forKey key: String,
            expiration: StorageExpiration? = nil) 

Stores a value to the storage under the specified key and expiration policy.


value T

The value to be stored.

key String

The key to which the value will be stored.

expiration Storage​Expiration?

The expiration policy used by this store action.


No error will


public func value(forKey key: String, extendingExpiration: ExpirationExtending = .cacheTime) -> T?  

Gets a value from the storage.


key String

The cache key of value.

extending​Expiration Expiration​Extending

The expiration policy used by this getting action.


The value under key if it is valid and found in the storage. Otherwise, nil.


public func isCached(forKey key: String) -> Bool  

Whether there is valid cached data under a given key.


key String

The cache key of value.


If there is valid data under the key, true. Otherwise, false.


public func remove(forKey key: String)  

Removes a value from a specified key.


key String

The cache key of value.


public func removeAll()  

Removes all values in this storage.