CacheMaintenance prunes the SwiftData caches so they don't grow forever: seen markers >30d, read >90d, cached articles >30d, cached stories >14d — always protecting saved bookmarks. Runs on launch. Settings gains a "Clear offline cache" action (keeps saved) with a count + confirmation. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
62 lines
2.7 KiB
Swift
62 lines
2.7 KiB
Swift
// CacheMaintenance.swift
|
|
// Jarvis — bounds the on-device SwiftData caches so they don't grow forever.
|
|
// Saved (bookmarked) stories are always protected.
|
|
|
|
import Foundation
|
|
import SwiftData
|
|
|
|
@MainActor
|
|
enum CacheMaintenance {
|
|
private static let day: TimeInterval = 86_400
|
|
|
|
// Retention windows.
|
|
private static let seenDays = 30.0 // "seen" markers: only useful while a story is live
|
|
private static let readDays = 90.0 // read history: small, keep longer
|
|
private static let articleDays = 30.0 // offline article bodies
|
|
private static let storyDays = 14.0 // offline story list
|
|
|
|
/// Age-based eviction. Run on launch. Cheap; protects saved stories.
|
|
static func prune(_ context: ModelContext) {
|
|
let now = Date()
|
|
let seenCutoff = now.addingTimeInterval(-seenDays * day)
|
|
let readCutoff = now.addingTimeInterval(-readDays * day)
|
|
let articleCutoff = now.addingTimeInterval(-articleDays * day)
|
|
let storyCutoff = now.addingTimeInterval(-storyDays * day)
|
|
|
|
// Markers: simple age batch deletes.
|
|
try? context.delete(model: SeenStory.self, where: #Predicate { $0.seenAt < seenCutoff })
|
|
try? context.delete(model: ReadStory.self, where: #Predicate { $0.readAt < readCutoff })
|
|
|
|
// Offline content: drop old entries that aren't saved.
|
|
let saved = savedIDs(context)
|
|
if let arts = try? context.fetch(
|
|
FetchDescriptor<CachedArticle>(predicate: #Predicate { $0.cachedAt < articleCutoff })) {
|
|
for a in arts where !saved.contains(a.storyId) { context.delete(a) }
|
|
}
|
|
if let stories = try? context.fetch(
|
|
FetchDescriptor<CachedStory>(predicate: #Predicate { $0.cachedAt < storyCutoff })) {
|
|
for s in stories where !saved.contains(s.id) { context.delete(s) }
|
|
}
|
|
try? context.save()
|
|
}
|
|
|
|
/// Manual "Clear cache" — wipes offline stories/articles + seen/read state,
|
|
/// keeps your saved bookmarks (and their articles).
|
|
static func clear(_ context: ModelContext) {
|
|
try? context.delete(model: SeenStory.self)
|
|
try? context.delete(model: ReadStory.self)
|
|
let saved = savedIDs(context)
|
|
if let arts = try? context.fetch(FetchDescriptor<CachedArticle>()) {
|
|
for a in arts where !saved.contains(a.storyId) { context.delete(a) }
|
|
}
|
|
if let stories = try? context.fetch(FetchDescriptor<CachedStory>()) {
|
|
for s in stories where !saved.contains(s.id) { context.delete(s) }
|
|
}
|
|
try? context.save()
|
|
}
|
|
|
|
private static func savedIDs(_ context: ModelContext) -> Set<String> {
|
|
Set((try? context.fetch(FetchDescriptor<SavedStory>()))?.map(\.id) ?? [])
|
|
}
|
|
}
|