KituraCache
public class KituraCache
A thread-safe, in-memory cache for storing an object against a Hashable key.
-
Statisticsabout the cache.Declaration
Swift
public private(set) var statistics: Statistics -
Initialize an instance of KituraCache.
Usage Example:
let cache = KituraCache(defaultTTL: 3600, checkFrequency: 600)Declaration
Swift
public init(defaultTTL: UInt = 0, checkFrequency: UInt = 60)Parameters
defaultTTLThe Time to Live value (in seconds) used for a new entry if none is specified in
setObject(_:forKey:withTTL:). IfdefaultTTLis not specified, a value of 0 (never expire) will be used.checkFrequencyThe frequency (in seconds) to check for expired entries. If
checkFrequencyis not specified, a value of 600 will be used (the check will occur every 10 minutes).
-
Adds a new entry or updates the existing entry if the key is already associated with an object in the cache. The lifespan of the entry (in seconds) in the cache can be set using the optional withTTL parameter.
Usage Example:
In this example, item is an instance of a
structobject with an id field which conforms toHashable.let cache = KituraCache() ... cache.setObject(item, forKey: item.id)Declaration
Swift
public func setObject<T: Hashable>(_ object: Any, forKey key: T, withTTL: UInt?=nil)Parameters
objectThe object to store in the cache.
forKeyThe
Hashablekey to be associated with the entry.withTTLThe optional Time to Live value (in seconds) for the entry. If not specified, the default TTL is used.
-
Retrieve an object from the cache for a specified key.
Usage Example:
In this example, item has been stored in the cache with an integer key.
let cache = KituraCache() ... if let item = cache.object(forKey: 1) { //Object with key of 1 retrieved from cache. ... } else { //No object stored in cache with key of 1. ... }Declaration
Swift
public func object<T: Hashable>(forKey key: T) -> Any?Parameters
forKeyThe key associated with the entry you want to retrieve.
Return Value
The object stored in the cache for the specified key, or nil if there is no object with the specified key.
-
Retrieve all of the keys present in the cache.
Usage Example:
let cache = KituraCache() ... let allKeys = cache.keys()Declaration
Swift
public func keys() -> [Any]Return Value
An array of all the keys present in the cache.
-
Remove an object from the cache for a specified key.
Usage Example:
In this example, objects have been stored in the cache with an integer key.
let cache = KituraCache() ... cache.removeObject(forKey: 1)Declaration
Swift
public func removeObject<T: Hashable>(forKey key: T)Parameters
forKeyThe key associated with the entry you want to remove from the cache.
-
Remove objects from the cache for multiple, specified keys.
Usage Example:
In this example, objects have been stored in the cache with an integer key.
let cache = KituraCache() ... cache.removeObjects(forKeys: 1, 2, 3)Declaration
Swift
public func removeObjects<T: Hashable>(forKeys keys: T...)Parameters
forKeysThe keys associated with the entries you want to remove.
-
Remove objects from the cache for multiple, specified keys provided in an array.
Usage Example:
In this example, objects have been stored in the cache with an integer key.
let cache = KituraCache() ... cache.removeObjects(forKeys: [1, 2, 3])Declaration
Swift
public func removeObjects<T: Hashable>(forKeys keys: [T])Parameters
forKeysAn array of keys associated with the entries you want to remove.
-
Remove all objects from the cache.
Usage Example:
let cache = KituraCache() ... cache.removeAllObjects()Declaration
Swift
public func removeAllObjects()
-
Set the Time to Live value (in seconds) for a cache entry.
Usage Example:
In this example, objects have been stored in the cache with an integer key.
let cache = KituraCache() ... cache.setTTL(ttl: 360, forKey: 1)Declaration
Swift
public func setTTL<T: Hashable>(_ ttl: UInt, forKey key: T) -> BoolParameters
ttlThe Time to Live value in seconds.
forKeyThe key specifying for which entry to set the TTL.
Return Value
True if the TTL was set successfully. False if the key doesn’t exist.
-
Remove all cache entries and reset the cache statistics.
Usage Example:
let cache = KituraCache() ... cache.flush()Declaration
Swift
public func flush()
View on GitHub
KituraCache Class Reference