Caching

public protocol Caching

Caching

Specifies the basic operations a cache implementation should support. In general the Cache is a key/value store. The stored items can be automatically removed from the cache when the cache reaches the maximum storage capacity. For each item in the cache a cost value can be specified. Cost is an arbitrary value (can be the size of the data for example). The behavior how the cost is handled implementation dependent. Caching is a generic protocol meaning the KeyType and ValueType can be arbitrariry selected.

  • the type of the key by which the caller can retrieve values from the cache

    Declaration

    Swift

    associatedtype KeyType
  • the type of the value stored in the cache

    Declaration

    Swift

    associatedtype ValueType
  • aggregated value of the cost of the entries in the cache. The concrete behavior is implementation dependent but in general it is the cumulated value of the costs

    Declaration

    Swift

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

    Declaration

    Swift

    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

    func set(value: ValueType, forKey: KeyType, withCost: Double)
  • retrieves the value for the given key or nil in case the item is not in the cache

    Declaration

    Swift

    func value(forKey: KeyType) -> ValueType?
  • retrieves the entry for the given key or nil in case the item is not in the cache

    Declaration

    Swift

    func entry(forKey: KeyType) -> CacheEntry<ValueType>?
  • removes the item from the case. If there is no item with the key nothing happens

    Declaration

    Swift

    func removeValue(forKey: KeyType)
  • removes all items from the cache

    Declaration

    Swift

    func removeAllValues()
  • returns a sequence of the keys currently exists in the cache

    Declaration

    Swift

    func keys() -> AnySequence<KeyType>