Files
NetNewsWire/Modules/Articles/Sources/Articles/DatabaseID.swift
2024-07-06 21:07:05 -07:00

36 lines
806 B
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// DatabaseID.swift
// NetNewsWire
//
// Created by Brent Simmons on 7/15/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import FoundationExtras
import os
final class DatabaseIDCache: Sendable {
static let shared = DatabaseIDCache()
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 {
databaseIDCache.withLock { cache in
if let identifier = cache[s] {
return identifier
}
// MD5 works because:
// * Its fast
// * Collisions arent going to happen with feed data
let identifier = s.md5String
cache[s] = identifier
return identifier
}
}
}