KituraCache

public class KituraCache

A thread-safe, in-memory cache for storing an object against a Hashable key.

  • Statistics about 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

    defaultTTL

    The Time to Live value (in seconds) used for a new entry if none is specified in setObject(_:forKey:withTTL:). If defaultTTL is not specified, a value of 0 (never expire) will be used.

    checkFrequency

    The frequency (in seconds) to check for expired entries. If checkFrequency is 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 struct object with an id field which conforms to Hashable.

    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

    object

    The object to store in the cache.

    forKey

    The Hashable key to be associated with the entry.

    withTTL

    The 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

    forKey

    The 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

    forKey

    The 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

    forKeys

    The 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

    forKeys

    An 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) -> Bool

    Parameters

    ttl

    The Time to Live value in seconds.

    forKey

    The 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()