From 0fb5a949315096da7830305b77768383da54228b Mon Sep 17 00:00:00 2001 From: leotu Date: Wed, 12 Jan 2022 14:10:49 +0800 Subject: [PATCH] Avoid loading disk content to memory when calling existsObject --- Source/Shared/Storage/AsyncStorage.swift | 6 ++++++ Source/Shared/Storage/DiskStorage.swift | 5 +++++ Source/Shared/Storage/HybridStorage.swift | 4 ++++ Source/Shared/Storage/MemoryStorage.swift | 4 ++++ Source/Shared/Storage/Storage.swift | 4 ++++ Source/Shared/Storage/StorageAware.swift | 16 ++++++++-------- Source/Shared/Storage/SyncStorage.swift | 6 ++++++ Tests/iOS/Tests/Storage/SyncStorageTests.swift | 4 ++-- 8 files changed, 39 insertions(+), 10 deletions(-) diff --git a/Source/Shared/Storage/AsyncStorage.swift b/Source/Shared/Storage/AsyncStorage.swift index 032ea08a..aaf2132c 100644 --- a/Source/Shared/Storage/AsyncStorage.swift +++ b/Source/Shared/Storage/AsyncStorage.swift @@ -65,6 +65,12 @@ extension AsyncStorage { } } } + + public func existsObject(forKey key: Key) -> Bool { + serialQueue.sync { + return innerStorage.existsObject(forKey: key) + } + } public func removeAll(completion: @escaping (Result<(), Error>) -> Void) { serialQueue.async { [weak self] in diff --git a/Source/Shared/Storage/DiskStorage.swift b/Source/Shared/Storage/DiskStorage.swift index ca4db9a1..f7b74101 100644 --- a/Source/Shared/Storage/DiskStorage.swift +++ b/Source/Shared/Storage/DiskStorage.swift @@ -86,6 +86,11 @@ extension DiskStorage: StorageAware { _ = fileManager.createFile(atPath: filePath, contents: data, attributes: nil) try fileManager.setAttributes([.modificationDate: expiry.date], ofItemAtPath: filePath) } + + public func existsObject(forKey key: Key) -> Bool { + let filePath = makeFilePath(for: key) + return fileManager.fileExists(atPath: filePath) + } public func removeObject(forKey key: Key) throws { let filePath = makeFilePath(for: key) diff --git a/Source/Shared/Storage/HybridStorage.swift b/Source/Shared/Storage/HybridStorage.swift index 385980fe..9458b6c7 100644 --- a/Source/Shared/Storage/HybridStorage.swift +++ b/Source/Shared/Storage/HybridStorage.swift @@ -52,6 +52,10 @@ extension HybridStorage: StorageAware { notifyStorageObservers(about: .remove(key: key)) } + public func existsObject(forKey key: Key) -> Bool { + return memoryStorage.existsObject(forKey: key) || diskStorage.existsObject(forKey: key) + } + public func setObject(_ object: Value, forKey key: Key, expiry: Expiry? = nil) throws { var keyChange: KeyChange? diff --git a/Source/Shared/Storage/MemoryStorage.swift b/Source/Shared/Storage/MemoryStorage.swift index 1adc46ff..1c76692d 100644 --- a/Source/Shared/Storage/MemoryStorage.swift +++ b/Source/Shared/Storage/MemoryStorage.swift @@ -44,6 +44,10 @@ extension MemoryStorage { cache.setObject(capsule, forKey: WrappedKey(key)) keys.insert(key) } + + public func existsObject(forKey key: Key) -> Bool { + return cache.object(forKey: WrappedKey(key)) != nil + } public func removeAll() { cache.removeAllObjects() diff --git a/Source/Shared/Storage/Storage.swift b/Source/Shared/Storage/Storage.swift index e2d7c69c..2eda4cd3 100644 --- a/Source/Shared/Storage/Storage.swift +++ b/Source/Shared/Storage/Storage.swift @@ -62,6 +62,10 @@ extension Storage: StorageAware { public func setObject(_ object: Value, forKey key: Key, expiry: Expiry? = nil) throws { try self.syncStorage.setObject(object, forKey: key, expiry: expiry) } + + public func existsObject(forKey key: Key) -> Bool { + return self.syncStorage.existsObject(forKey: key) + } public func removeAll() throws { try self.syncStorage.removeAll() diff --git a/Source/Shared/Storage/StorageAware.swift b/Source/Shared/Storage/StorageAware.swift index cbbb8ce2..8729991c 100644 --- a/Source/Shared/Storage/StorageAware.swift +++ b/Source/Shared/Storage/StorageAware.swift @@ -71,14 +71,14 @@ public extension StorageAware { return try entry(forKey: key).object } - func existsObject(forKey key: Key) throws -> Bool { - do { - let _: Value = try object(forKey: key) - return true - } catch { - return false - } - } +// func existsObject(forKey key: Key) throws -> Bool { +// do { +// let _: Value = try object(forKey: key) +// return true +// } catch { +// return false +// } +// } func isExpiredObject(forKey key: Key) throws -> Bool { do { diff --git a/Source/Shared/Storage/SyncStorage.swift b/Source/Shared/Storage/SyncStorage.swift index 51d15f0e..64474689 100644 --- a/Source/Shared/Storage/SyncStorage.swift +++ b/Source/Shared/Storage/SyncStorage.swift @@ -49,6 +49,12 @@ extension SyncStorage: StorageAware { try innerStorage.setObject(object, forKey: key, expiry: expiry) } } + + public func existsObject(forKey key: Key) -> Bool { + serialQueue.sync { + return innerStorage.existsObject(forKey: key) + } + } public func removeAll() throws { try serialQueue.sync { diff --git a/Tests/iOS/Tests/Storage/SyncStorageTests.swift b/Tests/iOS/Tests/Storage/SyncStorageTests.swift index 4c055b33..182c1a3a 100644 --- a/Tests/iOS/Tests/Storage/SyncStorageTests.swift +++ b/Tests/iOS/Tests/Storage/SyncStorageTests.swift @@ -40,8 +40,8 @@ final class SyncStorageTests: XCTestCase { try intStorage.removeAll() } - try then("all are removed") { - XCTAssertFalse(try intStorage.existsObject(forKey: "key-99")) + then("all are removed") { + XCTAssertFalse(intStorage.existsObject(forKey: "key-99")) } } }