Fix trends cache
This commit is contained in:
parent
5822253796
commit
80344e3c5f
|
@ -194,12 +194,8 @@ app.get('/api/v2/search', searchController);
|
|||
|
||||
app.get('/api/pleroma/frontend_configurations', frontendConfigController);
|
||||
|
||||
app.get(
|
||||
'/api/v1/trends/tags',
|
||||
cacheMiddleware({ cacheName: 'web', expires: Time.minutes(15) }),
|
||||
trendingTagsController,
|
||||
);
|
||||
app.get('/api/v1/trends', cacheMiddleware({ cacheName: 'web', expires: Time.minutes(15) }), trendingTagsController);
|
||||
app.get('/api/v1/trends/tags', trendingTagsController);
|
||||
app.get('/api/v1/trends', trendingTagsController);
|
||||
|
||||
app.get('/api/v1/suggestions', suggestionsV1Controller);
|
||||
app.get('/api/v2/suggestions', suggestionsV2Controller);
|
||||
|
|
|
@ -10,10 +10,20 @@ await TrendsWorker.open('data/trends.sqlite3');
|
|||
|
||||
const limitSchema = z.coerce.number().catch(10).transform((value) => Math.min(Math.max(value, 0), 20));
|
||||
|
||||
let cache = getTrends();
|
||||
|
||||
Deno.cron('update trends cache', { minute: { every: 15 } }, async () => {
|
||||
const trends = await getTrends();
|
||||
cache = Promise.resolve(trends);
|
||||
});
|
||||
|
||||
const trendingTagsController: AppController = async (c) => {
|
||||
const limit = limitSchema.parse(c.req.query('limit'));
|
||||
if (limit < 1) return c.json([]);
|
||||
const trends = await cache;
|
||||
return c.json(trends.slice(0, limit));
|
||||
};
|
||||
|
||||
async function getTrends() {
|
||||
const now = new Date();
|
||||
const yesterday = new Date(now.getTime() - Time.days(1));
|
||||
const lastWeek = new Date(now.getTime() - Time.days(7));
|
||||
|
@ -22,11 +32,10 @@ const trendingTagsController: AppController = async (c) => {
|
|||
const tags = await TrendsWorker.getTrendingTags({
|
||||
since: yesterday,
|
||||
until: now,
|
||||
limit,
|
||||
limit: 20,
|
||||
});
|
||||
|
||||
return c.json(
|
||||
await Promise.all(tags.map(async ({ tag, uses, accounts }) => ({
|
||||
return Promise.all(tags.map(async ({ tag, uses, accounts }) => ({
|
||||
name: tag,
|
||||
url: Conf.local(`/tags/${tag}`),
|
||||
history: [
|
||||
|
@ -50,8 +59,7 @@ const trendingTagsController: AppController = async (c) => {
|
|||
uses: String(history.uses),
|
||||
})),
|
||||
],
|
||||
}))),
|
||||
);
|
||||
};
|
||||
})));
|
||||
}
|
||||
|
||||
export { trendingTagsController };
|
||||
|
|
Loading…
Reference in New Issue