Simplify and improve DatabaseIDCache.
This commit is contained in:
parent
ae77aece2a
commit
48bfcedbf7
@ -14,20 +14,13 @@ final class DatabaseIDCache: Sendable {
|
|||||||
|
|
||||||
static let shared = DatabaseIDCache()
|
static let shared = DatabaseIDCache()
|
||||||
|
|
||||||
private let _databaseIDCache = OSAllocatedUnfairLock(initialState: [String: String]())
|
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.
|
/// Generates — or retrieves from cache — a database-suitable ID based on a String.
|
||||||
func databaseIDWithString(_ s: String) -> String {
|
func databaseIDWithString(_ s: String) -> String {
|
||||||
|
|
||||||
if let identifier = databaseIDCache[s] {
|
databaseIDCache.withLock { cache in
|
||||||
|
if let identifier = cache[s] {
|
||||||
return identifier
|
return identifier
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +28,8 @@ final class DatabaseIDCache: Sendable {
|
|||||||
// * It’s fast
|
// * It’s fast
|
||||||
// * Collisions aren’t going to happen with feed data
|
// * Collisions aren’t going to happen with feed data
|
||||||
let identifier = s.md5String
|
let identifier = s.md5String
|
||||||
databaseIDCache[s] = identifier
|
cache[s] = identifier
|
||||||
return identifier
|
return identifier
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user