diff --git a/ghost/core/core/server/services/posts-public/service.js b/ghost/core/core/server/services/posts-public/service.js index 584f0b5e42..fe1e2890f2 100644 --- a/ghost/core/core/server/services/posts-public/service.js +++ b/ghost/core/core/server/services/posts-public/service.js @@ -8,34 +8,18 @@ class PostsPublicServiceWrapper { // Wire up all the dependencies 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 postsCache; if (config.get('hostSettings:postsPublicCache:enabled')) { - const cache = adapterManager.getAdapter('cache:postsPublic'); - postsCache = new EventAwareCacheWrapper({ - cache: cache, - resetEvents: ['site.changed'], - eventRegistry: EventRegistry + postsCache = adapterManager.getAdapter('cache:postsPublic'); + EventRegistry.on('site.changed', () => { + postsCache.reset(); }); } - let cache; - if (postsCache) { - // @NOTE: exposing cache through getter and setter to not loose the context of "this" - cache = { - get() { - return postsCache.get(...arguments); - }, - set() { - return postsCache.set(...arguments); - } - }; - } - this.api = { - cache: cache + cache: postsCache }; } } diff --git a/ghost/core/core/server/services/tags-public/service.js b/ghost/core/core/server/services/tags-public/service.js index 3cc82bde3b..fe59154696 100644 --- a/ghost/core/core/server/services/tags-public/service.js +++ b/ghost/core/core/server/services/tags-public/service.js @@ -8,33 +8,18 @@ class TagsPublicServiceWrapper { // Wire up all the dependencies 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')) { - let tagsPublicCache = adapterManager.getAdapter('cache:tagsPublic'); - tagsCache = new EventAwareCacheWrapper({ - cache: tagsPublicCache, - resetEvents: ['site.changed'], - eventRegistry: EventRegistry + tagsCache = adapterManager.getAdapter('cache:tagsPublic'); + EventRegistry.on('site.changed', () => { + tagsCache.reset(); }); } - 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 = { - cache: cache + cache: tagsCache }; } } diff --git a/ghost/core/package.json b/ghost/core/package.json index 29cc16299e..cda7cc93dd 100644 --- a/ghost/core/package.json +++ b/ghost/core/package.json @@ -87,7 +87,6 @@ "@tryghost/email-service": "0.0.0", "@tryghost/email-suppression-list": "0.0.0", "@tryghost/errors": "1.2.26", - "@tryghost/event-aware-cache-wrapper": "0.0.0", "@tryghost/express-dynamic-redirects": "0.0.0", "@tryghost/external-media-inliner": "0.0.0", "@tryghost/helpers": "1.1.88", diff --git a/ghost/event-aware-cache-wrapper/.eslintrc.js b/ghost/event-aware-cache-wrapper/.eslintrc.js deleted file mode 100644 index c9c1bcb522..0000000000 --- a/ghost/event-aware-cache-wrapper/.eslintrc.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - plugins: ['ghost'], - extends: [ - 'plugin:ghost/node' - ] -}; diff --git a/ghost/event-aware-cache-wrapper/README.md b/ghost/event-aware-cache-wrapper/README.md deleted file mode 100644 index d304f180c3..0000000000 --- a/ghost/event-aware-cache-wrapper/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# Event Aware Cache Wrapper - -Cache wrapper allowing to reset the cache after certain events - - -## Usage - - -## Develop - -This is a monorepo package. - -Follow the instructions for the top-level repo. -1. `git clone` this repo & `cd` into it as usual -2. Run `yarn` to install top-level dependencies. - - - -## Test - -- `yarn lint` run just eslint -- `yarn test` run lint and tests - diff --git a/ghost/event-aware-cache-wrapper/index.js b/ghost/event-aware-cache-wrapper/index.js deleted file mode 100644 index 9935b2737b..0000000000 --- a/ghost/event-aware-cache-wrapper/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./lib/EventAwareCacheWrapper'); diff --git a/ghost/event-aware-cache-wrapper/lib/EventAwareCacheWrapper.js b/ghost/event-aware-cache-wrapper/lib/EventAwareCacheWrapper.js deleted file mode 100644 index 59b73e36c7..0000000000 --- a/ghost/event-aware-cache-wrapper/lib/EventAwareCacheWrapper.js +++ /dev/null @@ -1,38 +0,0 @@ -class EventAwareCacheWrapper { - #cache; - /** - * @param {Object} deps - * @param {Object} deps.cache - cache instance extending adapter-base-cache - * @param {Object} [deps.eventRegistry] - event registry instance - * @param {String[]} [deps.resetEvents] - event to listen to triggering reset - */ - constructor(deps) { - this.#cache = deps.cache; - - if (deps.resetEvents && deps.eventRegistry) { - this.#initListeners(deps.eventRegistry, deps.resetEvents); - } - } - - #initListeners(eventRegistry, eventsToResetOn) { - eventsToResetOn.forEach((event) => { - eventRegistry.on(event, () => { - this.reset(); - }); - }); - } - - async get(key) { - return this.#cache.get(key); - } - - async set(key, value) { - return this.#cache.set(key, value); - } - - reset() { - return this.#cache.reset(); - } -} - -module.exports = EventAwareCacheWrapper; diff --git a/ghost/event-aware-cache-wrapper/package.json b/ghost/event-aware-cache-wrapper/package.json deleted file mode 100644 index d207279f5f..0000000000 --- a/ghost/event-aware-cache-wrapper/package.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "name": "@tryghost/event-aware-cache-wrapper", - "version": "0.0.0", - "repository": "https://github.com/TryGhost/Ghost/tree/main/packages/event-aware-cache-wrapper", - "author": "Ghost Foundation", - "private": true, - "main": "index.js", - "scripts": { - "dev": "echo \"Implement me!\"", - "test:unit": "NODE_ENV=testing c8 --all --check-coverage --100 --reporter text --reporter cobertura -- mocha --reporter dot './test/**/*.test.js'", - "test": "yarn test:unit", - "lint:code": "eslint *.js lib/ --ext .js --cache", - "lint": "yarn lint:code && yarn lint:test", - "lint:test": "eslint -c test/.eslintrc.js test/ --ext .js --cache" - }, - "files": [ - "index.js", - "lib" - ], - "devDependencies": { - "@tryghost/adapter-cache-memory-ttl": "0.0.0", - "c8": "8.0.1", - "mocha": "10.2.0", - "sinon": "15.2.0" - }, - "dependencies": {} -} diff --git a/ghost/event-aware-cache-wrapper/test/.eslintrc.js b/ghost/event-aware-cache-wrapper/test/.eslintrc.js deleted file mode 100644 index 829b601eb0..0000000000 --- a/ghost/event-aware-cache-wrapper/test/.eslintrc.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - plugins: ['ghost'], - extends: [ - 'plugin:ghost/test' - ] -}; diff --git a/ghost/event-aware-cache-wrapper/test/EventAwareCacheWrapper.test.js b/ghost/event-aware-cache-wrapper/test/EventAwareCacheWrapper.test.js deleted file mode 100644 index 62860eb92a..0000000000 --- a/ghost/event-aware-cache-wrapper/test/EventAwareCacheWrapper.test.js +++ /dev/null @@ -1,48 +0,0 @@ -const assert = require('assert/strict'); -const InMemoryCache = require('@tryghost/adapter-cache-memory-ttl'); - -const EventAwareCacheWrapper = require('../index'); -const {EventEmitter} = require('stream'); - -describe('EventAwareCacheWrapper', function () { - it('Can initialize', function () { - const cache = new InMemoryCache(); - const wrappedCache = new EventAwareCacheWrapper({ - cache - }); - assert.ok(wrappedCache); - }); - - describe('get', function () { - it('calls a wrapped cache with extra key', async function () { - const cache = new InMemoryCache(); - const wrapper = new EventAwareCacheWrapper({ - cache: cache - }); - - await wrapper.set('a', 'b'); - assert.equal(await wrapper.get('a'), 'b'); - assert.equal(await cache.get('a'), 'b'); - }); - }); - - describe('listens to reset events', function () { - it('resets the cache when reset event is triggered', async function () { - const cache = new InMemoryCache(); - - const eventRegistry = new EventEmitter(); - const wrapper = new EventAwareCacheWrapper({ - cache: cache, - resetEvents: ['site.changed'], - eventRegistry: eventRegistry - }); - - await wrapper.set('a', 'b'); - assert.equal(await wrapper.get('a'), 'b'); - - eventRegistry.emit('site.changed'); - - assert.equal(await wrapper.get('a'), undefined); - }); - }); -});