mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
63723aa36a
closes #8037 🔥 Remove API-level default settings population - This is a relic! - We ALWAYS populate defaults on server start therefore this code could never run. - This was a lot of complicated code that wasn't even needed!! 🎨 Move settings cache - Move settings cache to be its own thing - Update all references - Adds TODOs for further cleanup 🎨 Create settings initialisation step - Create new settings library, which will eventually house more code - Unify the interface for initialising settings (will be more useful later) - Reduce number of calls to updateSettingsCache
67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
// It's important to keep the requires absolutely minimal here,
|
|
// As this cache is used in SO many other areas, we may open ourselves to
|
|
// circular dependency bugs.
|
|
var debug = require('debug')('ghost:settings:cache'),
|
|
events = require('../events'),
|
|
/**
|
|
* ## Cache
|
|
* Holds cached settings
|
|
* @type {{}}
|
|
*/
|
|
settingsCache = {};
|
|
|
|
/**
|
|
*
|
|
* IMPORTANT:
|
|
* We store settings with a type and a key in the database.
|
|
*
|
|
* {
|
|
* type: core
|
|
* key: dbHash
|
|
* value: ...
|
|
* }
|
|
*
|
|
* But the settings cache does not allow requesting a value by type, only by key.
|
|
* e.g. settingsCache.get('dbHash')
|
|
*/
|
|
module.exports = {
|
|
get: function get(key, options) {
|
|
if (!settingsCache[key]) {
|
|
return;
|
|
}
|
|
|
|
// Don't try to resolve to the value of the setting
|
|
if (options && options.resolve === false) {
|
|
return settingsCache[key];
|
|
}
|
|
|
|
// Default behaviour is to try to resolve the value and return that
|
|
try {
|
|
return JSON.parse(settingsCache[key].value);
|
|
} catch (err) {
|
|
return settingsCache[key].value;
|
|
}
|
|
},
|
|
set: function set(key, value) {
|
|
settingsCache[key] = value;
|
|
},
|
|
getAll: function getAll() {
|
|
return settingsCache;
|
|
},
|
|
init: function init() {
|
|
var self = this,
|
|
updateSettingFromModel = function updateSettingFromModel(settingModel) {
|
|
debug('Auto updating', settingModel.get('key'));
|
|
self.set(settingModel.get('key'), settingModel.toJSON());
|
|
};
|
|
// First, reset the cache
|
|
settingsCache = {};
|
|
|
|
// Bind to events to automatically keep up-to-date
|
|
events.on('settings.edited', updateSettingFromModel);
|
|
events.on('settings.added', updateSettingFromModel);
|
|
events.on('settings.deleted', updateSettingFromModel);
|
|
|
|
return settingsCache;
|
|
}
|
|
};
|