diff --git a/Articles/Sources/Articles/DatabaseID.swift b/Articles/Sources/Articles/DatabaseID.swift index 1decef9b8..8ba9b667e 100644 --- a/Articles/Sources/Articles/DatabaseID.swift +++ b/Articles/Sources/Articles/DatabaseID.swift @@ -14,28 +14,22 @@ final class DatabaseIDCache: Sendable { static let shared = DatabaseIDCache() - private let _databaseIDCache = OSAllocatedUnfairLock(initialState: [String: String]()) - private var databaseIDCache: [String: String] { - get { - _databaseIDCache.withLock { $0 } - } - set { - _databaseIDCache.withLock { $0 = newValue } - } - } + private let databaseIDCache = OSAllocatedUnfairLock(initialState: [String: String]()) /// Generates — or retrieves from cache — a database-suitable ID based on a String. func databaseIDWithString(_ s: String) -> String { - if let identifier = databaseIDCache[s] { + databaseIDCache.withLock { cache in + if let identifier = cache[s] { + return identifier + } + + // MD5 works because: + // * It’s fast + // * Collisions aren’t going to happen with feed data + let identifier = s.md5String + cache[s] = identifier return identifier } - - // MD5 works because: - // * It’s fast - // * Collisions aren’t going to happen with feed data - let identifier = s.md5String - databaseIDCache[s] = identifier - return identifier } }