iosswiftmd5kingfisher

Why did kingfisher create cacheFileName using md5?


Why did kingfisher create cacheFileName using md5? Are there any other special reasons?

// DiskStorage.swift in Kingfisher
        func cacheFileName(forKey key: String) -> String {
            if config.usesHashedFileName {
                let hashedKey = key.kf.md5
                if let ext = config.pathExtension {
                    return "\(hashedKey).\(ext)"
                } else if config.autoExtAfterHashedFileName,
                          let ext = key.kf.ext {
                    return "\(hashedKey).\(ext)"
                }
                return hashedKey
            } else {
                if let ext = config.pathExtension {
                    return "\(key).\(ext)"
                }
                return key
            }
        }

Solution

  • Using a file's md5 as a filename in a cache is a common pattern. The cache name will change when the file changes, while the source url may not.
    Note that the extension is kept because it may be handy when using the file later.