Remove lock since writes are atomic.

This commit is contained in:
Brent Simmons committed 2026-10-29 11:00:40 -07:00
1 parent af2d55a194
commit ef49feed8b
1 file changed
+16 -27
@@ -7,57 +7,46 @@
//
import Foundation
import Synchronization
/// Each key is its own file. Writes are atomic.
nonisolated public final class BinaryDiskCache: Sendable {
public let folder: String
private let mutex = Mutex(())
public init(folder: String) {
self.folder = folder
}
public func data(forKey key: String) throws -> Data? {
try mutex.withLock { _ in
try _data(forKey: key)
}
try _data(forKey: key)
}
public func setData(_ data: Data, forKey key: String) throws {
try mutex.withLock { _ in
try _setData(data, forKey: key)
}
try _setData(data, forKey: key)
}
public func deleteData(forKey key: String) throws {
try mutex.withLock { _ in
try _deleteData(forKey: key)
}
try _deleteData(forKey: key)
}
// Subscript doesn’t throw. Use when you can ignore errors.
public subscript(_ key: String) -> Data? {
get {
mutex.withLock { _ in
do {
return try _data(forKey: key)
} catch {}
return nil
}
do {
return try _data(forKey: key)
} catch {}
return nil
}
set {
mutex.withLock { _ in
if let data = newValue {
do {
try _setData(data, forKey: key)
} catch {}
} else {
do {
try _deleteData(forKey: key)
} catch {}
}
if let data = newValue {
do {
try _setData(data, forKey: key)
} catch {}
} else {
do {
try _deleteData(forKey: key)
} catch {}
}
}
}