mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 20:03:12 +03:00
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:" } } }, ```
This commit is contained in:
parent
c3d7104367
commit
3ddfe59e15
@ -295,6 +295,7 @@ async function initServices({config}) {
|
|||||||
const emailAnalytics = require('./server/services/email-analytics');
|
const emailAnalytics = require('./server/services/email-analytics');
|
||||||
const mentionsService = require('./server/services/mentions');
|
const mentionsService = require('./server/services/mentions');
|
||||||
const tagsPublic = require('./server/services/tags-public');
|
const tagsPublic = require('./server/services/tags-public');
|
||||||
|
const postsPublic = require('./server/services/posts-public');
|
||||||
|
|
||||||
const urlUtils = require('./shared/url-utils');
|
const urlUtils = require('./shared/url-utils');
|
||||||
|
|
||||||
@ -313,6 +314,7 @@ async function initServices({config}) {
|
|||||||
members.init(),
|
members.init(),
|
||||||
tiers.init(),
|
tiers.init(),
|
||||||
tagsPublic.init(),
|
tagsPublic.init(),
|
||||||
|
postsPublic.init(),
|
||||||
membersEvents.init(),
|
membersEvents.init(),
|
||||||
permissions.init(),
|
permissions.init(),
|
||||||
xmlrpc.listen(),
|
xmlrpc.listen(),
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
const tpl = require('@tryghost/tpl');
|
const tpl = require('@tryghost/tpl');
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
const models = require('../../models');
|
const models = require('../../models');
|
||||||
|
const postsPublicService = require('../../services/posts-public');
|
||||||
|
|
||||||
const ALLOWED_INCLUDES = ['tags', 'authors', 'tiers'];
|
const ALLOWED_INCLUDES = ['tags', 'authors', 'tiers'];
|
||||||
|
|
||||||
const messages = {
|
const messages = {
|
||||||
@ -34,7 +36,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
permissions: true,
|
permissions: true,
|
||||||
query(frame) {
|
query(frame) {
|
||||||
return models.Post.findPage(frame.options);
|
return postsPublicService.api.browse(frame.options);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
const models = require('../../models');
|
const models = require('../../models');
|
||||||
const tpl = require('@tryghost/tpl');
|
const tpl = require('@tryghost/tpl');
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
|
const postsPublicService = require('../../services/posts-public');
|
||||||
|
|
||||||
const allowedIncludes = ['tags', 'authors', 'tiers', 'sentiment'];
|
const allowedIncludes = ['tags', 'authors', 'tiers', 'sentiment'];
|
||||||
|
|
||||||
const messages = {
|
const messages = {
|
||||||
@ -34,7 +36,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
permissions: true,
|
permissions: true,
|
||||||
query(frame) {
|
query(frame) {
|
||||||
return models.Post.findPage(frame.options);
|
return postsPublicService.api.browse(frame.options);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
1
ghost/core/core/server/services/posts-public/index.js
Normal file
1
ghost/core/core/server/services/posts-public/index.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
module.exports = require('./service');
|
31
ghost/core/core/server/services/posts-public/service.js
Normal file
31
ghost/core/core/server/services/posts-public/service.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
class PostsPublicServiceWrapper {
|
||||||
|
async init() {
|
||||||
|
if (this.api) {
|
||||||
|
// Already done
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wire up all the dependencies
|
||||||
|
const {Post} = require('../../models');
|
||||||
|
const adapterManager = require('../adapter-manager');
|
||||||
|
const config = require('../../../shared/config');
|
||||||
|
|
||||||
|
let postsCache;
|
||||||
|
if (config.get('hostSettings:postsPublicCache:enabled')) {
|
||||||
|
postsCache = adapterManager.getAdapter('cache:postsPublic');
|
||||||
|
}
|
||||||
|
|
||||||
|
const {PublicResourcesRepository} = require('@tryghost/public-resource-repository');
|
||||||
|
|
||||||
|
this.postsRepository = new PublicResourcesRepository({
|
||||||
|
Model: Post,
|
||||||
|
cache: postsCache
|
||||||
|
});
|
||||||
|
|
||||||
|
this.api = {
|
||||||
|
browse: this.postsRepository.getAll.bind(this.postsRepository)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = new PostsPublicServiceWrapper();
|
Loading…
Reference in New Issue
Block a user