Make Cache generic.

This commit is contained in:
Brent Simmons
2024-10-12 18:59:03 -07:00
parent fbb8c4ba38
commit 7e32d99d78

View File

@@ -12,14 +12,14 @@ public protocol CacheRecord: Sendable {
var dateCreated: Date { get }
}
final class Cache: Sendable {
public final class Cache<T: CacheRecord>: Sendable {
public let timeToLive: TimeInterval
public let timeBetweenCleanups: TimeInterval
private struct State: Sendable {
var lastCleanupDate = Date()
var cache = [String: CacheRecord]()
var cache = [String: T]()
}
private let stateLock = OSAllocatedUnfairLock(initialState: State())
@@ -29,7 +29,7 @@ final class Cache: Sendable {
self.timeBetweenCleanups = timeBetweenCleanups
}
public subscript(_ key: String) -> CacheRecord? {
public subscript(_ key: String) -> T? {
get {
stateLock.withLock { state in
@@ -42,7 +42,7 @@ final class Cache: Sendable {
state.cache[key] = nil
return nil
}
return value
}
}
@@ -52,6 +52,12 @@ final class Cache: Sendable {
}
}
}
public func cleanup() {
stateLock.withLock { state in
cleanupIfNeeded(&state)
}
}
}
extension Cache {