mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 03:44:29 +03:00
Removed usage of EventAwareCacheAdapter
This logic is so simple it isn't worth having the indirection of another class. This also removes the indirection of wrapped getters/setters, which is useful because otherwise we need to update the wrapper with new methods each time theunderlying implementation is changed. There was a note about losing the context of this, but I haven't found anywhere that the context is lost.
This commit is contained in:
parent
c7d7b883cc
commit
c60dd779c9
@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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",
|
||||
|
@ -1,6 +0,0 @@
|
||||
module.exports = {
|
||||
plugins: ['ghost'],
|
||||
extends: [
|
||||
'plugin:ghost/node'
|
||||
]
|
||||
};
|
@ -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
|
||||
|
@ -1 +0,0 @@
|
||||
module.exports = require('./lib/EventAwareCacheWrapper');
|
@ -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;
|
@ -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": {}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
module.exports = {
|
||||
plugins: ['ghost'],
|
||||
extends: [
|
||||
'plugin:ghost/test'
|
||||
]
|
||||
};
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user