Count views and evict server copies by retention criteria before LRU

This commit is contained in:
Claude
2026-09-30 07:25:56 +00:00
parent f12ca87131
commit eb3fe45761
8 changed files with 118 additions and 5 deletions

View File

@@ -240,6 +240,27 @@ describe('media cache jobs', () => {
expect(await cache.getReady('lruAAAAAAA3')).not.toBeNull();
});
test('eviction follows listMediaEvictionOrder when the db provides it', async () => {
await clearDb();
const size = readFileSync(fx('good.mp4')).length;
const db = {
getMedia: dbmod.getMedia, upsertMedia: dbmod.upsertMedia, deleteMedia: dbmod.deleteMedia,
listMedia: dbmod.listMedia, listMediaLru: dbmod.listMediaLru, touchMedia: dbmod.touchMedia,
mediaStats: dbmod.mediaStats,
// Retention says the MORE recently played #2 is the one to drop (e.g. no views).
listMediaEvictionOrder: async () => (await dbmod.listMediaLru()).reverse(),
};
const { cache } = makeCache({ db, maxBytes: size * 2 + 60 * 250 * 1024 + 50_000, duration: 10 });
await cache.init();
await cache.ensureCached('retAAAAAAA1', { priority: HIGH });
await cache.ensureCached('retAAAAAAA2', { priority: HIGH });
await dbmod.upsertMedia('retAAAAAAA1', { last_access: 1000 });
await dbmod.upsertMedia('retAAAAAAA2', { last_access: 2000 });
await cache.ensureCached('retAAAAAAA3', { priority: HIGH });
expect(await dbmod.getMedia('retAAAAAAA2')).toBeNull();
expect(await dbmod.getMedia('retAAAAAAA1')).not.toBeNull();
});
test('free-disk guard skips caching', async () => {
await clearDb();
const { cache, calls } = makeCache({ freeBytes: () => 1024 ** 3, minFreeBytes: 5 * 1024 ** 3 });