From 32ad6cab8d58d99ebd8a28e8fa6e6f4e587cb1e5 Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Tue, 17 Mar 2026 23:19:43 -0500 Subject: v1.0 --- Hutch/Networking/ResponseCache.swift | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Hutch/Networking/ResponseCache.swift (limited to 'Hutch/Networking/ResponseCache.swift') 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() } + } +} -- cgit v1.2.3