Swiftpack.co - Package - IBM-Swift/Kitura-Cache


Kitura cache

Build Status - Master Mac OS X Linux Apache 2


Kitura thread-safe in-memory cache.



  public init(defaultTTL: UInt = 0, checkFrequency: UInt = 600)


  • defaultTTL is the default Time To Live (TTL) of cache entries in seconds, its default value is 0, which means infinity

  • checkFrequency defines the frequency in seconds for checking and removing expired entries

Add or update entry in the cache

public func setObject<T: Hashable>(_ object: Any, forKey key: T, withTTL: UInt?=nil) {
  • key has to be Hashable
  • if TTL is not specified, cache's defaultTTL is used for the entry

Retrieve an entry from the cache

  public func object<T: Hashable>(forKey key: T) -> Any?
  • key has to be Hashable

Delete entries in the cache

public func removeObject<T: Hashable>(forKey key: T)
public func removeObjects<T: Hashable>(forKeys keys: T...)
public func removeObjects<T: Hashable>(forKeys keys: [T])
public func removeAllObjects()
  • key/s have to be Hashable

Set TTL of an entry in the cache

public func setTTL<T: Hashable>(_ ttl: UInt, forKey key: T) -> Bool
  • returns false if key doesn't exits
  • key has to be Hashable

Retrieve all keys

public func keys() -> [Any]?

Remove all values and reset the statistics of the cache

public func flush()

Retrieve cache statistics

Cache statistics are stored in

public private(set) var statistics: Statistics

Statistics struct contains:

  • hits the number of cache hits
  • misses the number of cache misses
  • numberOfKeys the total number of keys in the cache


This library is licensed under Apache 2.0. Full license text is available in LICENSE.


Stars: 4
Help us keep the lights on



2.0.0 - Oct 31, 2017

What's New?

  • Full migration to Swift 4

1.7.1 - Sep 13, 2017

What's New

  • Add support for Swift 4