Skip to content

Commit

Permalink
Avoid loading disk content to memory when calling existsObject
Browse files Browse the repository at this point in the history
  • Loading branch information
leotu committed Jan 12, 2022
1 parent 520b1af commit 0fb5a94
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 10 deletions.
6 changes: 6 additions & 0 deletions Source/Shared/Storage/AsyncStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Source/Shared/Storage/DiskStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions Source/Shared/Storage/HybridStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>?

Expand Down
4 changes: 4 additions & 0 deletions Source/Shared/Storage/MemoryStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions Source/Shared/Storage/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions Source/Shared/Storage/StorageAware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions Source/Shared/Storage/SyncStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Tests/iOS/Tests/Storage/SyncStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
}

0 comments on commit 0fb5a94

Please sign in to comment.