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 entriesForSpineIndex(_ spineIndex: Int) -> [(RDEPUBChapterCacheKey, RDEPUBRuntimePageCount)] { lock.lock() defer { lock.unlock() } return storage.filter { $0.value.spineIndex == spineIndex }.map { ($0.key, $0.value) } } 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() } }