缓存
http://www.jianshu.com/p/ce5e7427e740
自定义设置时间戳缓存
static func set<T>(key: String, value: T, timeout: Double = 0) {
objc_sync_enter(lock)
let saveValue: [String: Any] = [
"saveTime" : timeout == 0 ? 0 : NSDate().timeIntervalSince1970 + timeout,
"data" : value
]
self.value[key] = saveValue
objc_sync_exit(lock)
}
static func get<T>(key: String) -> T? {
objc_sync_enter(lock)
let t = _T<[String: Any]>.cast(self.value[key])
if t != nil {
let cacheTime = (t!["saveTime"] ?? 0) as! Double
if cacheTime > 0 && NSDate().timeIntervalSince1970 > cacheTime {
self.value.removeValueForKey(key)
}
} else {
return nil
}
objc_sync_exit(lock)
return t!["data"] as? T
}