Skip to content

Commit ec27377

Browse files
authored
Merge pull request #147 from yim-lee/cache-delete
Add 'delete' to PersistentCache
2 parents 243beea + 55e1ed0 commit ec27377

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Sources/TSCUtility/PersistenceCache.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Foundation
1515
public protocol PersistentCacheProtocol {
1616
func get(key: Data) throws -> Data?
1717
func put(key: Data, value: Data) throws
18+
func delete(key: Data) throws
1819
}
1920

2021
/// SQLite backed persistent cache.
@@ -63,4 +64,14 @@ public final class SQLiteBackedPersistentCache: PersistentCacheProtocol {
6364
try writeStmt.step()
6465
try writeStmt.finalize()
6566
}
67+
68+
public func delete(key: Data) throws {
69+
let deleteStmt = try self.db.prepare(query: "DELETE FROM TSCCACHE WHERE key == ?;")
70+
let bindings: [SQLite.SQLiteValue] = [
71+
.blob(key)
72+
]
73+
try deleteStmt.bind(bindings)
74+
try deleteStmt.step()
75+
try deleteStmt.finalize()
76+
}
6677
}

Tests/TSCUtilityTests/PersistentCacheTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ class PersistentCacheTests: XCTestCase {
5959
try decoder.decode(Value.self, from: $0)
6060
}
6161
XCTAssertEqual(retVal3, value1)
62+
63+
try cache.delete(key: encoder.encode(key1))
64+
65+
XCTAssertNil(try cache.get(key: encoder.encode(key1)))
66+
let retVal4 = try cache.get(key: encoder.encode(key2)).map {
67+
try decoder.decode(Value.self, from: $0)
68+
}
69+
XCTAssertEqual(retVal4, value2)
6270
}
6371
}
6472
}

0 commit comments

Comments
 (0)