From b8bdd3e74276d07307ce9ac50747f39589b1166d Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 23 Feb 2023 12:47:26 +0800 Subject: [PATCH] Moved Tags Content API to api-level caching refs https://github.com/TryGhost/Toolbox/issues/522 - Caching on a repository level was pretty hard to achieve with more complex models like "posts", so that approach was abandoned in favor of API-response level caching. - Also removed use of "public-resource-repository" as it was not serving any specific purpose anymore. --- .../core/server/api/endpoints/tags-public.js | 3 +- .../server/services/tags-public/service.js | 31 +++++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/ghost/core/core/server/api/endpoints/tags-public.js b/ghost/core/core/server/api/endpoints/tags-public.js index e90d2aba10..1e9e19ce81 100644 --- a/ghost/core/core/server/api/endpoints/tags-public.js +++ b/ghost/core/core/server/api/endpoints/tags-public.js @@ -14,6 +14,7 @@ module.exports = { docName: 'tags', browse: { + cache: tagsPublicService.api?.cache, options: [ 'include', 'filter', @@ -32,7 +33,7 @@ module.exports = { }, permissions: true, query(frame) { - return tagsPublicService.api.browse(frame.options); + return models.TagPublic.findPage(frame.options); } }, diff --git a/ghost/core/core/server/services/tags-public/service.js b/ghost/core/core/server/services/tags-public/service.js index 42941dfe98..3cc82bde3b 100644 --- a/ghost/core/core/server/services/tags-public/service.js +++ b/ghost/core/core/server/services/tags-public/service.js @@ -6,24 +6,35 @@ class TagsPublicServiceWrapper { } // Wire up all the dependencies - const {TagPublic} = require('../../models'); const adapterManager = require('../adapter-manager'); const config = require('../../../shared/config'); + const EventAwareCacheWrapper = require('@tryghost/event-aware-cache-wrapper'); + const EventRegistry = require('../../lib/common/events'); let tagsCache; if (config.get('hostSettings:tagsPublicCache:enabled')) { - tagsCache = adapterManager.getAdapter('cache:tagsPublic'); + let tagsPublicCache = adapterManager.getAdapter('cache:tagsPublic'); + tagsCache = new EventAwareCacheWrapper({ + cache: tagsPublicCache, + resetEvents: ['site.changed'], + eventRegistry: EventRegistry + }); } - const {PublicResourcesRepository} = require('@tryghost/public-resource-repository'); - - this.tagsPublicRepository = new PublicResourcesRepository({ - Model: TagPublic, - cache: tagsCache - }); - + let cache; + if (tagsCache) { + // @NOTE: exposing cache through getter and setter to not loose the context of "this" + cache = { + get() { + return tagsCache.get(...arguments); + }, + set() { + return tagsCache.set(...arguments); + } + }; + } this.api = { - browse: this.tagsPublicRepository.getAll.bind(this.tagsPublicRepository) + cache: cache }; } }