Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/Bodega/DiskStorageEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public actor DiskStorageEngine: StorageEngine {
/// - dataAndKeys: An array of the `[(CacheKey, Data)]` to store
/// multiple `Data` items with their associated keys at once.
public func write(_ dataAndKeys: [(key: CacheKey, data: Data)]) throws {
guard !dataAndKeys.isEmpty else { return }
for dataAndKey in dataAndKeys {
try self.write(dataAndKey.data, key: dataAndKey.key)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Bodega/ObjectStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public actor ObjectStorage<Object: Codable> {
/// - objectsAndKeys: An array of `[(CacheKey, Object)]` to store
/// multiple objects with their associated keys at once.
public func store(_ objectsAndKeys: [(key: CacheKey, object: Object)]) async throws {
guard !objectsAndKeys.isEmpty else { return }
let dataAndKeys = try objectsAndKeys.map({ try ($0.key, self.encoder.encode($0.object)) })

try await storage.write(dataAndKeys)
Expand Down Expand Up @@ -127,9 +128,8 @@ public actor ObjectStorage<Object: Codable> {
/// - Parameters:
/// - keys: A `[CacheKey]` for matching multiple `Object`s.
public func removeObject(forKeys keys: [CacheKey]) async throws {
for key in keys {
try await storage.remove(key: key)
}
guard !keys.isEmpty else { return }
try await storage.remove(keys: keys)
}

/// Removes all of the `Object`s.
Expand Down
1 change: 1 addition & 0 deletions Sources/Bodega/StorageEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extension StorageEngine {
/// - Parameters:
/// - keys: A `[CacheKey]` for matching multiple `Data` items.
public func remove(keys: [CacheKey]) async throws {
guard !keys.isEmpty else { return }
for key in keys {
try await self.remove(key: key)
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/BodegaTests/ObjectStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ class ObjectStorageTests: XCTestCase {
XCTAssertEqual(Self.storedKeysAndObjects.map(\.object), readKeysAndObjects.map(\.object))
}

func testWritingEmptyObjectsAndKeys() async throws {
try await storage.store([] as [(key: CacheKey, object: CodableObject)])

let objectCount = await storage.keyCount()
XCTAssertEqual(objectCount, 0)
}

func testReadingObjectSucceeds() async throws {
// Read one object
try await storage.store(Self.testObject, forKey: Self.testCacheKey)
Expand Down Expand Up @@ -251,6 +258,14 @@ class ObjectStorageTests: XCTestCase {

}

func testRemovingEmptyKeysDoesNothing() async throws {
try await storage.store(Self.storedKeysAndObjects)
try await storage.removeObject(forKeys: [] as [CacheKey])

let objectCount = await storage.keyCount()
XCTAssertEqual(objectCount, Self.storedKeysAndObjects.count)
}

func testInvalidRemoveErrors() async throws {
try await storage.store(Self.testObject, forKey: Self.testCacheKey)
try await storage.removeObject(forKey: CacheKey("alternative-test-key"))
Expand Down
Loading