From df84c4061bc602fa35066ca4f2372ab8dfaefd76 Mon Sep 17 00:00:00 2001 From: Khoa Pham Date: Mon, 18 Sep 2017 09:17:46 +0200 Subject: [PATCH] Add ofType parameter --- Source/Shared/Storage/AsyncStorage.swift | 4 ++-- Source/Shared/Storage/AsyncStorageAware.swift | 10 +++++----- Source/Shared/Storage/DiskStorage.swift | 2 +- Source/Shared/Storage/HybridStorage.swift | 6 +++--- Source/Shared/Storage/MemoryStorage.swift | 2 +- Source/Shared/Storage/Storage.swift | 4 ++-- Source/Shared/Storage/StorageAware.swift | 10 +++++----- Source/Shared/Storage/SyncStorage.swift | 4 ++-- Source/Shared/Storage/TypeWrapperStorage.swift | 4 ++-- 9 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Source/Shared/Storage/AsyncStorage.swift b/Source/Shared/Storage/AsyncStorage.swift index 26e81b6..ea75153 100644 --- a/Source/Shared/Storage/AsyncStorage.swift +++ b/Source/Shared/Storage/AsyncStorage.swift @@ -13,7 +13,7 @@ public class AsyncStorage { } extension AsyncStorage: AsyncStorageAware { - public func entry(forKey key: String, completion: @escaping (Result>) -> Void) { + public func entry(ofType type: T.Type, forKey key: String, completion: @escaping (Result>) -> Void) { serialQueue.async { [weak self] in guard let `self` = self else { completion(Result.error(StorageError.deallocated)) @@ -21,7 +21,7 @@ extension AsyncStorage: AsyncStorageAware { } do { - let anEntry = try self.internalStorage.entry(forKey: key) as Entry + let anEntry = try self.internalStorage.entry(ofType: type, forKey: key) completion(Result.value(anEntry)) } catch { completion(Result.error(error)) diff --git a/Source/Shared/Storage/AsyncStorageAware.swift b/Source/Shared/Storage/AsyncStorageAware.swift index df91e52..7cd5024 100644 --- a/Source/Shared/Storage/AsyncStorageAware.swift +++ b/Source/Shared/Storage/AsyncStorageAware.swift @@ -10,14 +10,14 @@ public protocol AsyncStorageAware: class { - Parameter key: Unique key to identify the object in the cache. - Parameter completion: Triggered until the operation completes. */ - func object(forKey key: String, completion: @escaping (Result) -> Void) + func object(ofType type: T.Type, forKey key: String, completion: @escaping (Result) -> Void) /** Get cache entry which includes object with metadata. - Parameter key: Unique key to identify the object in the cache - Parameter completion: Triggered until the operation completes. */ - func entry(forKey key: String, completion: @escaping (Result>) -> Void) + func entry(ofType type: T.Type, forKey key: String, completion: @escaping (Result>) -> Void) /** Removes the object by the given key. @@ -61,8 +61,8 @@ public protocol AsyncStorageAware: class { } public extension AsyncStorageAware { - func object(forKey key: String, completion: @escaping (Result) -> Void) { - entry(forKey: key, completion: { (result: Result>) in + func object(ofType type: T.Type, forKey key: String, completion: @escaping (Result) -> Void) { + entry(ofType: type, forKey: key, completion: { (result: Result>) in completion(result.map({ entry in return entry.object })) @@ -72,7 +72,7 @@ public extension AsyncStorageAware { func existsObject(ofType type: T.Type, forKey key: String, completion: @escaping (Result) -> Void) { - object(forKey: key, completion: { (result: Result) in + object(ofType: type, forKey: key, completion: { (result: Result) in completion(result.map({ _ in return true })) diff --git a/Source/Shared/Storage/DiskStorage.swift b/Source/Shared/Storage/DiskStorage.swift index 66c8068..3e23c31 100644 --- a/Source/Shared/Storage/DiskStorage.swift +++ b/Source/Shared/Storage/DiskStorage.swift @@ -35,7 +35,7 @@ final class DiskStorage { } extension DiskStorage: StorageAware { - func entry(forKey key: String) throws -> Entry { + func entry(ofType type: T.Type, forKey key: String) throws -> Entry { let filePath = makeFilePath(for: key) let data = try Data(contentsOf: URL(fileURLWithPath: filePath)) let attributes = try fileManager.attributesOfItem(atPath: filePath) diff --git a/Source/Shared/Storage/HybridStorage.swift b/Source/Shared/Storage/HybridStorage.swift index 97cc05a..f4aaa2c 100644 --- a/Source/Shared/Storage/HybridStorage.swift +++ b/Source/Shared/Storage/HybridStorage.swift @@ -12,11 +12,11 @@ class HybridStorage { } extension HybridStorage: StorageAware { - func entry(forKey key: String) throws -> Entry { + func entry(ofType type: T.Type, forKey key: String) throws -> Entry { do { - return try memoryStorage.entry(forKey: key) + return try memoryStorage.entry(ofType: type, forKey: key) } catch { - let entry = try diskStorage.entry(forKey: key) as Entry + let entry = try diskStorage.entry(ofType: type, forKey: key) // set back to memoryStorage memoryStorage.setObject(entry.object, forKey: key) return entry diff --git a/Source/Shared/Storage/MemoryStorage.swift b/Source/Shared/Storage/MemoryStorage.swift index 07e4456..0cbab46 100644 --- a/Source/Shared/Storage/MemoryStorage.swift +++ b/Source/Shared/Storage/MemoryStorage.swift @@ -17,7 +17,7 @@ final class MemoryStorage { } extension MemoryStorage: StorageAware { - func entry(forKey key: String) throws -> Entry { + func entry(ofType type: T.Type, forKey key: String) throws -> Entry { guard let capsule = cache.object(forKey: key as NSString) else { throw StorageError.notFound } diff --git a/Source/Shared/Storage/Storage.swift b/Source/Shared/Storage/Storage.swift index 38f3044..dde87cd 100644 --- a/Source/Shared/Storage/Storage.swift +++ b/Source/Shared/Storage/Storage.swift @@ -42,8 +42,8 @@ public class Storage { } extension Storage: StorageAware { - public func entry(forKey key: String) throws -> Entry { - return try sync.entry(forKey: key) as Entry + public func entry(ofType type: T.Type, forKey key: String) throws -> Entry { + return try sync.entry(ofType: type, forKey: key) } public func removeObject(forKey key: String) throws { diff --git a/Source/Shared/Storage/StorageAware.swift b/Source/Shared/Storage/StorageAware.swift index c1c76ad..fd27c91 100644 --- a/Source/Shared/Storage/StorageAware.swift +++ b/Source/Shared/Storage/StorageAware.swift @@ -7,14 +7,14 @@ public protocol StorageAware { - Parameter key: Unique key to identify the object in the cache - Returns: Cached object or nil if not found */ - func object(forKey key: String) throws -> T + func object(ofType type: T.Type, forKey key: String) throws -> T /** Get cache entry which includes object with metadata. - Parameter key: Unique key to identify the object in the cache - Returns: Object wrapper with metadata or nil if not found */ - func entry(forKey key: String) throws -> Entry + func entry(ofType type: T.Type, forKey key: String) throws -> Entry /** Removes the object by the given key. @@ -48,13 +48,13 @@ public protocol StorageAware { } public extension StorageAware { - func object(forKey key: String) throws -> T { - return try entry(forKey: key).object + func object(ofType type: T.Type, forKey key: String) throws -> T { + return try entry(ofType: type, forKey: key).object } func existsObject(ofType type: T.Type, forKey key: String) throws -> Bool { do { - let _: T = try object(forKey: key) + let _: T = try object(ofType: type, forKey: key) return true } catch { return false diff --git a/Source/Shared/Storage/SyncStorage.swift b/Source/Shared/Storage/SyncStorage.swift index 048f3a9..0601daf 100644 --- a/Source/Shared/Storage/SyncStorage.swift +++ b/Source/Shared/Storage/SyncStorage.swift @@ -13,10 +13,10 @@ public class SyncStorage { } extension SyncStorage: StorageAware { - public func entry(forKey key: String) throws -> Entry { + public func entry(ofType type: T.Type, forKey key: String) throws -> Entry { var entry: Entry! try serialQueue.sync { - entry = try internalStorage.entry(forKey: key) as Entry + entry = try internalStorage.entry(ofType: type, forKey: key) } return entry diff --git a/Source/Shared/Storage/TypeWrapperStorage.swift b/Source/Shared/Storage/TypeWrapperStorage.swift index 59b8ea7..6dfbe80 100644 --- a/Source/Shared/Storage/TypeWrapperStorage.swift +++ b/Source/Shared/Storage/TypeWrapperStorage.swift @@ -12,8 +12,8 @@ final class TypeWrapperStorage { } extension TypeWrapperStorage: StorageAware { - public func entry(forKey key: String) throws -> Entry { - let wrapperEntry = try internalStorage.entry(forKey: key) as Entry> + public func entry(ofType type: T.Type, forKey key: String) throws -> Entry { + let wrapperEntry = try internalStorage.entry(ofType: TypeWrapper.self, forKey: key) return Entry(object: wrapperEntry.object.object, expiry: wrapperEntry.expiry) }