parent
d224f47b8c
commit
ba209fa4d2
|
@ -21,7 +21,7 @@ class DiskCache<T> {
|
|||
let defaultExpiry: CacheExpiry
|
||||
let transformer: DiskCacheTransformer<T>
|
||||
|
||||
private var fileStates = [String: FileState]()
|
||||
private var fileStates = MultiThreadDictionary<String, FileState>()
|
||||
|
||||
init(name: String, defaultExpiry: CacheExpiry, transformer: DiskCacheTransformer<T>, fileManager: FileManager = .default) throws {
|
||||
self.defaultExpiry = defaultExpiry
|
||||
|
@ -117,7 +117,9 @@ class DiskCache<T> {
|
|||
func removeAll() throws {
|
||||
try fileManager.removeItem(atPath: path)
|
||||
try createDirectory()
|
||||
fileStates.removeAll()
|
||||
fileStates.withLock { dict in
|
||||
dict.removeAll()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,13 +16,7 @@ class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> {
|
|||
private let lock: LockHolder<[AnyHashable: Any]>
|
||||
|
||||
init() {
|
||||
if #available(iOS 16.0, *) {
|
||||
let lock = OSAllocatedUnfairLock(initialState: [:])
|
||||
self.lock = LockHolder(withLock: lock.withLock(_:))
|
||||
} else {
|
||||
let lock = UnfairLock(initialState: [:])
|
||||
self.lock = LockHolder(withLock: lock.withLock(_:))
|
||||
}
|
||||
self.lock = LockHolder(initialState: [:])
|
||||
}
|
||||
|
||||
subscript(key: Key) -> Value? {
|
||||
|
@ -66,6 +60,16 @@ class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> {
|
|||
// see #178
|
||||
fileprivate struct LockHolder<State> {
|
||||
let withLock: (_ body: @Sendable (inout State) throws -> any Sendable) throws -> any Sendable
|
||||
|
||||
init(initialState: State) {
|
||||
if #available(iOS 16.0, *) {
|
||||
let lock = OSAllocatedUnfairLock(initialState: initialState)
|
||||
self.withLock = lock.withLock(_:)
|
||||
} else {
|
||||
let lock = UnfairLock(initialState: initialState)
|
||||
self.withLock = lock.withLock(_:)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: replace this only with OSAllocatedUnfairLock
|
||||
|
|
Loading…
Reference in New Issue