25 lines
529 B
Swift
25 lines
529 B
Swift
import Foundation
|
|
|
|
@Observable
|
|
final class ContentCache {
|
|
static let shared = ContentCache()
|
|
|
|
private var timestamps: [String: Date] = [:]
|
|
private let ttl: TimeInterval = 300 // 5 分钟
|
|
|
|
private init() {}
|
|
|
|
func isExpired(key: String) -> Bool {
|
|
guard let ts = timestamps[key] else { return true }
|
|
return Date().timeIntervalSince(ts) > ttl
|
|
}
|
|
|
|
func markFresh(key: String) {
|
|
timestamps[key] = Date()
|
|
}
|
|
|
|
func invalidate(key: String) {
|
|
timestamps[key] = nil
|
|
}
|
|
}
|