import Foundation final class RDEPUBPageCountCache { private var storage: [RDEPUBChapterCacheKey: RDEPUBRuntimePageCount] = [:] private let lock = NSLock() subscript(key: RDEPUBChapterCacheKey) -> RDEPUBRuntimePageCount? { get { lock.lock() defer { lock.unlock() } return storage[key] } set { lock.lock() defer { lock.unlock() } storage[key] = newValue } } func remove(forSpineIndex spineIndex: Int) { lock.lock() defer { lock.unlock() } storage = storage.filter { $0.value.spineIndex != spineIndex } } func removeAll() { lock.lock() defer { lock.unlock() } storage.removeAll() } }