From 1ea01774267aa46e2e41e375b3cc176e0457e119 Mon Sep 17 00:00:00 2001 From: Vadym Markov Date: Thu, 27 Apr 2017 23:36:39 +0200 Subject: [PATCH] Refactor basic hybrid cache --- Source/Shared/BasicHybridCache.swift | 7 ++++--- Source/Shared/Cache.swift | 4 ++-- Source/Shared/HybridCache.swift | 8 ++++---- Source/Shared/Library/SyncHybridCache.swift | 4 ++-- Tests/iOS/Specs/CacheSpec.swift | 1 - 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Source/Shared/BasicHybridCache.swift b/Source/Shared/BasicHybridCache.swift index 23b2b4a..71d2df9 100644 --- a/Source/Shared/BasicHybridCache.swift +++ b/Source/Shared/BasicHybridCache.swift @@ -69,12 +69,13 @@ public class BasicHybridCache: NSObject { /** Adds passed object to the front and back cache storages. - - Parameter key: Unique key to identify the object in the cache - Parameter object: Object that needs to be cached + - Parameter key: Unique key to identify the object in the cache - Parameter expiry: Expiration date for the cached object - Parameter completion: Completion closure to be called when the task is done */ - func add(_ key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) { + func add(_ object: T, forKey key: String, + expiry: Expiry? = nil, completion: (() -> Void)? = nil) { let expiry = expiry ?? config.expiry frontStorage.add(key, object: object, expiry: expiry) { [weak self] in @@ -95,7 +96,7 @@ public class BasicHybridCache: NSObject { - Parameter key: Unique key to identify the object in the cache - Parameter completion: Completion closure returns object or nil */ - func object(_ key: String, completion: @escaping (_ object: T?) -> Void) { + func object(forKey key: String, completion: @escaping (_ object: T?) -> Void) { frontStorage.object(key) { [weak self] (object: T?) in if let object = object { completion(object) diff --git a/Source/Shared/Cache.swift b/Source/Shared/Cache.swift index eafca6c..11da216 100644 --- a/Source/Shared/Cache.swift +++ b/Source/Shared/Cache.swift @@ -18,7 +18,7 @@ public final class Cache: BasicHybridCache { - Parameter completion: Completion closure to be called when the task is done */ public func add(_ key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) { - super.add(key, object: object, expiry: expiry, completion: completion) + super.add(object, forKey: key, expiry: expiry, completion: completion) } /** @@ -29,6 +29,6 @@ public final class Cache: BasicHybridCache { */ public func object(_ key: String, completion: @escaping (_ object: T?) -> Void) { - super.object(key, completion: completion) + super.object(forKey: key, completion: completion) } } diff --git a/Source/Shared/HybridCache.swift b/Source/Shared/HybridCache.swift index 706bf2e..77c78dd 100644 --- a/Source/Shared/HybridCache.swift +++ b/Source/Shared/HybridCache.swift @@ -13,8 +13,8 @@ public class HybridCache: BasicHybridCache { - Parameter expiry: Expiration date for the cached object - Parameter completion: Completion closure to be called when the task is done */ - public override func add(_ key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) { - super.add(key, object: object, expiry: expiry, completion: completion) + public func add(_ key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) { + super.add(object, forKey: key, expiry: expiry, completion: completion) } /** @@ -23,7 +23,7 @@ public class HybridCache: BasicHybridCache { - Parameter key: Unique key to identify the object in the cache - Parameter completion: Completion closure returns object or nil */ - public override func object(_ key: String, completion: @escaping (_ object: T?) -> Void) { - super.object(key, completion: completion) + public func object(_ key: String, completion: @escaping (_ object: T?) -> Void) { + super.object(forKey: key, completion: completion) } } diff --git a/Source/Shared/Library/SyncHybridCache.swift b/Source/Shared/Library/SyncHybridCache.swift index 27b9577..fe835fb 100644 --- a/Source/Shared/Library/SyncHybridCache.swift +++ b/Source/Shared/Library/SyncHybridCache.swift @@ -31,7 +31,7 @@ public struct SyncHybridCache { public func add(_ key: String, object: T, expiry: Expiry? = nil) { let semaphore = DispatchSemaphore(value: 0) - cache.add(key, object: object, expiry: expiry) { + cache.add(object, forKey: key, expiry: expiry) { semaphore.signal() } @@ -49,7 +49,7 @@ public struct SyncHybridCache { let semaphore = DispatchSemaphore(value: 0) - cache.object(key) { (object: T?) in + cache.object(forKey: key) { (object: T?) in result = object semaphore.signal() } diff --git a/Tests/iOS/Specs/CacheSpec.swift b/Tests/iOS/Specs/CacheSpec.swift index 27f665d..cd2f767 100644 --- a/Tests/iOS/Specs/CacheSpec.swift +++ b/Tests/iOS/Specs/CacheSpec.swift @@ -20,7 +20,6 @@ class CacheSpec: QuickSpec { } describe("#init") { - it("sets a name") { expect(cache.name).to(equal(name)) }