Skip to content

Commit

Permalink
Perform data cache lookups on a dedicated queue
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jun 28, 2018
1 parent 50f93f7 commit 371eb72
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 39 deletions.
6 changes: 1 addition & 5 deletions Demo/Sources/CustomCacheDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ final class CustomCacheDemoViewController: BasicDemoViewController {
}

extension DFCache: DataCaching {
public func cachedData(for key: String, _ completion: @escaping (Data?) -> Void) -> Cancellable {
class NoOpCancellable: Cancellable {
func cancel() {}
}
public func cachedData(for key: String, _ completion: @escaping (Data?) -> Void) {
self.cachedData(forKey: key, completion: completion)
return NoOpCancellable()
}

public func storeData(_ data: Data, for key: String) {
Expand Down
6 changes: 1 addition & 5 deletions Documentation/Guides/Third Party Libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ Nuke can be used with any third party caching library.

```swift
extension DFCache: DataCaching {
public func cachedData(for key: String, _ completion: @escaping (Data?) -> Void) -> Cancellable {
class NoOpCancellable: Cancellable {
func cancel() {}
}
public func cachedData(for key: String, _ completion: @escaping (Data?) -> Void) {
self.cachedData(forKey: key, completion: completion)
return NoOpCancellable()
}

public func storeData(_ data: Data, for key: String) {
Expand Down
11 changes: 4 additions & 7 deletions Sources/DataCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
/// - warning: The implementation must be thread safe.
public protocol DataCaching {
/// Retrieves data from cache for the given key.
func cachedData(for key: String, _ completion: @escaping (Data?) -> Void) -> Cancellable
func cachedData(for key: String, _ completion: @escaping (Data?) -> Void)

/// Stores data for the given key.
func storeData(_ data: Data, for key: String)
Expand Down Expand Up @@ -142,13 +142,10 @@ public final class DataCache: DataCaching {

/// Retrieves data for the given key. The completion will be called
/// syncrhonously if there is no cached data for the given key.
@discardableResult
public func cachedData(for key: Key, _ completion: @escaping (Data?) -> Void) -> Cancellable {
let work = DispatchWorkItem { [weak self] in
completion(self?._getData(for: key))
public func cachedData(for key: Key, _ completion: @escaping (Data?) -> Void) {
_rqueue.async {
completion(self._getData(for: key))
}
_rqueue.async(execute: work)
return work
}

/// Stores data for the given key. The method returns instantly and the data
Expand Down
35 changes: 21 additions & 14 deletions Sources/ImagePipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public /* final */ class ImagePipeline {
/// Data loading queue. Default maximum concurrent task count is 6.
public var dataLoadingQueue = OperationQueue()

/// Data cache used by the pipeline.
public var dataCache: DataCaching?

/// Data caching queue. Default maximum concurrent task count is 2.
public var dataCachingQueue = OperationQueue()

/// Default implementation uses shared `ImageDecoderRegistry` to create
/// a decoder that matches the context.
internal var imageDecoder: (ImageDecodingContext) -> ImageDecoding = {
Expand All @@ -145,9 +151,6 @@ public /* final */ class ImagePipeline {
/// Image decoding queue. Default maximum concurrent task count is 1.
public var imageDecodingQueue = OperationQueue()

/// Data cache used by the pipeline.
public var dataCache: DataCaching?

/// This is here just for backward compatibility with `Loader`.
internal var imageProcessor: (Image, ImageRequest) -> AnyImageProcessor? = { $1.processor }

Expand Down Expand Up @@ -206,6 +209,7 @@ public /* final */ class ImagePipeline {
self.imageCache = imageCache

self.dataLoadingQueue.maxConcurrentOperationCount = 6
self.dataCachingQueue.maxConcurrentOperationCount = 2
self.imageDecodingQueue.maxConcurrentOperationCount = 1
self.imageProcessingQueue.maxConcurrentOperationCount = 2
}
Expand Down Expand Up @@ -372,19 +376,22 @@ public /* final */ class ImagePipeline {

session.metrics.checkDiskCacheStartDate = Date()

// Disk cache lookup
let task = cache.cachedData(for: key) { [weak self, weak session] data in
guard let session = session else { return }
session.metrics.checkDiskCacheEndDate = Date()
self?.queue.async {
if let data = data {
self?._decodeFinalImage(for: session, data: data)
} else {
self?._loadData(for: session)
let operation = Operation(starter: { [weak self, weak session] finish in
cache.cachedData(for: key) { data in
guard let session = session else { finish(); return }
session.metrics.checkDiskCacheEndDate = Date()
self?.queue.async {
finish()
if let data = data {
self?._decodeFinalImage(for: session, data: data)
} else {
self?._loadData(for: session)
}
}
}
}
session.token.register { task.cancel() }
})

_session(session, enqueue: operation, on: configuration.dataCachingQueue)
}

private func _loadData(for session: ImageLoadingSession) {
Expand Down
8 changes: 0 additions & 8 deletions Sources/Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,6 @@ internal struct Printer {

// MARK: - Misc

final class NoOpCancellable: Cancellable {
func cancel() {
return // Do nothing
}
}

extension DispatchWorkItem: Cancellable {}

struct TaskMetrics {
var startDate: Date? = nil
var endDate: Date? = nil
Expand Down

0 comments on commit 371eb72

Please sign in to comment.