Skip to content

Commit 8cd58e5

Browse files
authored
fix: invalidate unchanged alias cache when updating feeds (#492)
* fix: invalidate unchanged alias cache when updating feeds * fix: resolve main conflicts for issue 429 * chore: drop obsolete feed-cache helper * fix: re-export clearFeedCache for server consumers
1 parent 48f7636 commit 8cd58e5

3 files changed

Lines changed: 63 additions & 11 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expect, it } from 'bun:test';
2+
3+
import { clearFeedCache } from '../clear-feed-cache';
4+
5+
describe('clearFeedCache', () => {
6+
it('deletes alias cache when alias is unchanged', async () => {
7+
const deletedPrefixes: string[] = [];
8+
const deletedKeys: Array<{ key: string; save: boolean | undefined }> = [];
9+
const cache = {
10+
async deletePrefix(prefix: string) {
11+
deletedPrefixes.push(prefix);
12+
},
13+
async delete(key: string, save?: boolean) {
14+
deletedKeys.push({ key, save });
15+
}
16+
} as any;
17+
18+
await clearFeedCache(cache, 42, 'about', 'about');
19+
20+
expect(deletedPrefixes).toEqual([
21+
'feeds_',
22+
'search_',
23+
'42_previous_feed',
24+
'42_next_feed'
25+
]);
26+
expect(deletedKeys).toEqual([
27+
{ key: 'feed_42', save: false },
28+
{ key: 'feed_about', save: false }
29+
]);
30+
});
31+
32+
it('deletes both old and new alias cache keys when alias changes', async () => {
33+
const deletedKeys: Array<{ key: string; save: boolean | undefined }> = [];
34+
const cache = {
35+
async deletePrefix() {},
36+
async delete(key: string, save?: boolean) {
37+
deletedKeys.push({ key, save });
38+
}
39+
} as any;
40+
41+
await clearFeedCache(cache, 42, 'about', 'about-us');
42+
43+
expect(deletedKeys).toEqual([
44+
{ key: 'feed_42', save: false },
45+
{ key: 'feed_about', save: false },
46+
{ key: 'feed_about-us', save: false }
47+
]);
48+
});
49+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { CacheImpl } from "../core/hono-types";
2+
3+
export async function clearFeedCache(cache: CacheImpl, id: number, alias: string | null, newAlias: string | null) {
4+
await cache.deletePrefix('feeds_');
5+
await cache.deletePrefix('search_');
6+
await cache.delete(`feed_${id}`, false);
7+
await cache.deletePrefix(`${id}_previous_feed`);
8+
await cache.deletePrefix(`${id}_next_feed`);
9+
if (alias) await cache.delete(`feed_${alias}`, false);
10+
if (newAlias && newAlias !== alias) await cache.delete(`feed_${newAlias}`, false);
11+
}

server/src/services/feed.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { and, asc, count, desc, eq, gt, like, lt, or } from "drizzle-orm";
22
import { Hono } from "hono";
3-
import type { Variables, CacheImpl } from "../core/hono-types";
3+
import type { Variables } from "../core/hono-types";
44
import { profileAsync } from "../core/server-timing";
55
import { feeds, visits, visitStats } from "../db/schema";
66
import { HyperLogLog } from "../utils/hyperloglog";
77
import { extractImageWithMetadata } from "../utils/image";
88
import { syncFeedAISummaryQueueState } from "./feed-ai-summary";
99
import { bindTagToPost } from "./tag";
10+
import { clearFeedCache } from "./clear-feed-cache";
11+
export { clearFeedCache } from "./clear-feed-cache";
1012

1113
// Lazy-loaded modules for WordPress import
1214
let XMLParser: any;
@@ -659,13 +661,3 @@ type FeedItem = {
659661
tags?: string[];
660662
}
661663

662-
export async function clearFeedCache(cache: CacheImpl, id: number, alias: string | null, newAlias: string | null) {
663-
await cache.deletePrefix('feeds_');
664-
await cache.deletePrefix('search_');
665-
await cache.delete(`feed_${id}`, false);
666-
await cache.deletePrefix(`${id}_previous_feed`);
667-
await cache.deletePrefix(`${id}_next_feed`);
668-
if (alias === newAlias) return;
669-
if (alias) await cache.delete(`feed_${alias}`, false);
670-
if (newAlias) await cache.delete(`feed_${newAlias}`, false);
671-
}

0 commit comments

Comments
 (0)