Refactor basic hybrid cache

This commit is contained in:
Vadym Markov
2017-04-27 23:36:39 +02:00
parent 8fb3a2b49e
commit 1ea0177426
5 changed files with 12 additions and 12 deletions
+4 -3
View File
@@ -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<T: Cachable>(_ key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) {
func add<T: Cachable>(_ 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<T: Cachable>(_ key: String, completion: @escaping (_ object: T?) -> Void) {
func object<T: Cachable>(forKey key: String, completion: @escaping (_ object: T?) -> Void) {
frontStorage.object(key) { [weak self] (object: T?) in
if let object = object {
completion(object)
+2 -2
View File
@@ -18,7 +18,7 @@ public final class Cache<T: Cachable>: 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<T: Cachable>: BasicHybridCache {
*/
public func object(_ key: String, completion: @escaping (_ object: T?) -> Void) {
super.object(key, completion: completion)
super.object(forKey: key, completion: completion)
}
}
+4 -4
View File
@@ -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<T: Cachable>(_ key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) {
super.add(key, object: object, expiry: expiry, completion: completion)
public func add<T: Cachable>(_ 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<T: Cachable>(_ key: String, completion: @escaping (_ object: T?) -> Void) {
super.object(key, completion: completion)
public func object<T: Cachable>(_ key: String, completion: @escaping (_ object: T?) -> Void) {
super.object(forKey: key, completion: completion)
}
}
+2 -2
View File
@@ -31,7 +31,7 @@ public struct SyncHybridCache {
public func add<T: Cachable>(_ 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()
}
-1
View File
@@ -20,7 +20,6 @@ class CacheSpec: QuickSpec {
}
describe("#init") {
it("sets a name") {
expect(cache.name).to(equal(name))
}