diff --git a/Modules/Core/Sources/Core/Cache.swift b/Modules/Core/Sources/Core/Cache.swift index e9da4c3fc..33ee208b5 100644 --- a/Modules/Core/Sources/Core/Cache.swift +++ b/Modules/Core/Sources/Core/Cache.swift @@ -12,14 +12,14 @@ public protocol CacheRecord: Sendable { var dateCreated: Date { get } } -final class Cache: Sendable { +public final class Cache: 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 {