Last active
May 8, 2019 07:17
-
-
Save hlung/4ab4077f9cd68fc6b7422adfdef1e483 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Cache<T> { | |
private var dict: [String: T] = [:] | |
var didSetHandler: ((_ key: String, _ value: T) -> Void)? | |
func clear() { | |
dict.removeAll() | |
} | |
func set(_ value: T, for key: String, skipDidSetHandler: Bool = false) { | |
dict[key] = value | |
if !skipDidSetHandler { | |
didSetHandler?(key, value) | |
} | |
} | |
// returns an optional because key can be non-existent | |
func value(for key: String) -> T? { | |
return dict[key] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment