- Rename source module from RDReaderView to RDEpubReaderView - Move all source files from Sources/RDReaderView/ to Sources/RDEpubReaderView/ - Update podspec: RDReaderView.podspec -> RDEpubReaderView.podspec - Update Podfile, demo project, and CocoaPods config for new pod name - Delete old RDReaderView pod support files from ReadViewDemo/Pods - Add new RDEpubReaderView pod support files - Update documentation (API ref, architecture, UML, conventions, etc.) - Add FixedLayoutRotationTests - Update .gitignore: exclude .DS_Store, manual unpack backups, _ssoft-output
76 lines
2.1 KiB
Swift
76 lines
2.1 KiB
Swift
import Foundation
|
|
|
|
final class RDEPUBChapterDataCache {
|
|
|
|
private var storage: [Int: RDEPUBRuntimeChapter] = [:]
|
|
private var accessOrder: [Int] = [] // H-05: LRU tracking for eviction
|
|
private let maxEntryCount: Int
|
|
|
|
private let lock = NSLock()
|
|
|
|
init(maxEntryCount: Int = 30) {
|
|
self.maxEntryCount = maxEntryCount
|
|
}
|
|
|
|
subscript(_ spineIndex: Int) -> RDEPUBRuntimeChapter? {
|
|
get {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
guard let chapter = storage[spineIndex] else {
|
|
return nil
|
|
}
|
|
touchLocked(spineIndex)
|
|
return chapter
|
|
}
|
|
set {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
if let newValue {
|
|
storage[spineIndex] = newValue
|
|
touchLocked(spineIndex)
|
|
// Evict oldest entries if over limit
|
|
evictIfNeededLocked()
|
|
} else {
|
|
storage.removeValue(forKey: spineIndex)
|
|
accessOrder.removeAll { $0 == spineIndex }
|
|
}
|
|
}
|
|
}
|
|
|
|
var storedSpineIndices: [Int] {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
return Array(storage.keys)
|
|
}
|
|
|
|
func remove(spineIndex: Int) {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
storage.removeValue(forKey: spineIndex)
|
|
accessOrder.removeAll { $0 == spineIndex }
|
|
}
|
|
|
|
func removeAll() {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
storage.removeAll()
|
|
accessOrder.removeAll()
|
|
}
|
|
|
|
/// H-05: Evict least recently used entries when cache exceeds maxEntryCount.
|
|
/// Must be called while holding lock.
|
|
private func evictIfNeededLocked() {
|
|
while storage.count > maxEntryCount, let oldest = accessOrder.first {
|
|
storage.removeValue(forKey: oldest)
|
|
accessOrder.removeFirst()
|
|
}
|
|
}
|
|
|
|
/// Marks an entry as most recently used.
|
|
/// Must be called while holding lock.
|
|
private func touchLocked(_ spineIndex: Int) {
|
|
accessOrder.removeAll { $0 == spineIndex }
|
|
accessOrder.append(spineIndex)
|
|
}
|
|
}
|