CodableCache

open class CodableCache<Cache> : CodableCaching where Cache : Caching, Cache.ValueType == NSData

Implementation for CodableCache Stores each instance as Data in the cache using coder for encoding/decoding. The CodableCache instance is not thread safe. Using directly from multiple threads can lead to unexpected behaviors.

  • Typealias of KeyType for CodableCache

    Declaration

    Swift

    public typealias KeyType = Cache.KeyType
  • 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

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

    Declaration

    Swift

    open var numberOfEntries: Int { get }
  • Public initializer for CodableCache

    Declaration

    Swift

    public init(cache: Cache, coder: CoderProtocol = PlistCoder())

    Parameters

    cache

    cache which conforms to Caching protocol

    coder

    a Coder object which is able to encode and decode data. Conforms to CoderProtocol. By default it is a PlistCoder()

  • Loads data from codable cache

    Declaration

    Swift

    public func get<T>(_ type: T.Type, for key: KeyType) throws -> T? where T : Decodable, T : Encodable

    Parameters

    type

    the requested type, which has to conform to Codable protocol

    key

    a String object as a key, which identifies the data

    Return Value

    the requested data type if the key exists, or nil if not exists

  • Inserts data to codable cache In some cases it is possible that the item is not stored (for example the cost is higher than the maximum possible cost), in this case no error will happen

    Declaration

    Swift

    public func put<T>(_ value: T, for key: KeyType, with cost: Double = 0) throws where T: Codable

    Parameters

    value

    the insertable value which is a type, conforming to Codable protocol

    key

    a String which identifies the value in the cache

    cost

    a Double which is associated with the Value as a cost

  • Loads a CacheEntry from cache for the provided key and decodes the data

    Declaration

    Swift

    public func entry<T>(_ type: T.Type, for key: KeyType) throws -> CacheEntry<T>? where T : Decodable, T : Encodable

    Parameters

    type

    the requested type, which has to conform to Codable protocol

    key

    a String which identifies the entry in the cache

    Return Value

    a CacheEntry struct with decoded data

  • Removes the value from the cache for the provided key If the key does not exists, nothing happens, the result should be the same, the key and the value would not exist in the cache

    Declaration

    Swift

    public func remove(for key: KeyType)

    Parameters

    key

    a String which identifies the value in the cache

  • Removes everything from the cache

    Declaration

    Swift

    public func removeAllValues()
  • Loads the currently existing keys from the cache

    Declaration

    Swift

    public func keys() -> AnySequence<KeyType>

    Return Value

    an AnySequence which contains the String type keys