1
1
mirror of https://github.com/kean/Nuke.git synced 2024-12-01 02:44:40 +03:00
Nuke/Tests/MockImageCache.swift
2024-01-06 15:18:14 -05:00

45 lines
1010 B
Swift

// The MIT License (MIT)
//
// Copyright (c) 2015-2024 Alexander Grebenyuk (github.com/kean).
import Foundation
@testable import Nuke
class MockImageCache: ImageCaching, @unchecked Sendable {
let queue = DispatchQueue(label: "com.github.Nuke.MockCache")
var enabled = true
var images = [AnyHashable: ImageContainer]()
var readCount = 0
var writeCount = 0
init() {}
func resetCounters() {
readCount = 0
writeCount = 0
}
subscript(key: ImageCacheKey) -> ImageContainer? {
get {
queue.sync {
readCount += 1
return enabled ? images[key] : nil
}
}
set {
queue.sync {
writeCount += 1
if let image = newValue {
if enabled { images[key] = image }
} else {
images[key] = nil
}
}
}
}
func removeAll() {
images.removeAll()
}
}