mirror of
https://github.com/samsonjs/CacheCreek.git
synced 2026-04-27 15:07:39 +00:00
Add documentation
This commit is contained in:
parent
2c607fd984
commit
0431ce8d79
2 changed files with 41 additions and 0 deletions
|
|
@ -10,9 +10,14 @@ import Foundation
|
||||||
|
|
||||||
/// This code was taken from:
|
/// 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
|
/// 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 {
|
struct AnyKey: Hashable {
|
||||||
|
/// The underlying value
|
||||||
private let underlying: Any
|
private let underlying: Any
|
||||||
|
/// The hashing function
|
||||||
private let hashValueFunc: () -> Int
|
private let hashValueFunc: () -> Int
|
||||||
|
/// The equality function
|
||||||
private let equalityFunc: (Any) -> Bool
|
private let equalityFunc: (Any) -> Bool
|
||||||
|
|
||||||
init<T: Hashable>(_ key: T) {
|
init<T: Hashable>(_ key: T) {
|
||||||
|
|
@ -31,6 +36,7 @@ struct AnyKey: Hashable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `Hashable` protocol conformance
|
||||||
var hashValue: Int { return hashValueFunc() }
|
var hashValue: Int { return hashValueFunc() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,17 +8,24 @@
|
||||||
|
|
||||||
import Foundation
|
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 {
|
public class KingCache {
|
||||||
// MARK: - Private variables
|
// MARK: - Private variables
|
||||||
|
/// An array of `NSNotificationCenter` observers that need to be removed upon deinitialization
|
||||||
private var notificationObservers: [NSObjectProtocol] = []
|
private var notificationObservers: [NSObjectProtocol] = []
|
||||||
|
|
||||||
// MARK: - Internal variables
|
// MARK: - Internal variables
|
||||||
|
/// The dictionary that holds the cached values
|
||||||
var cacheDictionary: [AnyKey: Any] = [:]
|
var cacheDictionary: [AnyKey: Any] = [:]
|
||||||
|
|
||||||
// MARK: - Public variables
|
// MARK: - Public variables
|
||||||
|
/// The number of items in the cache
|
||||||
public var count: Int {
|
public var count: Int {
|
||||||
return cacheDictionary.count
|
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 {
|
public var countLimit: UInt = 0 {
|
||||||
didSet {
|
didSet {
|
||||||
evictItemsIfNeeded()
|
evictItemsIfNeeded()
|
||||||
|
|
@ -52,6 +59,9 @@ public class KingCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Internal methods
|
// 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() {
|
func evictItemsIfNeeded() {
|
||||||
if countLimit > 0 && cacheDictionary.count > Int(countLimit) {
|
if countLimit > 0 && cacheDictionary.count > Int(countLimit) {
|
||||||
// TODO: Evict items with more rhyme or reason
|
// TODO: Evict items with more rhyme or reason
|
||||||
|
|
@ -65,11 +75,24 @@ public class KingCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Public methods
|
// 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) {
|
public func setItem<K: Hashable>(item: Any, forKey key: K) {
|
||||||
cacheDictionary[AnyKey(key)] = item
|
cacheDictionary[AnyKey(key)] = item
|
||||||
evictItemsIfNeeded()
|
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? {
|
public func itemForKey<T, K: Hashable>(key: K) -> T? {
|
||||||
if let item = cacheDictionary[AnyKey(key)] as? T {
|
if let item = cacheDictionary[AnyKey(key)] as? T {
|
||||||
return item
|
return item
|
||||||
|
|
@ -77,16 +100,24 @@ public class KingCache {
|
||||||
return nil
|
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) {
|
public func removeItemForKey<K: Hashable>(key: K) {
|
||||||
cacheDictionary[AnyKey(key)] = nil
|
cacheDictionary[AnyKey(key)] = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Clears the entire cache.
|
||||||
|
///
|
||||||
public func removeAllItems() {
|
public func removeAllItems() {
|
||||||
cacheDictionary.removeAll()
|
cacheDictionary.removeAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Subscript methods
|
// MARK: - Subscript methods
|
||||||
// TODO: Consolidate these subscript methods once subscript generics with constraints are supported
|
// 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? {
|
public subscript(key: Int) -> Any? {
|
||||||
get {
|
get {
|
||||||
return itemForKey(key)
|
return itemForKey(key)
|
||||||
|
|
@ -101,6 +132,8 @@ public class KingCache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A subscript method that allows `Float` key subscripts.
|
||||||
|
///
|
||||||
public subscript(key: Float) -> Any? {
|
public subscript(key: Float) -> Any? {
|
||||||
get {
|
get {
|
||||||
return itemForKey(key)
|
return itemForKey(key)
|
||||||
|
|
@ -115,6 +148,8 @@ public class KingCache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A subscript method that allows `String` key subscripts.
|
||||||
|
///
|
||||||
public subscript(key: String) -> Any? {
|
public subscript(key: String) -> Any? {
|
||||||
get {
|
get {
|
||||||
return itemForKey(key)
|
return itemForKey(key)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue