From a1214cf38b49afa2e06cfe2046b5e1e8084e7ea4 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 20 Jan 2025 19:28:06 -0600 Subject: [PATCH] forgot a hashvalue always changes so I can't use that, therefore I created extension on URL for a UniqueIdentifier based of crypto that will always be the same. Signed-off-by: Matt Bruce --- EmployeeDirectory/Extensions/URL.swift | 16 ++++++++++++++++ .../Services/ImageCacheService.swift | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 EmployeeDirectory/Extensions/URL.swift diff --git a/EmployeeDirectory/Extensions/URL.swift b/EmployeeDirectory/Extensions/URL.swift new file mode 100644 index 0000000..e146d27 --- /dev/null +++ b/EmployeeDirectory/Extensions/URL.swift @@ -0,0 +1,16 @@ +// +// URL.swift +// EmployeeDirectory +// +// Created by Matt Bruce on 1/20/25. +// +import Foundation +import CryptoKit + +extension URL { + internal var uniqueIdentifier: String { + let data = Data(absoluteString.utf8) + let hash = SHA256.hash(data: data) + return hash.compactMap { String(format: "%02x", $0) }.joined() + } +} diff --git a/EmployeeDirectory/Services/ImageCacheService.swift b/EmployeeDirectory/Services/ImageCacheService.swift index b28b3c3..ba45f2a 100644 --- a/EmployeeDirectory/Services/ImageCacheService.swift +++ b/EmployeeDirectory/Services/ImageCacheService.swift @@ -20,7 +20,7 @@ public class ImageCacheService { public init() {} public func loadImage(from url: URL) async -> UIImage? { - let uniqueKey = String(url.absoluteString.hashValue) + let uniqueKey = url.uniqueIdentifier let cacheKey = uniqueKey as NSString if let cachedImage = memoryCache.object(forKey: cacheKey) {