0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Added simple non-expiring caching to Posts API

refs https://github.com/TryGhost/Toolbox/issues/522

- The public posts "browse" endpoint is causing the most strain on the instance performance. Caching responses with small TTL would allow to reduce the amount of request processing.
This commit is contained in:
Naz 2023-02-22 15:31:05 +08:00
parent 3cfe6d2cbb
commit 0ddf0dd003
No known key found for this signature in database
3 changed files with 21 additions and 8 deletions

View file

@ -13,6 +13,7 @@ module.exports = {
docName: 'posts', docName: 'posts',
browse: { browse: {
cache: postsPublicService.api?.cache,
options: [ options: [
'include', 'include',
'filter', 'filter',

View file

@ -12,17 +12,29 @@ class PostsPublicServiceWrapper {
let postsCache; let postsCache;
if (config.get('hostSettings:postsPublicCache:enabled')) { if (config.get('hostSettings:postsPublicCache:enabled')) {
postsCache = adapterManager.getAdapter('cache:postsPublic'); const cache = adapterManager.getAdapter('cache:postsPublic');
postsCache = new EventAwareCacheWrapper({
cache: cache,
resetEvents: ['site.changed'],
eventRegistry: EventRegistry
});
} }
const {PublicResourcesRepository} = require('@tryghost/public-resource-repository'); let cache;
if (postsCache) {
this.postsRepository = new PublicResourcesRepository({ // @NOTE: exposing cache through getter and setter to not loose the context of "this"
Model: Post, cache = {
cache: postsCache get() {
}); return postsCache.get(...arguments);
},
set() {
return postsCache.set(...arguments);
}
};
}
this.api = { this.api = {
cache: cache,
browse: this.postsRepository.getAll.bind(this.postsRepository) browse: this.postsRepository.getAll.bind(this.postsRepository)
}; };
} }

View file

@ -10,7 +10,7 @@ module.exports = class PublicResourcesRepository {
/** /**
* @param {object} deps * @param {object} deps
* @param {object} deps.Model Bookshelf Model instance of TagPublic/Post/Author etc. * @param {object} deps.Model Bookshelf Model instance of TagPublic/Post/Author etc.
* @param {object} deps.cache cache instance * @param {object} [deps.cache] cache instance
*/ */
constructor(deps) { constructor(deps) {
this.#Model = deps.Model; this.#Model = deps.Model;