mirror of
https://github.com/hyperoslo/Cache.git
synced 2026-04-07 19:17:36 +00:00
Refactor basic hybrid cache
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ class CacheSpec: QuickSpec {
|
||||
}
|
||||
|
||||
describe("#init") {
|
||||
|
||||
it("sets a name") {
|
||||
expect(cache.name).to(equal(name))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user