NSCache
는 주로 메모리 캐싱에 사용되는 클래스(class)
타입이다.
우선 키워드가 하나 나왔는데 메모리 캐싱
이다.
일반적으로 데이터 캐싱에는 크게 두 가지 유형으로 나눌 수 있습니다.
- Memory Cache (메모리 캐시)
- Disk Cache (디스크 캐시)
각각의 캐시는 다른 목적과 특성을 가지고 있으며, 앱의 성능과 사용자 경험을 향상시키기 위해 사용됩니다.
매우 빠른 읽기와 쓰기 속도를 제공
하지만, 앱이 종료
되거나 시스템에 의해 메모리가 회수될 때
Core Data
, Realm
, URLCache
저장 방법은 아래와 같이 여러 가지 옵션이 있으며 데이터를 관리하는 목적에 맞게 사용하면 되겠습니다.
저장된 Key 객체를 복사하지 않는 특징이 존재
nil
체크를 해야합니다.// 싱글턴 패턴을 적용한 이미지 Cache를 담당하는 객체 생성
final class ImageCacheManager {
// NSCache 객체 획득
static let shared = NSCache<NSString, UIImage>()
private init() {}
}
// 기본적인 사용 방법
// 캐시에 객체 저장 (첫 번째 매개변수: 저장할 객체, 두 번째 매개변수: 연결한 Key 값)
let image = UIImage(named: "example")
ImageCacheManager.shared.setObject(image, forKey: cacheKey)
// 캐시에 저장된 객체를 검색 및 사용
if let cacheImage = ImageCacheManager.shared.object(forKey: cacheKey) {
self.image = cacheImage
}
object(forkey:) 메서드
는 객체를 반환하고 그렇지 않다면 nil
을 반환한다.setObject(_:forKey:)
, object(forKey:)
는 캐시의 핵심적인 동작을 구현하는 메서드들이다.참고한 블로그 글에서는 대부분 DispatchQueue를 이용한 API 호출을 통해 이루어지는 경우를 담았습니다.
저의 프로젝트에서는 API를 다루지않고 mock.json 파일을 통해 로컬에서 Asset을 이용한 방법으로 상이한 부분이 있습니다.
import UIKit
final class ImageCacheManager {
static let shared = NSCache<NSString, UIImage>()
private init() {}
}
extension UIImageView {
func setImageURL(imageName: String) {
/// Cache 객체의 Key 값으로 사용할 String
let cacheKey = NSString(string: imageName)
/// Cache된 이미지가 존재하면 그 이미지를 사용 (API 호출안하는 형태)
if let cacheImage = ImageCacheManager.shared.object(forKey: cacheKey) {
self.image = cacheImage
}
if let image = UIImage(named: imageName) {
self.image = image
ImageCacheManager.shared.setObject(image, forKey: cacheKey)
}
}
}
final class TravelListView: UIView {
...
func configureOfImageView(by imageName: String) {
self.imageView.setImageURL(imageName: imageName)
}
}
/// APIManager는 참고 (로컬 환경의 JSON 파일 파싱 내용)
final class APIManager: JSONParsable {
...
func loadLocalJSON<T>(_ filename: String) throws -> T where T: Decodable {
let data: Data
guard let filePath = Bundle.main.url(forResource: filename, withExtension: nil) else {
print(JSONErrors.notFoundJSONFile.failureReason!)
throw JSONErrors.notFoundJSONFile
}
do {
data = try Data(contentsOf: filePath)
} catch {
print(JSONErrors.notLoadData.failureReason!)
throw JSONErrors.notLoadData
}
do {
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
} catch {
print(JSONErrors.unableToDecode.failureReason!)
throw JSONErrors.unableToDecode
}
}
}
NSCache | Apple Developer Documentation
[iOS - swift] NSCache 개념, 이미지 캐시 (Image cache), (memory cache, disk cache)
[Swift] NSCache 사용해보기