MemoryCache

open class MemoryCache<Key, Value> : Caching where Key : Hashable

MemoryCache

MemoryCache stores ValueType*s in memory associated with *KeyType. Currently the Least Recently Used (LRU) cache replacement policy is implemented. Generic parameters:

// create a memory cache with the given cache limits
let cache = MemoryCache<String, Data>(maximumNumberOfEntries: 2, maximumCost: 0)

let dataValue: Data = ...
let key: String = ...
let cost: Int = ...

// save the value in the cache for the key associated with the cost.
cache.set(value: dataValue, forKey: key, withCost: cost)

// retrieves the entry for the given key
let entry = cache.entry(forKey: key)

//retrieves the value for the given key
let value = cache.value(forKey: key)

//removes all items from the cache
cache.removeAllValues()
  • the maximum number of entries the cache will store. 0 means the cache doesn’t have specific limit

    Declaration

    Swift

    public let maximumNumberOfEntries: Int
  • the maximum cost the cache can store. The maximum cost is calculated by comulating the cost of each item. 0 means the cache doesn’t have cost limit

    Declaration

    Swift

    public let maximumCost: Double
  • Create an instance with the given cache limits. Zero or more parameters can be specified. It is possible to specify number and cost limit together. In this case both limit will be used. It is possible not to specify either of the limits in this case no limits will be used.

    Declaration

    Swift

    required public init(maximumNumberOfEntries: Int = 0, maximumCost: Double = 0, clearCacheOnMemoryWarning clearOnMemWarning: Bool = true)

    Parameters

    maximumNumberOfEntries

    the maximum number of items the cache will store. Exceeding the limit causes an item will be removed from the cache. The item is determined by the Cache Replacement Policy (currently LRU). Pass 0 to disable the limit of items.

    maximumCost

    the maximum cost the cache can store. The maximum cost is calculated by cumulating the cost of each item. When the cost exceeds the limit one or more item will be removed from the cache. The items are specified by the Cache Replacement Policy currently LRU). Pass 0 to disable the limit.

    clearOnMemWarning

    Default value is true. The cache will be cleared when the next MemoryWarning happens.

  • aggregated value of the cost of the entries in the cache.

    Declaration

    Swift

    open var costOfEntries: Double { get }
  • the number of entries currently stored in the cache

    Declaration

    Swift

    open var numberOfEntries: Int { get }
  • Saves the value in the cache for the key associated with the cost. In some cases it is possible that the item is not stored (for example the cost is higher than the maximum posbbile cost), in this case no error will happen

    Declaration

    Swift

    open func set(value: Value, forKey key: Key, withCost cost: Double = 0)
  • retrieves the entry for the given key or nil in case the item is not in the cache

    Declaration

    Swift

    open func entry(forKey key: Key) -> CacheEntry<Value>?
  • retrieves the value for the given key or nil in case the item is not in the cache

    Declaration

    Swift

    open func value(forKey key: Key) -> Value?
  • removes all items from the cache

    Declaration

    Swift

    open func removeAllValues()
  • removes the item from the case. If there is no item with the key nothing happens

    Declaration

    Swift

    open func removeValue(forKey key: Key)
  • returns a secuence of the keys currently exists in the cache

    Declaration

    Swift

    open func keys() -> AnySequence<KeyType>