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