SecureStoreCache

open class SecureStoreCache<Value> : Caching where Value : NSCoding

SecureStoreCache

SecureStoreCache stores Strings in SecureStorage with KeyType. Currently the Least Recently Used (LRU) cache replacement policy is implemented. Generic parameters:

let fileName: String = ...  // file name for the secure database store
let tableName: String = ... // table name for cache
let let encryptionKey = ... // secure database store encryption key

// create a new secure database store
var store = SecureDatabaseStore(databaseFileName: fileName)

// open the store using the encryption key
try store?.open(with: encryptionKey)

// create a secure store cache with the given cache limits
let cache = SecureStoreCache<NSData>(maximumNumberOfEntries: 10, maximumCost: 5, secureStore: store!, tableName: tableName)

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

let value: NSData = NSData(data: dataValue)

// save the value in the cache for the key associated with the cost.
cache.set(value: value, 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
  • instance of SecureDatabaseStore

    Declaration

    Swift

    public var store: SecureDatabaseStore? { get }
  • Create an instance with the given cache limits. Zero or more parameters can be specified.

    Declaration

    Swift

    required public init(maximumNumberOfEntries: Int = 0, maximumCost: Double = 0, secureStore: SecureDatabaseStore, tableName: String = "SApcpSDKSecureStoreCache")

    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.

    secureStore

    instance of SecureDatabaseStore with open state

    tableName

    the database table name where the items will be stored

  • 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: 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<ValueType>?
  • 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()
  • returns a secuence of the keys currently exists in the cache

    Declaration

    Swift

    open func keys() -> AnySequence<KeyType>