Skip to content

Commit 606c7bb

Browse files
author
Christian Elies
committed
feat(): implemented custom cache and custom cache key
1 parent d3cf954 commit 606c7bb

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// DefaultRemoteImageCache.swift
3+
//
4+
//
5+
// Created by Christian Elies on 14.12.19.
6+
//
7+
8+
import Foundation
9+
10+
struct DefaultRemoteImageCache {
11+
let cache = NSCache<AnyObject, PlatformSpecificImageType>()
12+
}
13+
14+
extension DefaultRemoteImageCache: RemoteImageCache {
15+
func object(forKey key: AnyObject) -> PlatformSpecificImageType? { cache.object(forKey: key) }
16+
17+
func setObject(_ object: PlatformSpecificImageType, forKey key: AnyObject) { cache.setObject(object, forKey: key) }
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// RemoteImageCache.swift
3+
//
4+
//
5+
// Created by Christian Elies on 14.12.19.
6+
//
7+
8+
import Foundation
9+
10+
public protocol RemoteImageCache {
11+
func object(forKey key: AnyObject) -> PlatformSpecificImageType?
12+
func setObject(_ object: PlatformSpecificImageType, forKey key: AnyObject)
13+
}

Sources/RemoteImage/public/Services/RemoteImageService.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@ import Combine
1010
import Foundation
1111

1212
public final class RemoteImageService: NSObject, ObservableObject {
13+
public typealias RemoteImageCacheKeyProvider = (RemoteImageType) -> AnyObject
14+
1315
private let dependencies: RemoteImageServiceDependenciesProtocol
1416
private var cancellable: AnyCancellable?
15-
17+
1618
@Published var state: RemoteImageState = .loading
17-
18-
public static let cache = NSCache<NSObject, PlatformSpecificImageType>()
19-
19+
20+
public static var cache: RemoteImageCache = DefaultRemoteImageCache()
21+
public static var cacheKeyProvider: RemoteImageCacheKeyProvider = { remoteImageType in
22+
switch remoteImageType {
23+
case .phAsset(let localIdentifier): return localIdentifier as NSString
24+
case .url(let url): return url as NSURL
25+
}
26+
}
27+
2028
init(dependencies: RemoteImageServiceDependenciesProtocol) {
2129
self.dependencies = dependencies
2230
}
23-
31+
2432
func fetchImage(ofType type: RemoteImageType) {
2533
switch type {
2634
case .url(let url):
@@ -34,15 +42,16 @@ public final class RemoteImageService: NSObject, ObservableObject {
3442
extension RemoteImageService {
3543
private func fetchImage(atURL url: URL) {
3644
cancellable?.cancel()
37-
38-
if let image = RemoteImageService.cache.object(forKey: url as NSURL) {
45+
46+
let cacheKey = Self.cacheKeyProvider(.url(url))
47+
if let image = Self.cache.object(forKey: cacheKey) {
3948
state = .image(image)
4049
return
4150
}
42-
51+
4352
let urlSession = URLSession.shared
4453
let urlRequest = URLRequest(url: url)
45-
54+
4655
cancellable = urlSession.dataTaskPublisher(for: urlRequest)
4756
.map { PlatformSpecificImageType(data: $0.data) }
4857
.receive(on: RunLoop.main)
@@ -54,23 +63,24 @@ extension RemoteImageService {
5463
}
5564
}) { image in
5665
if let image = image {
57-
RemoteImageService.cache.setObject(image, forKey: url as NSURL)
66+
Self.cache.setObject(image, forKey: cacheKey)
5867
self.state = .image(image)
5968
} else {
6069
self.state = .error(RemoteImageServiceError.couldNotCreateImage)
6170
}
6271
}
6372
}
64-
73+
6574
private func fetchImage(withLocalIdentifier localIdentifier: String) {
66-
if let image = RemoteImageService.cache.object(forKey: localIdentifier as NSString) {
75+
let cacheKey = Self.cacheKeyProvider(.phAsset(localIdentifier: localIdentifier))
76+
if let image = Self.cache.object(forKey: cacheKey) {
6777
state = .image(image)
6878
return
6979
}
70-
80+
7181
dependencies.photoKitService.getPhotoData(localIdentifier: localIdentifier, success: { data in
7282
if let image = PlatformSpecificImageType(data: data) {
73-
RemoteImageService.cache.setObject(image, forKey: localIdentifier as NSString)
83+
Self.cache.setObject(image, forKey: cacheKey)
7484
self.state = .image(image)
7585
} else {
7686
self.state = .error(RemoteImageServiceError.couldNotCreateImage)

0 commit comments

Comments
 (0)