From 0431ce8d7980e47545eb8d479ddd9639c94a24ae Mon Sep 17 00:00:00 2001 From: Christopher Luu Date: Wed, 27 Jan 2016 11:48:54 -0500 Subject: [PATCH] Add documentation --- Source/AnyKey.swift | 6 ++++++ Source/KingCache.swift | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/Source/AnyKey.swift b/Source/AnyKey.swift index bc31c78..3657825 100644 --- a/Source/AnyKey.swift +++ b/Source/AnyKey.swift @@ -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(_ key: T) { @@ -31,6 +36,7 @@ struct AnyKey: Hashable { } } + /// `Hashable` protocol conformance var hashValue: Int { return hashValueFunc() } } diff --git a/Source/KingCache.swift b/Source/KingCache.swift index faf43c7..4e1a4cf 100644 --- a/Source/KingCache.swift +++ b/Source/KingCache.swift @@ -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(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(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(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)