krz/hutch
an ios client for sourcehut
clone: git clone https://gitbay.org/krz/hutch.git
v3.10.0: Hutch/Networking/ResponseCache.swift · raw
1import Foundation
2import os
3
4/// Thread-safe in-memory cache for raw GraphQL response data.
5/// Keyed by a caller-provided string (typically service name + query hash).
6final class ResponseCache: Sendable {
7
8 private let storage: OSAllocatedUnfairLock<[String: Data]>
9
10 init() {
11 self.storage = OSAllocatedUnfairLock(initialState: [:])
12 }
13
14 /// Store raw response data under a cache key.
15 func set(_ data: Data, forKey key: String) {
16 storage.withLock { $0[key] = data }
17 }
18
19 /// Retrieve cached response data. Returns nil on cache miss.
20 func get(forKey key: String) -> Data? {
21 storage.withLock { $0[key] }
22 }
23
24 /// Remove a specific entry.
25 func remove(forKey key: String) {
26 storage.withLock { _ = $0.removeValue(forKey: key) }
27 }
28
29 /// Clear all cached data (e.g. on sign-out).
30 func clear() {
31 storage.withLock { $0.removeAll() }
32 }
33}