Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option for only from memory cache or refresh #806

Merged
merged 1 commit into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ open class ImageCache {
options.callbackDispatchQueue.safeAsync {
completionHandler(image, .memory)
}
} else if options.fromMemoryCacheOrRefresh { // Only allows to get images from memory cache.
options.callbackDispatchQueue.safeAsync {
completionHandler(nil, .none)
}
} else {
var sSelf: ImageCache! = self
block = DispatchWorkItem(block: {
Expand Down
11 changes: 11 additions & 0 deletions Sources/KingfisherOptionsInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public enum KingfisherOptionsInfoItem {

/// If set, `Kingfisher` will ignore the cache and try to fire a download task for the resource.
case forceRefresh

/// If set, `Kingfisher` will try to retrieve the image from memory cache first. If the image is not in memory
/// cache, then it will ignore the disk cache but download the image again from network. This is useful when
/// you want to display a changeable image behind the same url, while avoiding download it again and again.
case fromMemoryCacheOrRefresh

/// If set, setting the image to an image view will happen with transition even when retrieved from cache.
/// See `Transition` option for more.
Expand Down Expand Up @@ -148,6 +153,7 @@ func <== (lhs: KingfisherOptionsInfoItem, rhs: KingfisherOptionsInfoItem) -> Boo
case (.transition(_), .transition(_)): return true
case (.downloadPriority(_), .downloadPriority(_)): return true
case (.forceRefresh, .forceRefresh): return true
case (.fromMemoryCacheOrRefresh, .fromMemoryCacheOrRefresh): return true
case (.forceTransition, .forceTransition): return true
case (.cacheMemoryOnly, .cacheMemoryOnly): return true
case (.onlyFromCache, .onlyFromCache): return true
Expand Down Expand Up @@ -232,6 +238,11 @@ public extension Collection where Iterator.Element == KingfisherOptionsInfoItem
public var forceRefresh: Bool {
return contains{ $0 <== .forceRefresh }
}

/// Whether an image should be got only from memory cache or download.
public var fromMemoryCacheOrRefresh: Bool {
return contains{ $0 <== .fromMemoryCacheOrRefresh }
}

/// Whether the transition should always happen or not.
public var forceTransition: Bool {
Expand Down
56 changes: 56 additions & 0 deletions Tests/KingfisherTests/KingfisherManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,62 @@ class KingfisherManagerTests: XCTestCase {

waitForExpectations(timeout: 5, handler: nil)
}

func testImageShouldOnlyFromMemoryCacheOrRefreshCanBeGotFromMemory() {
let expectation = self.expectation(description: "only from memory cache or refresh")
let URLString = testKeys[0]
_ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)

let url = URL(string: URLString)!

manager.retrieveImage(with: url, options: [.fromMemoryCacheOrRefresh], progressBlock: nil) {
image, _, type, _ in
// Can download and cache normally
XCTAssertNotNil(image)
XCTAssertEqual(type, .none)

// Can still be got from memory even when disk cache cleared.
self.manager.cache.clearDiskCache {
self.manager.retrieveImage(with: url, options: [.fromMemoryCacheOrRefresh], progressBlock: nil) {
image, _, type, _ in
XCTAssertNotNil(image)
XCTAssertEqual(type, .memory)

expectation.fulfill()
}
}
}
waitForExpectations(timeout: 5, handler: nil)
}

func testImageShouldOnlyFromMemoryCacheOrRefreshCanRefreshIfNotInMemory() {
let expectation = self.expectation(description: "only from memory cache or refresh")
let URLString = testKeys[0]
_ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)

let url = URL(string: URLString)!

manager.retrieveImage(with: url, options: [.fromMemoryCacheOrRefresh], progressBlock: nil) {
image, _, type, _ in
// Can download and cache normally
XCTAssertNotNil(image)
XCTAssertEqual(type, .none)
XCTAssertEqual(self.manager.cache.imageCachedType(forKey: URLString), .memory)

self.manager.cache.clearMemoryCache()
XCTAssertEqual(self.manager.cache.imageCachedType(forKey: URLString), .disk)

self.manager.retrieveImage(with: url, options: [.fromMemoryCacheOrRefresh], progressBlock: nil) {
image, _, type, _ in
XCTAssertNotNil(image)
XCTAssertEqual(type, .none)
XCTAssertEqual(self.manager.cache.imageCachedType(forKey: URLString), .memory)

expectation.fulfill()
}
}
waitForExpectations(timeout: 5, handler: nil)
}
}

class SimpleProcessor: ImageProcessor {
Expand Down
3 changes: 3 additions & 0 deletions Tests/KingfisherTests/KingfisherOptionsInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class KingfisherOptionsInfoTests: XCTestCase {

XCTAssertEqual(options.downloadPriority, URLSessionTask.defaultPriority)
XCTAssertFalse(options.forceRefresh)
XCTAssertFalse(options.fromMemoryCacheOrRefresh)
XCTAssertFalse(options.cacheMemoryOnly)
XCTAssertFalse(options.backgroundDecode)
XCTAssertEqual(options.callbackDispatchQueue.label, DispatchQueue.main.label)
Expand Down Expand Up @@ -84,6 +85,7 @@ class KingfisherOptionsInfoTests: XCTestCase {
.transition(transition),
.downloadPriority(0.8),
.forceRefresh,
.fromMemoryCacheOrRefresh,
.cacheMemoryOnly,
.onlyFromCache,
.backgroundDecode,
Expand All @@ -108,6 +110,7 @@ class KingfisherOptionsInfoTests: XCTestCase {

XCTAssertEqual(options.downloadPriority, 0.8)
XCTAssertTrue(options.forceRefresh)
XCTAssertTrue(options.fromMemoryCacheOrRefresh)
XCTAssertTrue(options.cacheMemoryOnly)
XCTAssertTrue(options.onlyFromCache)
XCTAssertTrue(options.backgroundDecode)
Expand Down