freshyo/apps/user-ui/src/lib/image-cache-meta.ts
2026-11-18 09:40:33 +05:30

41 lines
1.5 KiB
TypeScript

import * as FileSystem from 'expo-file-system/legacy'
import { IMAGE_CACHE_CLEARED_AT_KEY } from '@/src/lib/image-cache-ttl'
/**
* Plain-file persistence for the image cache TTL timestamp.
*
* Deliberately not StorageServiceCasual: that writes to expo-secure-store, and
* a cache-maintenance timestamp is not a secret. expo-file-system ships as a
* dependency of `expo`, so no extra package is needed.
*
* The document directory survives app restarts and updates (but not a
* reinstall, which also clears the image cache — exactly what we want).
*/
function timestampFileUri(): string | null {
if (!FileSystem.documentDirectory) return null
return `${FileSystem.documentDirectory}${IMAGE_CACHE_CLEARED_AT_KEY}`
}
/** Epoch ms of the last successful disk-cache clear, or null when unknown. */
export async function readImageCacheClearedAt(): Promise<number | null> {
const uri = timestampFileUri()
if (!uri) return null
try {
const info = await FileSystem.getInfoAsync(uri)
if (!info.exists) return null
const raw = await FileSystem.readAsStringAsync(uri)
const parsed = Number(raw)
return Number.isFinite(parsed) ? parsed : null
} catch {
return null
}
}
/** Persist the last successful clear. Throws so the caller can retry later. */
export async function writeImageCacheClearedAt(timestamp: number): Promise<void> {
const uri = timestampFileUri()
if (!uri) return
await FileSystem.writeAsStringAsync(uri, String(timestamp))
}