Skip to content

Instantly share code, notes, and snippets.

@hlung
Last active May 8, 2019 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlung/4ab4077f9cd68fc6b7422adfdef1e483 to your computer and use it in GitHub Desktop.
Save hlung/4ab4077f9cd68fc6b7422adfdef1e483 to your computer and use it in GitHub Desktop.
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