首页 > 解决方案 > 为什么 kingfisher 使用 md5 创建 cacheFileName?

问题描述

为什么 kingfisher 使用 md5 创建 cacheFileName?是否有其他特殊原因?

// 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
            }
        }

标签: iosswiftmd5kingfisher

解决方案


使用文件的 md5 作为缓存中的文件名是一种常见模式。文件更改时缓存名称会更改,而源 url 可能不会。
请注意,保留扩展名是因为以后使用该文件时可能会很方便。


推荐阅读