From cedc9bd470f764ab38ed0300433f95a0f1e88627 Mon Sep 17 00:00:00 2001 From: Ashli Rankin Date: Tue, 31 Jan 2023 11:34:10 -0500 Subject: [PATCH 1/4] Added a new protocol method to the cache protocol --- Sources/Persister/Cache.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/Persister/Cache.swift b/Sources/Persister/Cache.swift index 86d6ae7..00ea296 100644 --- a/Sources/Persister/Cache.swift +++ b/Sources/Persister/Cache.swift @@ -24,6 +24,13 @@ public protocol Cache { /// - key: The key that can be used to recall the written item later. func write(item: Item, forKey key: String) throws + /// Writes an item to cache, providing an expiration policy. + /// - Parameters: + /// - item: The item to write to the cache. + /// - key: The key that can be used to recall the written item later. + /// - expirationPolicy: Determines when newly written items are considered expired. + func write(item: Item, forKey key: String, expirationPolicy: CacheExpirationPolicy) throws + /// Removes an item associated with the given `key`. /// - Parameter key: The key associated with the item when it was written. func remove(forKey key: String) throws From 2868294c52452b91e2b790cd9b0d7ab669710b1e Mon Sep 17 00:00:00 2001 From: Ashli Rankin Date: Tue, 31 Jan 2023 11:34:23 -0500 Subject: [PATCH 2/4] Extended new protocol method --- Sources/Persister/DiskCache.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/Persister/DiskCache.swift b/Sources/Persister/DiskCache.swift index e43ad31..14d5d2f 100644 --- a/Sources/Persister/DiskCache.swift +++ b/Sources/Persister/DiskCache.swift @@ -43,6 +43,10 @@ extension DiskCache: Cache { // MARK: - Cache public func write(item: Item, forKey key: String) throws { + try write(item: item, forKey: key, expirationPolicy: expirationPolicy) + } + + public func write(item: Item, forKey key: String, expirationPolicy: CacheExpirationPolicy) throws { try diskManager.createDirectoryIfNecessary(directoryURL: rootDirectoryURL) let filePath = persistencePath(forKey: key) From 4300fac255aa5124071c9a6cdc0d300bcb9efd4f Mon Sep 17 00:00:00 2001 From: Ashli Rankin Date: Tue, 31 Jan 2023 11:34:59 -0500 Subject: [PATCH 3/4] Adding new protocol method implementation. --- Sources/Persister/MemoryCache.swift | 4 ++++ Sources/Persister/Persister.swift | 5 +++++ Tests/DiskCacheTests.swift | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/Sources/Persister/MemoryCache.swift b/Sources/Persister/MemoryCache.swift index a64acf6..fae77ef 100644 --- a/Sources/Persister/MemoryCache.swift +++ b/Sources/Persister/MemoryCache.swift @@ -38,6 +38,10 @@ extension MemoryCache: Cache { } public func write(item: Item, forKey key: String) throws { + try write(item: item, forKey: key, expirationPolicy: expirationPolicy) + } + + public func write(item: Item, forKey key: String, expirationPolicy: CacheExpirationPolicy) throws where Item : Decodable, Item : Encodable { let expirationDate = expirationPolicy.expirationDate(from: Date()) let container = ItemContainer(item: item, expirationDate: expirationDate) diff --git a/Sources/Persister/Persister.swift b/Sources/Persister/Persister.swift index 3a7f4e3..7738a70 100644 --- a/Sources/Persister/Persister.swift +++ b/Sources/Persister/Persister.swift @@ -48,6 +48,11 @@ extension Persister: Cache { try diskCache.write(item: item, forKey: key) } + public func write(item: Item, forKey key: String, expirationPolicy: CacheExpirationPolicy) throws { + try memoryCache.write(item: item, forKey: key, expirationPolicy: expirationPolicy) + try diskCache.write(item: item, forKey: key, expirationPolicy: expirationPolicy) + } + public func remove(forKey key: String) throws { try memoryCache.remove(forKey: key) try diskCache.remove(forKey: key) diff --git a/Tests/DiskCacheTests.swift b/Tests/DiskCacheTests.swift index 55f10ae..96e76cb 100644 --- a/Tests/DiskCacheTests.swift +++ b/Tests/DiskCacheTests.swift @@ -91,4 +91,23 @@ final class DiskCacheTests: XCTestCase { } } } + + func testUsesCorrectExpirationPolicy() throws { + let expectation = XCTestExpectation(description: "Only one item should have been removed.") + + let cache = DiskCache(rootDirectoryURL: diskURL, expirationPolicy: .afterInterval(1)) + try cache.write(item: TestCodable(), forKey: itemKey, expirationPolicy: .afterInterval(500)) + + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + + try? cache.removeExpired() + + let item1: ItemContainer? = try? cache.read(forKey: self.itemKey) + + XCTAssertNotNil(item1) + + expectation.fulfill() + } + wait(for: [expectation], timeout: 5) + } } From b9f663ba8acdd84015fbd1a830060cab554f3abb Mon Sep 17 00:00:00 2001 From: Ashli Rankin Date: Tue, 31 Jan 2023 11:37:04 -0500 Subject: [PATCH 4/4] Added test to the memory cache test file --- Tests/MemoryCacheTests.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Tests/MemoryCacheTests.swift b/Tests/MemoryCacheTests.swift index 31c0f26..0d35eab 100644 --- a/Tests/MemoryCacheTests.swift +++ b/Tests/MemoryCacheTests.swift @@ -90,4 +90,23 @@ final class MemoryCacheTests: XCTestCase { XCTAssertNotNil(item3) XCTAssertNotNil(item4) } + + func testUsesCorrectExpirationPolicy() throws { + let expectation = XCTestExpectation(description: "Only one item should have been removed.") + + let cache = MemoryCache(capacity: .unlimited, expirationPolicy: .afterInterval(1)) + try cache.write(item: TestCodable(), forKey: itemKey, expirationPolicy: .afterInterval(500)) + + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + + try? cache.removeExpired() + + let item1: ItemContainer? = try? cache.read(forKey: self.itemKey) + + XCTAssertNotNil(item1) + + expectation.fulfill() + } + wait(for: [expectation], timeout: 5) + } }