Skip to content

Commit

Permalink
Apply all feed cache for all
Browse files Browse the repository at this point in the history
  • Loading branch information
mzyy94 committed Feb 16, 2024
1 parent b7a7764 commit 44b9ad3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 72 deletions.
19 changes: 4 additions & 15 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@ const feedCacheKey = (
iss: string,
marker: { control: ControlMode; updatedAt: number }[],
) => {
const added = marker.find(
(m) => m.control === ControlMode.LastAdded,
)?.updatedAt;
const deleted = marker.find(
(m) => m.control === ControlMode.LastDeleted,
)?.updatedAt;
if (!added || !deleted) {
return null;
}
const added =
marker.find((m) => m.control === ControlMode.LastAdded)?.updatedAt ?? 0;
const deleted =
marker.find((m) => m.control === ControlMode.LastDeleted)?.updatedAt ?? 0;
const { FEED_HOST } = env<{ FEED_HOST: string }>(c);
const url = new URL(
`https://${FEED_HOST}/xrpc/app.bsky.feed.getFeedSkeleton?internal`,
Expand All @@ -60,9 +55,6 @@ export async function getAllFeedFromCache(
) {
const cache = await caches.open('feed-cache');
const req = feedCacheKey(c, iss, marker);
if (!req) {
return null;
}
const res = await cache.match(req);
return res?.json<AllFeed>();
}
Expand All @@ -75,9 +67,6 @@ export async function putAllFeedToCache(
) {
const cache = await caches.open('feed-cache');
const req = feedCacheKey(c, iss, marker);
if (!req) {
return null;
}
const res = new Response(JSON.stringify(allFeed));
return cache.put(req, res);
}
80 changes: 23 additions & 57 deletions src/xrpc/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const getFeedSkeletonHandlers = factory.createHandlers(
const db = drizzle(DB);

const { limit, cursor } = c.req.valid('query');
const [begin, time, cid] = cursor ?? [];
const [, , cid] = cursor ?? [];

const cacheMarkers = await db
.select({
Expand All @@ -66,68 +66,34 @@ export const getFeedSkeletonHandlers = factory.createHandlers(
),
);

if (cacheMarkers.length === 2) {
let allFeed = await getAllFeedFromCache(c, iss, cacheMarkers);
if (!allFeed) {
allFeed = await db
.select({
post: bookmarks.uri,
cid: bookmarks.cid,
updatedAt: sql<number>`unixepoch(${bookmarks.updatedAt})`,
})
.from(bookmarks)
.limit(1000)
.where(
and(
eq(bookmarks.sub, iss),
eq(bookmarks.control, ControlMode.Active),
),
);
if (allFeed.length === 0) {
return c.json({ feed: [] });
}
allFeed.sort((a, b) => b.updatedAt - a.updatedAt);
await putAllFeedToCache(c, iss, cacheMarkers, allFeed);
let allFeed = await getAllFeedFromCache(c, iss, cacheMarkers);
if (!allFeed) {
allFeed = await db
.select({
post: bookmarks.uri,
cid: bookmarks.cid,
updatedAt: sql<number>`unixepoch(${bookmarks.updatedAt})`,
})
.from(bookmarks)
.limit(1000)
.where(
and(
eq(bookmarks.sub, iss),
eq(bookmarks.control, ControlMode.Active),
),
);
if (allFeed.length === 0) {
return c.json({ feed: [] });
}

allFeed.sort((a, b) => b.updatedAt - a.updatedAt);
const index = allFeed.findIndex((a) => a.cid === cid);
const result = allFeed.slice(index + 1, index + 1 + limit);
const feed = result.map(({ post }) => ({ post }));
const lastPost = result[result.length - 1];
const lastCur = createCursor(lastPost);
return c.json({ cursor: lastCur, feed });
await putAllFeedToCache(c, iss, cacheMarkers, allFeed);
}

const filters =
+time && cid
? [
lte(sql`unixepoch(${bookmarks.updatedAt})`, +time),
ne(bookmarks.cid, cid),
]
: [];

const result = await db
.select({
post: bookmarks.uri,
cid: bookmarks.cid,
updatedAt: sql<number>`unixepoch(${bookmarks.updatedAt})`,
})
.from(bookmarks)
.orderBy(desc(bookmarks.updatedAt))
.limit(limit)
.where(
and(
eq(bookmarks.sub, iss),
eq(bookmarks.control, ControlMode.Active),
...filters,
),
);

const index = allFeed.findIndex((a) => a.cid === cid);
const result = allFeed.slice(index + 1, index + 1 + limit);
const feed = result.map(({ post }) => ({ post }));
const lastPost = result[result.length - 1];
const lastCur = createCursor(lastPost);
const res = c.json({ cursor: lastCur, feed });
return res;
return c.json({ cursor: lastCur, feed });
},
);

0 comments on commit 44b9ad3

Please sign in to comment.