summaryrefslogtreecommitdiff
path: root/Hutch/Networking/ResponseCache.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Hutch/Networking/ResponseCache.swift')
-rw-r--r--Hutch/Networking/ResponseCache.swift33
1 files changed, 33 insertions, 0 deletions
diff --git a/Hutch/Networking/ResponseCache.swift b/Hutch/Networking/ResponseCache.swift
new file mode 100644
index 0000000..a6cc269
--- /dev/null
+++ b/Hutch/Networking/ResponseCache.swift
@@ -0,0 +1,33 @@
+import Foundation
+import os
+
+/// Thread-safe in-memory cache for raw GraphQL response data.
+/// Keyed by a caller-provided string (typically service name + query hash).
+final class ResponseCache: Sendable {
+
+ private let storage: OSAllocatedUnfairLock<[String: Data]>
+
+ init() {
+ self.storage = OSAllocatedUnfairLock(initialState: [:])
+ }
+
+ /// Store raw response data under a cache key.
+ func set(_ data: Data, forKey key: String) {
+ storage.withLock { $0[key] = data }
+ }
+
+ /// Retrieve cached response data. Returns nil on cache miss.
+ func get(forKey key: String) -> Data? {
+ storage.withLock { $0[key] }
+ }
+
+ /// Remove a specific entry.
+ func remove(forKey key: String) {
+ storage.withLock { _ = $0.removeValue(forKey: key) }
+ }
+
+ /// Clear all cached data (e.g. on sign-out).
+ func clear() {
+ storage.withLock { $0.removeAll() }
+ }
+}