Skip to content

Commit

Permalink
Merge pull request onevcat#456 from DangerCove/default-file-extension
Browse files Browse the repository at this point in the history
Add option to supply default path extension
  • Loading branch information
onevcat authored Sep 27, 2016
2 parents 1aff4bd + 440a5b5 commit 97e1f07
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Sources/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ open class ImageCache {

///The disk cache location.
open let diskCachePath: String

/// The default file extension appended to cached files.
open var pathExtension: String?

/// The longest time duration in second of the cache being stored in disk.
/// Default is 1 week (60 * 60 * 24 * 7 seconds).
Expand Down Expand Up @@ -621,7 +624,7 @@ extension ImageCache {

// MARK: - Internal Helper
extension ImageCache {

func diskImage(forComputedKey key: String, serializer: CacheSerializer, options: KingfisherOptionsInfo) -> Image? {
if let data = diskImageData(forComputedKey: key) {
return serializer.image(with: data, options: options)
Expand All @@ -636,6 +639,9 @@ extension ImageCache {
}

func cacheFileName(forComputedKey key: String) -> String {
if let ext = self.pathExtension {
return (key.kf.md5 as NSString).appendingPathExtension(ext)!
}
return key.kf.md5
}
}
Expand Down
45 changes: 45 additions & 0 deletions Tests/KingfisherTests/ImageCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,51 @@ class ImageCacheTests: XCTestCase {
XCTAssertFalse(exists)
}

func testCachedFileWithCustomPathExtensionExists() {
cache.pathExtension = "jpg"
let expectation = self.expectation(description: "cache with custom path extension does contain image")

let URLString = testKeys[0]
let url = URL(string: URLString)!

let exists = cache.isImageCached(forKey: url.cacheKey).cached
XCTAssertFalse(exists)

cache.retrieveImage(forKey: URLString, options: nil, completionHandler: { (image, type) -> () in
XCTAssertNil(image, "Should not be cached yet")

XCTAssertEqual(type, .none)

self.cache.store(testImage, forKey: URLString, toDisk: true) { () -> () in
self.cache.retrieveImage(forKey: URLString, options: nil, completionHandler: { (image, type) -> () in
XCTAssertNotNil(image, "Should be cached (memory or disk)")
XCTAssertEqual(type, .memory)

let exists = self.cache.isImageCached(forKey: url.cacheKey).cached
XCTAssertTrue(exists, "Image should exist in the cache (memory or disk)")

self.cache.clearMemoryCache()
self.cache.retrieveImage(forKey: URLString, options: nil, completionHandler: { (image, type) -> () in
XCTAssertNotNil(image, "Should be cached (disk)")
XCTAssertEqual(type, CacheType.disk)

let exists = self.cache.isImageCached(forKey: url.cacheKey).cached
XCTAssertTrue(exists, "Image should exist in the cache (disk)")

let cachePath = self.cache.cachePath(forKey: url.cacheKey)
let hasExtension = cachePath.hasSuffix(".jpg")
XCTAssert(hasExtension, "Should have .jpg file extension")

expectation.fulfill()
})
})
}
})

waitForExpectations(timeout: 5, handler: nil)
}


func testCachedImageIsFetchedSyncronouslyFromTheMemoryCache() {
cache.store(testImage, forKey: testKeys[0], toDisk: false) { () -> () in
// do nothing
Expand Down

0 comments on commit 97e1f07

Please sign in to comment.