Add ofType parameter

This commit is contained in:
Khoa Pham
2017-09-18 09:17:46 +02:00
parent c52649b02b
commit df84c4061b
9 changed files with 23 additions and 23 deletions
+2 -2
View File
@@ -13,7 +13,7 @@ public class AsyncStorage {
}
extension AsyncStorage: AsyncStorageAware {
public func entry<T>(forKey key: String, completion: @escaping (Result<Entry<T>>) -> Void) {
public func entry<T>(ofType type: T.Type, forKey key: String, completion: @escaping (Result<Entry<T>>) -> 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<T>
let anEntry = try self.internalStorage.entry(ofType: type, forKey: key)
completion(Result.value(anEntry))
} catch {
completion(Result.error(error))
@@ -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<T: Codable>(forKey key: String, completion: @escaping (Result<T>) -> Void)
func object<T: Codable>(ofType type: T.Type, forKey key: String, completion: @escaping (Result<T>) -> 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<T>(forKey key: String, completion: @escaping (Result<Entry<T>>) -> Void)
func entry<T>(ofType type: T.Type, forKey key: String, completion: @escaping (Result<Entry<T>>) -> Void)
/**
Removes the object by the given key.
@@ -61,8 +61,8 @@ public protocol AsyncStorageAware: class {
}
public extension AsyncStorageAware {
func object<T: Codable>(forKey key: String, completion: @escaping (Result<T>) -> Void) {
entry(forKey: key, completion: { (result: Result<Entry<T>>) in
func object<T: Codable>(ofType type: T.Type, forKey key: String, completion: @escaping (Result<T>) -> Void) {
entry(ofType: type, forKey: key, completion: { (result: Result<Entry<T>>) in
completion(result.map({ entry in
return entry.object
}))
@@ -72,7 +72,7 @@ public extension AsyncStorageAware {
func existsObject<T: Codable>(ofType type: T.Type,
forKey key: String,
completion: @escaping (Result<Bool>) -> Void) {
object(forKey: key, completion: { (result: Result<T>) in
object(ofType: type, forKey: key, completion: { (result: Result<T>) in
completion(result.map({ _ in
return true
}))
+1 -1
View File
@@ -35,7 +35,7 @@ final class DiskStorage {
}
extension DiskStorage: StorageAware {
func entry<T: Codable>(forKey key: String) throws -> Entry<T> {
func entry<T: Codable>(ofType type: T.Type, forKey key: String) throws -> Entry<T> {
let filePath = makeFilePath(for: key)
let data = try Data(contentsOf: URL(fileURLWithPath: filePath))
let attributes = try fileManager.attributesOfItem(atPath: filePath)
+3 -3
View File
@@ -12,11 +12,11 @@ class HybridStorage {
}
extension HybridStorage: StorageAware {
func entry<T: Codable>(forKey key: String) throws -> Entry<T> {
func entry<T: Codable>(ofType type: T.Type, forKey key: String) throws -> Entry<T> {
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<T>
let entry = try diskStorage.entry(ofType: type, forKey: key)
// set back to memoryStorage
memoryStorage.setObject(entry.object, forKey: key)
return entry
+1 -1
View File
@@ -17,7 +17,7 @@ final class MemoryStorage {
}
extension MemoryStorage: StorageAware {
func entry<T: Codable>(forKey key: String) throws -> Entry<T> {
func entry<T: Codable>(ofType type: T.Type, forKey key: String) throws -> Entry<T> {
guard let capsule = cache.object(forKey: key as NSString) else {
throw StorageError.notFound
}
+2 -2
View File
@@ -42,8 +42,8 @@ public class Storage {
}
extension Storage: StorageAware {
public func entry<T: Codable>(forKey key: String) throws -> Entry<T> {
return try sync.entry(forKey: key) as Entry<T>
public func entry<T: Codable>(ofType type: T.Type, forKey key: String) throws -> Entry<T> {
return try sync.entry(ofType: type, forKey: key)
}
public func removeObject(forKey key: String) throws {
+5 -5
View File
@@ -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<T: Codable>(forKey key: String) throws -> T
func object<T: Codable>(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<T: Codable>(forKey key: String) throws -> Entry<T>
func entry<T: Codable>(ofType type: T.Type, forKey key: String) throws -> Entry<T>
/**
Removes the object by the given key.
@@ -48,13 +48,13 @@ public protocol StorageAware {
}
public extension StorageAware {
func object<T: Codable>(forKey key: String) throws -> T {
return try entry(forKey: key).object
func object<T: Codable>(ofType type: T.Type, forKey key: String) throws -> T {
return try entry(ofType: type, forKey: key).object
}
func existsObject<T: Codable>(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
+2 -2
View File
@@ -13,10 +13,10 @@ public class SyncStorage {
}
extension SyncStorage: StorageAware {
public func entry<T: Codable>(forKey key: String) throws -> Entry<T> {
public func entry<T: Codable>(ofType type: T.Type, forKey key: String) throws -> Entry<T> {
var entry: Entry<T>!
try serialQueue.sync {
entry = try internalStorage.entry(forKey: key) as Entry<T>
entry = try internalStorage.entry(ofType: type, forKey: key)
}
return entry
@@ -12,8 +12,8 @@ final class TypeWrapperStorage {
}
extension TypeWrapperStorage: StorageAware {
public func entry<T: Codable>(forKey key: String) throws -> Entry<T> {
let wrapperEntry = try internalStorage.entry(forKey: key) as Entry<TypeWrapper<T>>
public func entry<T: Codable>(ofType type: T.Type, forKey key: String) throws -> Entry<T> {
let wrapperEntry = try internalStorage.entry(ofType: TypeWrapper<T>.self, forKey: key)
return Entry(object: wrapperEntry.object.object, expiry: wrapperEntry.expiry)
}