Skip to content

Commit ab8d8eb

Browse files
committed
Use Clock directly
1 parent 176f144 commit ab8d8eb

File tree

2 files changed

+5
-45
lines changed

2 files changed

+5
-45
lines changed

Sources/DataCacheKit/DiskCache.swift

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public actor DiskCache<Key: Hashable & Sendable>: Caching, @unchecked Sendable {
2727
// }
2828
}
2929

30-
private let clock: _Clock
30+
private let clock: any Clock<Duration>
3131

3232
private var path: URL {
3333
get throws {
@@ -57,23 +57,9 @@ public actor DiskCache<Key: Hashable & Sendable>: Caching, @unchecked Sendable {
5757
return "[\(path.lastPathComponent)] "
5858
}
5959

60-
public init(options: Options, logger: Logger = .init(.disabled)) {
60+
public init(options: Options, clock: some Clock<Duration> = .suspending, logger: Logger = .init(.disabled)) {
6161
self.options = options
62-
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
63-
self.clock = NewClock(.suspending)
64-
} else {
65-
self.clock = _Clock()
66-
}
67-
self.logger = logger
68-
Task {
69-
try await _prepare()
70-
}
71-
}
72-
73-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
74-
init<C: Clock>(options: Options, clock: C, logger: Logger = .init(.disabled)) where C.Instant.Duration == Duration {
75-
self.options = options
76-
self.clock = NewClock(clock)
62+
self.clock = clock
7763
self.logger = logger
7864
Task {
7965
try await _prepare()
@@ -225,7 +211,7 @@ extension DiskCache {
225211
isFlushScheduled = true
226212
defer { isFlushScheduled = false }
227213

228-
try await clock.sleep(until: 1)
214+
try await clock.sleep(for: .seconds(1))
229215
try? await oldTask?.value
230216

231217
guard isFlushNeeded else { return }
@@ -344,7 +330,7 @@ extension DiskCache {
344330
logger.debug("\(self.logKey)sweep scheduled")
345331
let oldTask = sweepingTask
346332
sweepingTask = Task {
347-
try await clock.sleep(until: seconds)
333+
try await clock.sleep(for: .seconds(seconds))
348334
_ = await oldTask?.result
349335
do {
350336
logger.debug("\(self.logKey)sweep starting")
@@ -460,27 +446,6 @@ extension DiskCache {
460446
}
461447
}
462448

463-
extension DiskCache {
464-
class _Clock: @unchecked Sendable {
465-
func sleep(until seconds: Int) async throws {
466-
try await Task.sleep(nanoseconds: UInt64(seconds) * 1_000_000_000)
467-
}
468-
}
469-
470-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
471-
final class NewClock<C: Clock>: _Clock, @unchecked Sendable where C.Instant.Duration == Duration {
472-
let clock: C
473-
474-
init(_ clock: C) {
475-
self.clock = clock
476-
}
477-
478-
override func sleep(until seconds: Int) async throws {
479-
try await clock.sleep(until: clock.now.advanced(by: .seconds(seconds)), tolerance: nil)
480-
}
481-
}
482-
}
483-
484449
struct Staging<Key: Hashable & Sendable> {
485450
enum Operation: Equatable {
486451
case add(Data)

Tests/DataCacheKitTests/DiskCacheTests.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ final class DiskCacheTests {
3939
}
4040

4141
@Test
42-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
4342
func testStoreData() async throws {
4443
let clock = ManualClock()
4544
let cache = DiskCache<String>(options: cacheOptions(), clock: clock, logger: .init(.default))
@@ -78,7 +77,6 @@ final class DiskCacheTests {
7877
}
7978

8079
@Test
81-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
8280
func testStoreDataMultiple() async throws {
8381
let clock = ManualClock()
8482
let cache = DiskCache<String>(options: cacheOptions(), clock: clock, logger: .init(.default))
@@ -102,7 +100,6 @@ final class DiskCacheTests {
102100
}
103101

104102
@Test
105-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
106103
func testRemoveData() async throws {
107104
let clock = ManualClock()
108105
let cache = DiskCache<String>(options: cacheOptions(), clock: clock, logger: .init(.default))
@@ -137,7 +134,6 @@ final class DiskCacheTests {
137134
}
138135

139136
@Test
140-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
141137
func testRemoveDataAll() async throws {
142138
let clock = ManualClock()
143139
let cache = DiskCache<String>(options: cacheOptions(), clock: clock, logger: .init(.default))
@@ -177,7 +173,6 @@ final class DiskCacheTests {
177173
}
178174

179175
@Test
180-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
181176
func testSweep() async throws {
182177
let allocationUnit = 4096
183178

0 commit comments

Comments
 (0)