Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Add ability to cache comments count endpoint #15734

Conversation

lenabaidakova
Copy link
Contributor

@lenabaidakova lenabaidakova commented Oct 31, 2022

closes TryGhost/Team#2094

PR to comments repo TryGhost/comments-ui#12

@codecov
Copy link

codecov bot commented Oct 31, 2022

Codecov Report

Base: 53.40% // Head: 53.42% // Increases project coverage by +0.01% 🎉

Coverage data is based on head (21cb7ef) compared to base (ab678c3).
Patch coverage: 36.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15734      +/-   ##
==========================================
+ Coverage   53.40%   53.42%   +0.01%     
==========================================
  Files        1502     1502              
  Lines       97935    97950      +15     
  Branches    10976    10985       +9     
==========================================
+ Hits        52307    52332      +25     
+ Misses      44369    44360       -9     
+ Partials     1259     1258       -1     
Impacted Files Coverage Δ
...t/core/core/server/services/comments/controller.js 0.00% <0.00%> (ø)
ghost/core/core/server/web/comments/routes.js 0.00% <0.00%> (ø)
ghost/mw-cache-control/lib/mw-cache-control.js 100.00% <100.00%> (ø)
ghost/admin/app/helpers/gh-price-amount.js 44.44% <0.00%> (-22.23%) ⬇️
...ata-generator/lib/tables/members-created-events.js 91.42% <0.00%> (-8.58%) ⬇️
ghost/admin/app/utils/publish-options.js 76.87% <0.00%> (+1.15%) ⬆️
ghost/data-generator/lib/tables/members.js 100.00% <0.00%> (+1.53%) ⬆️
...host/data-generator/lib/tables/members-products.js 97.14% <0.00%> (+2.85%) ⬆️
ghost/admin/app/components/gh-file-uploader.js 86.04% <0.00%> (+3.10%) ⬆️
ghost/data-generator/lib/tables/subscriptions.js 100.00% <0.00%> (+4.68%) ⬆️
... and 3 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@lenabaidakova lenabaidakova force-pushed the 2094-inability-to-cache-comments-count-endpoint-due-to-post-setup branch 4 times, most recently from 5ed0e10 to 43a64ec Compare October 31, 2022 14:10
@lenabaidakova lenabaidakova requested review from allouis and naz October 31, 2022 14:11
@lenabaidakova lenabaidakova marked this pull request as ready for review October 31, 2022 14:11
@lenabaidakova lenabaidakova force-pushed the 2094-inability-to-cache-comments-count-endpoint-due-to-post-setup branch from 43a64ec to 5470287 Compare October 31, 2022 14:19
@lenabaidakova lenabaidakova changed the title Add ability to cache comments count endpoint 🐛 Add ability to cache comments count endpoint Oct 31, 2022
@@ -140,6 +140,9 @@
},
"contentAPI": {
"maxAge": 0
},
"comments": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like the contentAPI convention used above. This is an API we are caching, so maybe commentsAPI would be a better fit. Also, with the discussion on the issue itself if we end up only caching the comments endpoint long-term, than commentsCountAPI would be even better name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed it to commentsCountAPI


const bodyParser = require('body-parser');
const membersService = require('../../../server/services/members');

module.exports = function apiRoutes() {
const router = express.Router('comment api');

router.use(shared.middleware.cacheControl('public', {maxAge: config.get('caching:comments:maxAge')}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue we are solving here is for one specific endpoint and not the whole API. I think we would be most safe to apply this caching rule to the counts endpoint first to unblock the issue and do more research around correct approach for the whole Comments API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added cache just of counts API

Copy link
Contributor

@naz naz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look good. The controller/api level changes are good to go into main I think. The part around setting a custom cache header should to be endpoint-specific.

@lenabaidakova lenabaidakova force-pushed the 2094-inability-to-cache-comments-count-endpoint-due-to-post-setup branch 2 times, most recently from 5cb07c1 to 721d94e Compare November 1, 2022 06:37
@lenabaidakova
Copy link
Contributor Author

lenabaidakova commented Nov 1, 2022

@naz , thank you for explanation 🙌

Fixed next issues:

  • Renamed setting in config to commentsCountAPI
  • Changes applied just for count API
  • Added stale-while-revalidate option

@lenabaidakova lenabaidakova requested a review from naz November 1, 2022 06:39
@@ -12,10 +12,18 @@ const isString = require('lodash/isString');
* @param {'public'|'private'} profile Use "private" if you do not want caching
* @param {object} [options]
* @param {number} [options.maxAge] The max-age in seconds to use when profile is "public"
* @param {number} [options.staleWhileRevalidate] The stale-while-revalidate in seconds to use when profile is "public"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh exciting! Nice 😊

@lenabaidakova lenabaidakova force-pushed the 2094-inability-to-cache-comments-count-endpoint-due-to-post-setup branch from 96cb5f2 to 9ce43e2 Compare November 1, 2022 10:18
@lenabaidakova lenabaidakova requested a review from naz November 1, 2022 10:20
Copy link
Contributor

@naz naz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! All of this looks about right 👌 If I had to be extra nit-picky about it, would separate it into two commits and staleWhileRevalidate functionality there with specific explanation for whys and hows 😊

@lenabaidakova lenabaidakova force-pushed the 2094-inability-to-cache-comments-count-endpoint-due-to-post-setup branch from 9ce43e2 to fbec822 Compare November 2, 2022 05:57
@lenabaidakova
Copy link
Contributor Author

Yup! All of this looks about right 👌 If I had to be extra nit-picky about it, would separate it into two commits and staleWhileRevalidate functionality there with specific explanation for whys and hows 😊

Fixed :)

closes TryGhost/Team#2094
- Comment counts request was changed from `post` to `get` to enable request caching.
closes TryGhost/Team#2094
This value can be used for non-crucial data with a `public` option. For example: `public, max-age=1, stale-while-revalidate=9`.
The idea behind this option is that the browser would cache the value for the number of seconds in `max-age` and would use it for the number of seconds in `stale-while-revalidate` until it gets a "validated response" from the server. The behaviour should be almost unnoticeable for the end user but would make a big difference in the amount of requests to server.
@lenabaidakova lenabaidakova force-pushed the 2094-inability-to-cache-comments-count-endpoint-due-to-post-setup branch from fbec822 to 21cb7ef Compare November 2, 2022 06:31
@lenabaidakova lenabaidakova merged commit 93c6abc into TryGhost:main Nov 2, 2022
@lenabaidakova lenabaidakova deleted the 2094-inability-to-cache-comments-count-endpoint-due-to-post-setup branch November 2, 2022 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants