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/pleroma/frontend_configurations', frontendConfigController);
|
||||||
|
|
||||||
app.get(
|
app.get('/api/v1/trends/tags', trendingTagsController);
|
||||||
'/api/v1/trends/tags',
|
app.get('/api/v1/trends', trendingTagsController);
|
||||||
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/suggestions', suggestionsV1Controller);
|
app.get('/api/v1/suggestions', suggestionsV1Controller);
|
||||||
app.get('/api/v2/suggestions', suggestionsV2Controller);
|
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));
|
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 trendingTagsController: AppController = async (c) => {
|
||||||
const limit = limitSchema.parse(c.req.query('limit'));
|
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 now = new Date();
|
||||||
const yesterday = new Date(now.getTime() - Time.days(1));
|
const yesterday = new Date(now.getTime() - Time.days(1));
|
||||||
const lastWeek = new Date(now.getTime() - Time.days(7));
|
const lastWeek = new Date(now.getTime() - Time.days(7));
|
||||||
|
@ -22,11 +32,10 @@ const trendingTagsController: AppController = async (c) => {
|
||||||
const tags = await TrendsWorker.getTrendingTags({
|
const tags = await TrendsWorker.getTrendingTags({
|
||||||
since: yesterday,
|
since: yesterday,
|
||||||
until: now,
|
until: now,
|
||||||
limit,
|
limit: 20,
|
||||||
});
|
});
|
||||||
|
|
||||||
return c.json(
|
return Promise.all(tags.map(async ({ tag, uses, accounts }) => ({
|
||||||
await Promise.all(tags.map(async ({ tag, uses, accounts }) => ({
|
|
||||||
name: tag,
|
name: tag,
|
||||||
url: Conf.local(`/tags/${tag}`),
|
url: Conf.local(`/tags/${tag}`),
|
||||||
history: [
|
history: [
|
||||||
|
@ -50,8 +59,7 @@ const trendingTagsController: AppController = async (c) => {
|
||||||
uses: String(history.uses),
|
uses: String(history.uses),
|
||||||
})),
|
})),
|
||||||
],
|
],
|
||||||
}))),
|
})));
|
||||||
);
|
}
|
||||||
};
|
|
||||||
|
|
||||||
export { trendingTagsController };
|
export { trendingTagsController };
|
||||||
|
|
Loading…
Reference in New Issue