mirror of
https://github.com/hyperoslo/Cache.git
synced 2026-04-07 19:17:36 +00:00
Merge pull request #323 from kitwtnb/add-async-await
Add async await into AsyncStorage
This commit is contained in:
@@ -288,6 +288,45 @@ storage.async.removeExpiredObjects() { result in
|
||||
}
|
||||
```
|
||||
|
||||
#### Swift Concurrency
|
||||
|
||||
```swift
|
||||
do {
|
||||
try await storage.async.setObject("Oslo", forKey: "my favorite city")
|
||||
print("saved successfully")
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
|
||||
do {
|
||||
let city = try await storage.async.object(forKey: "my favorite city")
|
||||
print("my favorite city is \(city)")
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
|
||||
do {
|
||||
let exists = try await storage.async.objectExists(forKey: "my favorite city")
|
||||
if exists {
|
||||
print("I have a favorite city")
|
||||
}
|
||||
} catch {}
|
||||
|
||||
do {
|
||||
try await storage.async.remoeAll()
|
||||
print("removal completes")
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
|
||||
do {
|
||||
try await storage.async.removeExpiredObjects()
|
||||
print("removal completes")
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
```
|
||||
|
||||
### Expiry date
|
||||
|
||||
By default, all saved objects have the same expiry as the expiry you specify in `DiskConfig` or `MemoryConfig`. You can overwrite this for a specific object by specifying `expiry` for `setObject`
|
||||
|
||||
@@ -138,3 +138,65 @@ public extension AsyncStorage {
|
||||
return storage
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||
public extension AsyncStorage {
|
||||
func entry(forKey key: Key) async throws -> Entry<Value> {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
entry(forKey: key) {
|
||||
continuation.resume(with: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeObject(forKey key: Key) async throws {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
removeObject(forKey: key) {
|
||||
continuation.resume(with: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setObject(
|
||||
_ object: Value,
|
||||
forKey key: Key,
|
||||
expiry: Expiry? = nil) async throws {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
setObject(object, forKey: key, expiry: expiry) {
|
||||
continuation.resume(with: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeAll() async throws {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
removeAll {
|
||||
continuation.resume(with: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeExpiredObjects() async throws {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
removeExpiredObjects {
|
||||
continuation.resume(with: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func object(forKey key: Key) async throws -> Value {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
object(forKey: key) {
|
||||
continuation.resume(with: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func objectExists(forKey key: Key) async throws -> Bool {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
objectExists(forKey: key) {
|
||||
continuation.resume(with: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,23 @@ extension XCTestCase {
|
||||
try closure()
|
||||
}
|
||||
|
||||
func given(_ description: String, closure: () async throws -> Void) async rethrows {
|
||||
try await closure()
|
||||
}
|
||||
|
||||
func when(_ description: String, closure: () throws -> Void) rethrows {
|
||||
try closure()
|
||||
}
|
||||
|
||||
func when(_ description: String, closure: () async throws -> Void) async rethrows {
|
||||
try await closure()
|
||||
}
|
||||
|
||||
func then(_ description: String, closure: () throws -> Void) rethrows {
|
||||
try closure()
|
||||
}
|
||||
|
||||
func then(_ description: String, closure: () async throws -> Void) async rethrows {
|
||||
try await closure()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,13 @@ final class AsyncStorageTests: XCTestCase {
|
||||
wait(for: [expectation], timeout: 1)
|
||||
}
|
||||
|
||||
func testSetObject() async throws {
|
||||
try await storage.setObject(user, forKey: "user")
|
||||
let cachedUser = try await storage.object(forKey: "user")
|
||||
|
||||
XCTAssertEqual(cachedUser, self.user)
|
||||
}
|
||||
|
||||
func testRemoveAll() {
|
||||
let intStorage = storage.transform(transformer: TransformerFactory.forCodable(ofType: Int.self))
|
||||
let expectation = self.expectation(description: #function)
|
||||
@@ -62,4 +69,24 @@ final class AsyncStorageTests: XCTestCase {
|
||||
|
||||
wait(for: [expectation], timeout: 1)
|
||||
}
|
||||
|
||||
func testRemoveAll() async throws {
|
||||
let intStorage = storage.transform(transformer: TransformerFactory.forCodable(ofType: Int.self))
|
||||
try await given("add a lot of objects") {
|
||||
for i in 0 ..< 100 {
|
||||
try await intStorage.setObject(i, forKey: "key-\(i)")
|
||||
}
|
||||
}
|
||||
|
||||
try await when("remove all") {
|
||||
try await intStorage.removeAll()
|
||||
}
|
||||
|
||||
await then("all are removed") {
|
||||
do {
|
||||
_ = try await intStorage.objectExists(forKey: "key-99")
|
||||
XCTFail()
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user