CompositeCache

open class CompositeCache<CompositeCacheKeyType, CompositeCacheValueType> : Caching

CompositeCache

CompositeCache is a multilevel cache container. It stores Caching implementations which has to have the same KeyType and ValueType types and use them according to a well defined rule.

let firstCache = MemoryCache<String, NSData>(maximumNumberOfEntries: 3, maximumCost: 0)
let secondCache = SecureStoreCache<NSData>(maximumNumberOfEntries: 6, maximumCost: 0, secureStore: store!, tableName: tableName)

compositeCache.addCache(firstCache)
compositeCache.addCache(secondCache)

The caches are used in a chain: if one cache doesn’t hold the necessary data the next will be queried - so the order of the caches are important. The item which was found in an underlying level will be added to all the preceding caches. The caches are appended to the end of the chain. When a value cleared from a cache because free space is needed to store a new value, the removed value won’t be added to lower level caches.

  • Declaration

    Swift

    public typealias KeyType = CompositeCacheKeyType
  • Declaration

    Swift

    public typealias ValueType = CompositeCacheValueType
  • initializes a new instance

    Declaration

    Swift

    public init()
  • Adds a cache to the internal cache collection. The KeyType and ValueType of the cache have to be compatible with the KeyType and ValueType of the CompositeCache and the other caches in the CompositeCache

    Declaration

    Swift

    open func addCache<C>(_ cache: C) where CompositeCacheKeyType == C.KeyType, CompositeCacheValueType == C.ValueType, C : Caching

    Parameters

    cache

    the cache to add to the CompositeCache

  • Returns always 0. The operation is not applicable on CopositeCache.

    Declaration

    Swift

    open var costOfEntries: Double { get }
  • Returns always 0. The operation is not applicable on CopositeCache.

    Declaration

    Swift

    open var numberOfEntries: Int { get }
  • Returns always an empty sequence. The operation is not applicable on CopositeCache.

    Declaration

    Swift

    open func keys() -> AnySequence<KeyType>
  • 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 happe

    Declaration

    Swift

    open func set(value: ValueType, forKey key: KeyType, withCost cost: Double = 0)
  • 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: KeyType) -> ValueType?
  • 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: KeyType) -> CacheEntry<CompositeCacheValueType>?
  • removes the item from the case. If there is no item with the key nothing happens

    Declaration

    Swift

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

    Declaration

    Swift

    open func removeAllValues()