Add documentation

This commit is contained in:
Christopher Luu 2016-01-27 11:48:54 -05:00
parent 2c607fd984
commit 0431ce8d79
2 changed files with 41 additions and 0 deletions

View file

@ -10,9 +10,14 @@ import Foundation
/// This code was taken from:
/// http://stackoverflow.com/questions/24119624/how-to-create-dictionary-that-can-hold-anything-in-key-or-all-the-possible-type
///
/// `AnyKey` is a simple struct that conforms to `Hashable` to allow any other `Hashable` key to be used in the cache dictionary
struct AnyKey: Hashable {
/// The underlying value
private let underlying: Any
/// The hashing function
private let hashValueFunc: () -> Int
/// The equality function
private let equalityFunc: (Any) -> Bool
init<T: Hashable>(_ key: T) {
@ -31,6 +36,7 @@ struct AnyKey: Hashable {
}
}
/// `Hashable` protocol conformance
var hashValue: Int { return hashValueFunc() }
}

View file

@ -8,17 +8,24 @@
import Foundation
/// `KingCache` is a simple cache that can hold anything, including Swift structs, enums, and values.
/// It is designed to work similar to the `NSCache`, but with native Swift support.
///
public class KingCache {
// MARK: - Private variables
/// An array of `NSNotificationCenter` observers that need to be removed upon deinitialization
private var notificationObservers: [NSObjectProtocol] = []
// MARK: - Internal variables
/// The dictionary that holds the cached values
var cacheDictionary: [AnyKey: Any] = [:]
// MARK: - Public variables
/// The number of items in the cache
public var count: Int {
return cacheDictionary.count
}
/// The limit of the amount of items that can be held in the cache. This defaults to 0, which means there is no limit.
public var countLimit: UInt = 0 {
didSet {
evictItemsIfNeeded()
@ -52,6 +59,9 @@ public class KingCache {
}
// MARK: - Internal methods
/// Evicts items if the `countLimit` has been reached.
/// This currently uses a random eviction policy, kicking out random items until the `countLimit` is satisfied.
///
func evictItemsIfNeeded() {
if countLimit > 0 && cacheDictionary.count > Int(countLimit) {
// TODO: Evict items with more rhyme or reason
@ -65,11 +75,24 @@ public class KingCache {
}
// MARK: - Public methods
/// Adds an item to the cache for any given `Hashable` key.
///
/// - parameter item: The item to be cached
/// - parameter key: The key with which to cache the item
///
public func setItem<K: Hashable>(item: Any, forKey key: K) {
cacheDictionary[AnyKey(key)] = item
evictItemsIfNeeded()
}
/// Gets an item from the cache if it exists for a given `Hashable` key.
/// This method uses generics to infer the type that should be returned.
///
/// Note: Even if an item exists for the key, but does not match the given type, it will return `nil`.
///
/// - parameter key: The key whose item should be fetched
/// - returns: The item from the cache if it exists, or `nil` if an item could not be found
///
public func itemForKey<T, K: Hashable>(key: K) -> T? {
if let item = cacheDictionary[AnyKey(key)] as? T {
return item
@ -77,16 +100,24 @@ public class KingCache {
return nil
}
/// Discards an item for a given `Hashable` key.
///
/// - parameter key: The key whose item should be removed
///
public func removeItemForKey<K: Hashable>(key: K) {
cacheDictionary[AnyKey(key)] = nil
}
/// Clears the entire cache.
///
public func removeAllItems() {
cacheDictionary.removeAll()
}
// MARK: - Subscript methods
// TODO: Consolidate these subscript methods once subscript generics with constraints are supported
/// A subscript method that allows `Int` key subscripts.
///
public subscript(key: Int) -> Any? {
get {
return itemForKey(key)
@ -101,6 +132,8 @@ public class KingCache {
}
}
/// A subscript method that allows `Float` key subscripts.
///
public subscript(key: Float) -> Any? {
get {
return itemForKey(key)
@ -115,6 +148,8 @@ public class KingCache {
}
}
/// A subscript method that allows `String` key subscripts.
///
public subscript(key: String) -> Any? {
get {
return itemForKey(key)