diff --git a/config.js b/config.js index c6ea774a3..02b40681d 100644 --- a/config.js +++ b/config.js @@ -78,6 +78,12 @@ const CONFIG = { // messages through the websocket to keep the socket alive. KEEP_ALIVE: process.env.TALK_KEEP_ALIVE || '30s', + //------------------------------------------------------------------------------ + // Cache configuration + //------------------------------------------------------------------------------ + + CACHE_EXPIRY_COMMENT_COUNT: process.env.TALK_CACHE_EXPIRY_COMMENT_COUNT || '1hr', + //------------------------------------------------------------------------------ // Recaptcha configuration //------------------------------------------------------------------------------ diff --git a/docs/_docs/02-01-configuration.md b/docs/_docs/02-01-configuration.md index fd2ee0478..d2e990375 100644 --- a/docs/_docs/02-01-configuration.md +++ b/docs/_docs/02-01-configuration.md @@ -128,6 +128,11 @@ The default could be read as: - At the moment of writing, beheviour is not attached to the flagging reliability, but it is recorded. +### Cache + +- `TALK_CACHE_EXPIRY_COMMENT_COUNT` (_optional_) - configure the duration for which + comment counts are cached for. (Default `1hr`) + ### Plugins Plugins configuration can be found on the [Plugins]({{ "/docs/running/plugins/" | absolute_url }}) page. \ No newline at end of file diff --git a/graph/loaders/comments.js b/graph/loaders/comments.js index 16d2101f7..7a17cfabd 100644 --- a/graph/loaders/comments.js +++ b/graph/loaders/comments.js @@ -8,6 +8,10 @@ const { SEARCH_NON_NULL_OR_ACCEPTED_COMMENTS, SEARCH_OTHERS_COMMENTS } = require('../../perms/constants'); +const { + CACHE_EXPIRY_COMMENT_COUNT +} = require('../../config'); +const ms = require('ms'); const CommentModel = require('../../models/comment'); const UsersService = require('../../services/users'); @@ -481,11 +485,11 @@ module.exports = (context) => ({ get: new DataLoader((ids) => getComments(context, ids)), getByQuery: (query) => getCommentsByQuery(context, query), getCountByQuery: (query) => getCommentCountByQuery(context, query), - countByAssetID: new SharedCounterDataLoader('Comments.totalCommentCount', 3600, (ids) => getCountsByAssetID(context, ids)), + countByAssetID: new SharedCounterDataLoader('Comments.totalCommentCount', ms(CACHE_EXPIRY_COMMENT_COUNT), (ids) => getCountsByAssetID(context, ids)), countByAssetIDPersonalized: (query) => getCountsByAssetIDPersonalized(context, query), - parentCountByAssetID: new SharedCounterDataLoader('Comments.countByAssetID', 3600, (ids) => getParentCountsByAssetID(context, ids)), + parentCountByAssetID: new SharedCounterDataLoader('Comments.countByAssetID', ms(CACHE_EXPIRY_COMMENT_COUNT), (ids) => getParentCountsByAssetID(context, ids)), parentCountByAssetIDPersonalized: (query) => getParentCountByAssetIDPersonalized(context, query), - countByParentID: new SharedCounterDataLoader('Comments.countByParentID', 3600, (ids) => getCountsByParentID(context, ids)), + countByParentID: new SharedCounterDataLoader('Comments.countByParentID', ms(CACHE_EXPIRY_COMMENT_COUNT), (ids) => getCountsByParentID(context, ids)), countByParentIDPersonalized: (query) => getCountByParentIDPersonalized(context, query), genRecentReplies: new DataLoader((ids) => genRecentReplies(context, ids)), genRecentComments: new DataLoader((ids) => genRecentComments(context, ids))