feat: chapter runtime refactoring and related updates
- Refactor chapter runtime: replace window coordinator/snapshot with warmup orchestrator - Update EPUB core: parser, reading session, JS bridge, navigator layout - Update reader controller: data source, location resolution, persistence - Update chapter runtime: data cache, loader, runtime store, disk cache, warmup orchestrator - Remove deprecated navigation state machine and pagination state - Update text rendering: book cache, HTML normalizer - Update UI: text content view, dark image adjuster, text selection controller - Update settings and reader configuration - Add CODE_REVIEW.md and AUDIT_FINAL.md documentation - Update pod dependencies (remove SSAlertSwift, SnapKit) - Update podspec and pod configuration files Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+39
-3
@@ -3,19 +3,37 @@ 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() }
|
||||
return storage[spineIndex]
|
||||
guard let chapter = storage[spineIndex] else {
|
||||
return nil
|
||||
}
|
||||
touchLocked(spineIndex)
|
||||
return chapter
|
||||
}
|
||||
set {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
storage[spineIndex] = newValue
|
||||
if let newValue {
|
||||
storage[spineIndex] = newValue
|
||||
touchLocked(spineIndex)
|
||||
// Evict oldest entries if over limit
|
||||
evictIfNeededLocked()
|
||||
} else {
|
||||
storage.removeValue(forKey: spineIndex)
|
||||
accessOrder.removeAll { $0 == spineIndex }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +47,29 @@ final class RDEPUBChapterDataCache {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user