2017-07-17 04:36:38 +02:00
|
|
|
|
//
|
|
|
|
|
// DatabaseID.swift
|
|
|
|
|
// Data
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/15/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2017-09-03 01:08:02 +02:00
|
|
|
|
import RSCore
|
2017-07-17 04:36:38 +02:00
|
|
|
|
|
|
|
|
|
// MD5 works because:
|
|
|
|
|
// * It’s fast
|
|
|
|
|
// * Collisions aren’t going to happen with feed data
|
|
|
|
|
|
2017-09-02 23:19:42 +02:00
|
|
|
|
private var databaseIDCache = [String: String]()
|
2017-09-11 15:46:32 +02:00
|
|
|
|
private var databaseIDCacheLock = os_unfair_lock_s()
|
2017-09-02 23:19:42 +02:00
|
|
|
|
|
2017-09-19 07:00:35 +02:00
|
|
|
|
public func databaseIDWithString(_ s: String) -> String {
|
2017-07-17 04:36:38 +02:00
|
|
|
|
|
2017-09-11 15:46:32 +02:00
|
|
|
|
os_unfair_lock_lock(&databaseIDCacheLock)
|
|
|
|
|
defer {
|
|
|
|
|
os_unfair_lock_unlock(&databaseIDCacheLock)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-02 23:19:42 +02:00
|
|
|
|
if let identifier = databaseIDCache[s] {
|
|
|
|
|
return identifier
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let identifier = (s as NSString).rs_md5Hash()
|
|
|
|
|
databaseIDCache[s] = identifier
|
|
|
|
|
return identifier
|
2017-07-17 04:36:38 +02:00
|
|
|
|
}
|