mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Make Cache generic.
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user