From 5511a1504e6496e8a2a100b6a6913c5e1eacf0de Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 24 Sep 2026 18:40:53 -0700 Subject: Prefetch a smaller feed and bound its local cache --- tools/feed-cache-test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tools/feed-cache-test.js (limited to 'tools/feed-cache-test.js') diff --git a/tools/feed-cache-test.js b/tools/feed-cache-test.js new file mode 100644 index 0000000..995e01a --- /dev/null +++ b/tools/feed-cache-test.js @@ -0,0 +1,25 @@ +import assert from 'node:assert/strict'; +import { createFeedCache } from '../src/feed-cache.js'; +const values = new Map(); +const storage = { get length() { return values.size; }, key: (i) => [...values.keys()][i], + getItem: (k) => values.get(k) || null, setItem: (k, v) => values.set(k, v), removeItem: (k) => values.delete(k) }; +let time = 1000; +const cache = createFeedCache(storage, 'wallet:feedNotes', () => time++); +const notes = Array.from({ length: 50 }, (_, i) => ({ id: String(i), pubkey: 'author', kind: 1, + created_at: 100 - i, content: 'post', tags: [] })); +storage.setItem('wallet:feedNotes', JSON.stringify(notes)); +storage.setItem('wallet:funds', 'untouched'); +assert.equal(cache.read('following').length, 30); +assert.equal(JSON.parse(storage.getItem('wallet:feedNotes')).notes.length, 30); +for (let i = 0; i < 5; i++) cache.save('custom' + i, notes); +assert.equal(cache.read('following').length, 0); +assert.equal([...values.keys()].filter((k) => k.includes('feedNotes')).length, 4); +cache.read('custom1'); +cache.save('custom5', notes); +assert.equal(cache.read('custom2').length, 0); +cache.save('big', notes.map((e) => ({ ...e, content: 'x'.repeat(15000) }))); +assert([...values].filter(([k]) => k.includes('feedNotes')).reduce((n, [, v]) => n + v.length, 0) <= 256000); +time += 8 * 86400_000; +assert.equal(cache.read('big').length, 0); +assert.equal(storage.getItem('wallet:funds'), 'untouched'); +console.log('✓ legacy caches shrink; 30 posts/feed, four feeds, byte budget, LRU and seven-day expiry enforced'); -- cgit v1.2.3