36 lines
810 B
Swift
36 lines
810 B
Swift
//
|
|
// CachedDictionary.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 5/6/20.
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class CachedDictionary<Value> {
|
|
private let name: String
|
|
private var dict = [String: Value]()
|
|
private let queue: DispatchQueue
|
|
|
|
init(name: String) {
|
|
self.name = name
|
|
self.queue = DispatchQueue(label: "CachedDictionary (\(name)) Coordinator", attributes: .concurrent)
|
|
}
|
|
|
|
subscript(key: String) -> Value? {
|
|
get {
|
|
var result: Value? = nil
|
|
queue.sync {
|
|
result = dict[key]
|
|
}
|
|
return result
|
|
}
|
|
set(value) {
|
|
queue.async(flags: .barrier) {
|
|
self.dict[key] = value
|
|
}
|
|
}
|
|
}
|
|
}
|