diff options
| author | Christian Cleberg <[email protected]> | 2026-04-12 00:15:13 -0500 |
|---|---|---|
| committer | Christian Cleberg <[email protected]> | 2026-04-12 00:15:13 -0500 |
| commit | cb1ea5ea4f163d87053285d3fb7999b12a3558b9 (patch) | |
| tree | 84b562d12e642ff494194387b1b4e1d15b065eb0 /Hutch/Networking/SystemStatusCacheStore.swift | |
| parent | 1c5a417277d986ee6fb7dc9dff0332338bdf6f3e (diff) | |
| download | hutch-2.13.1.tar.gz hutch-2.13.1.tar.bz2 hutch-2.13.1.zip | |
harden system status and app reliabilityv2.13.1
Diffstat (limited to 'Hutch/Networking/SystemStatusCacheStore.swift')
| -rw-r--r-- | Hutch/Networking/SystemStatusCacheStore.swift | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/Hutch/Networking/SystemStatusCacheStore.swift b/Hutch/Networking/SystemStatusCacheStore.swift new file mode 100644 index 0000000..63c64b9 --- /dev/null +++ b/Hutch/Networking/SystemStatusCacheStore.swift @@ -0,0 +1,59 @@ +import Foundation + +actor SystemStatusCacheStore { + private let snapshotCacheKey = "systemStatusSnapshotCache" + private let incidentCacheKey = "systemStatusIncidentCache" + private let defaults: UserDefaults + + init(defaults: UserDefaults = .standard) { + self.defaults = defaults + } + + func loadSnapshotHTML() -> (html: String, timestamp: Date)? { + guard let html = defaults.string(forKey: snapshotCacheKey) else { + return nil + } + + let timestampValue = defaults.double(forKey: snapshotTimestampKey) + guard timestampValue > 0 else { + defaults.removeObject(forKey: snapshotCacheKey) + defaults.removeObject(forKey: snapshotTimestampKey) + return nil + } + + return (html, Date(timeIntervalSince1970: timestampValue)) + } + + func saveSnapshotHTML(_ html: String, timestamp: Date) { + defaults.set(html, forKey: snapshotCacheKey) + defaults.set(timestamp.timeIntervalSince1970, forKey: snapshotTimestampKey) + } + + func loadIncidentFeedData() -> (data: Data, timestamp: Date)? { + guard let data = defaults.data(forKey: incidentCacheKey) else { + return nil + } + + let timestampValue = defaults.double(forKey: incidentTimestampKey) + guard timestampValue > 0 else { + defaults.removeObject(forKey: incidentCacheKey) + defaults.removeObject(forKey: incidentTimestampKey) + return nil + } + + return (data, Date(timeIntervalSince1970: timestampValue)) + } + + func saveIncidentFeedData(_ data: Data, timestamp: Date) { + defaults.set(data, forKey: incidentCacheKey) + defaults.set(timestamp.timeIntervalSince1970, forKey: incidentTimestampKey) + } + + private var snapshotTimestampKey: String { + "\(snapshotCacheKey).timestamp" + } + + private var incidentTimestampKey: String { + "\(incidentCacheKey).timestamp" + } +} |
