Skip to content

Commit

Permalink
add cache duratin
Browse files Browse the repository at this point in the history
  • Loading branch information
kabeaty committed Jul 15, 2024
1 parent 822abf5 commit 92e9cbb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 6 additions & 0 deletions common/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
3 changes: 2 additions & 1 deletion server/src/core/server/app/handlers/api/story/count.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tenant>;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 92e9cbb

Please sign in to comment.