Make DatabaseIDCache properly Sendable (instead of unchecked) by using an OSAllocatedUnfairLock.

This commit is contained in:
Brent Simmons 2024-05-07 19:58:23 -07:00
parent 138177858c
commit ae77aece2a

View File

@ -8,22 +8,25 @@
import Foundation
import FoundationExtras
import os
class DatabaseIDCache: @unchecked Sendable {
final class DatabaseIDCache: Sendable {
static let shared = DatabaseIDCache()
private var databaseIDCache = [String: String]()
private let databaseIDCacheLock = NSLock()
private let _databaseIDCache = OSAllocatedUnfairLock(initialState: [String: String]())
private var databaseIDCache: [String: String] {
get {
_databaseIDCache.withLock { $0 }
}
set {
_databaseIDCache.withLock { $0 = newValue }
}
}
/// Generates or retrieves from cache  a database-suitable ID based on a String.
func databaseIDWithString(_ s: String) -> String {
databaseIDCacheLock.lock()
defer {
databaseIDCacheLock.unlock()
}
if let identifier = databaseIDCache[s] {
return identifier
}