diff --git a/common/lib/constants.ts b/common/lib/constants.ts index db19d6d600..4c9b6e9815 100644 --- a/common/lib/constants.ts +++ b/common/lib/constants.ts @@ -86,6 +86,12 @@ export const DEFAULT_SESSION_DURATION = 90 * TIME.DAY; */ export const COMMENT_REPEAT_POST_DURATION = 6 * TIME.MINUTE; +/** + * COUNTS_V2_CACHE_DURATION is the length of time in seconds that the comment + * counts for a story are cached for the counts v2 endoint. + */ +export const COUNTS_V2_CACHE_DURATION = 1 * TIME.DAY; + /** * SPOILER_CLASSNAME is the classname that is attached to spoilers. */ diff --git a/server/src/core/server/app/handlers/api/story/count.ts b/server/src/core/server/app/handlers/api/story/count.ts index 83cc019476..cea6d716a5 100644 --- a/server/src/core/server/app/handlers/api/story/count.ts +++ b/server/src/core/server/app/handlers/api/story/count.ts @@ -1,5 +1,6 @@ import Joi from "joi"; +import { COUNTS_V2_CACHE_DURATION } from "coral-common/common/lib/constants"; import { CountJSONPData } from "coral-common/common/lib/types/count"; import { AppOptions } from "coral-server/app"; import { validate } from "coral-server/app/request/body"; @@ -220,7 +221,7 @@ export const countsV2Handler = ); // add count to Redis cache here then return - await redis.set(key, count); + await redis.set(key, count, "EX", COUNTS_V2_CACHE_DURATION); logger.debug("set story count for counts v2 in redis cache", { storyID, count, diff --git a/server/src/core/server/stacks/helpers/updateAllCommentCounts.ts b/server/src/core/server/stacks/helpers/updateAllCommentCounts.ts index 4d61e699cd..0fb199c564 100644 --- a/server/src/core/server/stacks/helpers/updateAllCommentCounts.ts +++ b/server/src/core/server/stacks/helpers/updateAllCommentCounts.ts @@ -29,6 +29,7 @@ import { GQLFEATURE_FLAG, GQLTAG, } from "coral-server/graph/schema/__generated__/types"; +import { COUNTS_V2_CACHE_DURATION } from "coral-common/common/lib/constants"; interface UpdateAllCommentCountsInput { tenant: Readonly; @@ -226,11 +227,10 @@ export default async function updateAllCommentCounts( updatedStory.commentCounts.status ); if (PUBLISHED_STATUSES.includes(input.after.status)) { - // Add to Redis cache count const key = `${tenant.id}:${storyID}:count`; // set/update the count - await redis.set(key, totalCount); + await redis.set(key, totalCount, "EX", COUNTS_V2_CACHE_DURATION); } } }