0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00

Moved cache storage initialization

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

- Passing "cache" through constructor did not work out because cache setting is still dependent upon on the model layer (gets called before it has a chance to initialize during db migrations)
- To remove the initialization dependency blockers were:
    "defaults" method in the post model - the value resolved to "undefined" anyway during the fixture insertion
    validate-password module - checks the password against "undefined" during fixture initialization
- Passing the cache through "init" method works too, but is not as clear as with constructor DI pattern.
This commit is contained in:
Naz 2022-08-04 11:57:46 +01:00 committed by naz
parent e9bfc4ef01
commit af0014917b
4 changed files with 13 additions and 10 deletions

View file

@ -67,7 +67,7 @@ module.exports = {
*/
async init() {
const settingsCollection = await models.Settings.populateDefaults();
SettingsCache.init(events, settingsCollection, this.getCalculatedFields());
SettingsCache.init(events, settingsCollection, this.getCalculatedFields(), {});
},
/**

View file

@ -14,11 +14,11 @@ const _ = require('lodash');
class CacheManager {
/**
* @prop {Object} options
* @prop {{}} options.cache - object of objects. Holds cached settings, keyed by setting.key, contains the JSON version of the model
* @prop {Object} options.publicSettings - key/value pairs of settings which are publicly accessible
*/
constructor({cache, publicSettings}) {
this.settingsCache = cache;
constructor({publicSettings}) {
// settingsCache holds cached settings, keyed by setting.key, contains the JSON version of the model
this.settingsCache;
this.publicSettings = publicSettings;
this.calculatedFields = [];
@ -47,7 +47,10 @@ class CacheManager {
}
_doGet(key, options) {
if (!this.settingsCache[key]) {
// NOTE: "!this.settingsCache" is for when setting's cache is used
// before it had a chance to initialize. Should be fixed when
// it is decoupled from the model layer
if (!this.settingsCache || !this.settingsCache[key]) {
return;
}
@ -143,9 +146,11 @@ class CacheManager {
* @param {EventEmitter} events
* @param {Bookshelf.Collection<Settings>} settingsCollection
* @param {Array} calculatedFields
* @param {Object} cacheStore - cache storage instance
* @return {object}
*/
init(events, settingsCollection, calculatedFields) {
init(events, settingsCollection, calculatedFields, cacheStore) {
this.settingsCache = cacheStore;
// First, reset the cache and
this.reset(events);

View file

@ -1,7 +1,6 @@
const CacheManager = require('./cache');
const publicSettings = require('./public');
const cache = {};
const cacheManager = new CacheManager({cache, publicSettings});
const cacheManager = new CacheManager({publicSettings});
module.exports = cacheManager;

View file

@ -14,10 +14,9 @@ describe('UNIT: settings cache', function () {
beforeEach(function () {
cache = new CacheManager({
cache: {},
publicSettings
});
cache.init(events, {}, []);
cache.init(events, {}, [], {});
});
afterEach(function () {