Skip to content

Commit 3ddfe59

Browse files
committed
Added resource caching to Posts/Pages Content API
refs https://github.com/TryGhost/Toolbox/issues/522 - Browse endpoints for Posts and Pages are creating the most database traffic in the system. These are read-only endpoints that don't have to be fresh 100% of the time. Having optional cache allows to offload some of the database querying to more efficient storage. - To enable cache for Posts/Pages browse endpoints there are two prerequisites: - set 'hostSettings:postsPublicCache:enabled' to 'true' in the configuration file - add 'postsPublic' cache adapter in cache configuration - Example config for adapters with 60s TTL for a cached resource: ``` "adapters": { "cache": { "postsPublic": { "adapter": "Redis", "ttl": 60, "keyPrefix": "site_id_here:posts-content-api:" } } }, ```
1 parent c3d7104 commit 3ddfe59

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

ghost/core/core/boot.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ async function initServices({config}) {
295295
const emailAnalytics = require('./server/services/email-analytics');
296296
const mentionsService = require('./server/services/mentions');
297297
const tagsPublic = require('./server/services/tags-public');
298+
const postsPublic = require('./server/services/posts-public');
298299

299300
const urlUtils = require('./shared/url-utils');
300301

@@ -313,6 +314,7 @@ async function initServices({config}) {
313314
members.init(),
314315
tiers.init(),
315316
tagsPublic.init(),
317+
postsPublic.init(),
316318
membersEvents.init(),
317319
permissions.init(),
318320
xmlrpc.listen(),

ghost/core/core/server/api/endpoints/pages-public.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const tpl = require('@tryghost/tpl');
22
const errors = require('@tryghost/errors');
33
const models = require('../../models');
4+
const postsPublicService = require('../../services/posts-public');
5+
46
const ALLOWED_INCLUDES = ['tags', 'authors', 'tiers'];
57

68
const messages = {
@@ -34,7 +36,7 @@ module.exports = {
3436
},
3537
permissions: true,
3638
query(frame) {
37-
return models.Post.findPage(frame.options);
39+
return postsPublicService.api.browse(frame.options);
3840
}
3941
},
4042

ghost/core/core/server/api/endpoints/posts-public.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const models = require('../../models');
22
const tpl = require('@tryghost/tpl');
33
const errors = require('@tryghost/errors');
4+
const postsPublicService = require('../../services/posts-public');
5+
46
const allowedIncludes = ['tags', 'authors', 'tiers', 'sentiment'];
57

68
const messages = {
@@ -34,7 +36,7 @@ module.exports = {
3436
},
3537
permissions: true,
3638
query(frame) {
37-
return models.Post.findPage(frame.options);
39+
return postsPublicService.api.browse(frame.options);
3840
}
3941
},
4042

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./service');
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class PostsPublicServiceWrapper {
2+
async init() {
3+
if (this.api) {
4+
// Already done
5+
return;
6+
}
7+
8+
// Wire up all the dependencies
9+
const {Post} = require('../../models');
10+
const adapterManager = require('../adapter-manager');
11+
const config = require('../../../shared/config');
12+
13+
let postsCache;
14+
if (config.get('hostSettings:postsPublicCache:enabled')) {
15+
postsCache = adapterManager.getAdapter('cache:postsPublic');
16+
}
17+
18+
const {PublicResourcesRepository} = require('@tryghost/public-resource-repository');
19+
20+
this.postsRepository = new PublicResourcesRepository({
21+
Model: Post,
22+
cache: postsCache
23+
});
24+
25+
this.api = {
26+
browse: this.postsRepository.getAll.bind(this.postsRepository)
27+
};
28+
}
29+
}
30+
31+
module.exports = new PostsPublicServiceWrapper();

0 commit comments

Comments
 (0)