Skip to content

Commit

Permalink
Merge pull request onevcat#1114 from onevcat/fix/file-url-path
Browse files Browse the repository at this point in the history
Mark wrong method as deprecated and add URL based one
  • Loading branch information
onevcat authored Feb 10, 2019
2 parents 34fadec + 5cb8224 commit d40aeb7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
42 changes: 35 additions & 7 deletions Sources/Cache/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ open class ImageCache {
/// You should not use the same `name` for different caches, otherwise, the disk storage would
/// be conflicting to each other. The `name` should not be an empty string.
public convenience init(name: String) {
try! self.init(name: name, path: nil, diskCachePathClosure: nil)
try! self.init(name: name, cacheDirectoryURL: nil, diskCachePathClosure: nil)
}

/// Creates an `ImageCache` with a given `name`, cache directory `path`
Expand All @@ -223,21 +223,22 @@ open class ImageCache {
/// - name: The name of cache object. It is used to setup disk cache directories and IO queue.
/// You should not use the same `name` for different caches, otherwise, the disk storage would
/// be conflicting to each other.
/// - path: Location of cache path on disk. It will be internally pass to the initializer of `DiskStorage` as the
/// disk cache directory.
/// - cacheDirectoryURL: Location of cache directory URL on disk. It will be internally pass to the
/// initializer of `DiskStorage` as the disk cache directory. If `nil`, the cache
/// directory under user domain mask will be used.
/// - diskCachePathClosure: Closure that takes in an optional initial path string and generates
/// the final disk cache path. You could use it to fully customize your cache path.
/// - Throws: An error that happens during image cache creating, such as unable to create a directory at the given
/// path.
public convenience init(
name: String,
path: String?,
cacheDirectoryURL: URL?,
diskCachePathClosure: DiskCachePathClosure? = nil) throws
{
if name.isEmpty {
fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
}

let totalMemory = ProcessInfo.processInfo.physicalMemory
let costLimit = totalMemory / 4
let memoryStorage = MemoryStorage.Backend<Image>(config:
Expand All @@ -246,14 +247,14 @@ open class ImageCache {
var diskConfig = DiskStorage.Config(
name: name,
sizeLimit: 0,
directory: path.flatMap { URL(string: $0) }
directory: cacheDirectoryURL
)
if let closure = diskCachePathClosure {
diskConfig.cachePathBlock = closure
}
let diskStorage = try DiskStorage.Backend<Data>(config: diskConfig)
diskConfig.cachePathBlock = nil

self.init(memoryStorage: memoryStorage, diskStorage: diskStorage)
}

Expand Down Expand Up @@ -813,3 +814,30 @@ extension String {
}
}
}

extension ImageCache {

/// Creates an `ImageCache` with a given `name`, cache directory `path`
/// and a closure to modify the cache directory.
///
/// - Parameters:
/// - name: The name of cache object. It is used to setup disk cache directories and IO queue.
/// You should not use the same `name` for different caches, otherwise, the disk storage would
/// be conflicting to each other.
/// - path: Location of cache URL on disk. It will be internally pass to the initializer of `DiskStorage` as the
/// disk cache directory.
/// - diskCachePathClosure: Closure that takes in an optional initial path string and generates
/// the final disk cache path. You could use it to fully customize your cache path.
/// - Throws: An error that happens during image cache creating, such as unable to create a directory at the given
/// path.
@available(*, deprecated, message: "Use `init(name:cacheDirectoryURL:diskCachePathClosure:)` instead",
renamed: "init(name:cacheDirectoryURL:diskCachePathClosure:)")
public convenience init(
name: String,
path: String?,
diskCachePathClosure: DiskCachePathClosure? = nil) throws
{
let directoryURL = path.flatMap { URL(string: $0) }
try self.init(name: name, cacheDirectoryURL: directoryURL, diskCachePathClosure: diskCachePathClosure)
}
}
7 changes: 4 additions & 3 deletions Tests/KingfisherTests/ImageCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class ImageCacheTests: XCTestCase {

func testInvalidCustomCachePath() {
let customPath = "/path/to/image/cache"
XCTAssertThrowsError(try ImageCache(name: "test", path: customPath)) { error in
let url = URL(fileURLWithPath: customPath)
XCTAssertThrowsError(try ImageCache(name: "test", cacheDirectoryURL: url)) { error in
guard case KingfisherError.cacheError(reason: .cannotCreateDirectory(let path, _)) = error else {
XCTFail("Should be KingfisherError with cacheError reason.")
return
Expand All @@ -65,15 +66,15 @@ class ImageCacheTests: XCTestCase {
let subFolder = cacheURL.appendingPathComponent("temp")

let customPath = subFolder.path
let cache = try! ImageCache(name: "test", path: customPath)
let cache = try! ImageCache(name: "test", cacheDirectoryURL: subFolder)
XCTAssertEqual(
cache.diskStorage.directoryURL.path,
(customPath as NSString).appendingPathComponent("com.onevcat.Kingfisher.ImageCache.test"))
clearCaches([cache])
}

func testCustomCachePathByBlock() {
let cache = try! ImageCache(name: "test", path: nil, diskCachePathClosure: { (url, path) -> URL in
let cache = try! ImageCache(name: "test", cacheDirectoryURL: nil, diskCachePathClosure: { (url, path) -> URL in
let modifiedPath = path + "-modified"
return url.appendingPathComponent(modifiedPath, isDirectory: true)
})
Expand Down

0 comments on commit d40aeb7

Please sign in to comment.