2017-11-16 16:03:24 +03:00
|
|
|
'use strict';
|
|
|
|
// Based heavily on the settings cache
|
|
|
|
const _ = require('lodash'),
|
|
|
|
debug = require('ghost-ignition').debug('services:url:cache'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../lib/common'),
|
2017-11-16 16:03:24 +03:00
|
|
|
urlCache = {};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
/**
|
|
|
|
* Get the entire cache object
|
|
|
|
* Uses clone to prevent modifications from being reflected
|
|
|
|
* @return {{}} urlCache
|
|
|
|
*/
|
|
|
|
getAll() {
|
|
|
|
return _.cloneDeep(urlCache);
|
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
const existing = this.get(key);
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
debug('adding url', key);
|
|
|
|
urlCache[key] = _.cloneDeep(value);
|
2017-12-12 00:47:46 +03:00
|
|
|
common.events.emit('url.added', key, value);
|
2017-11-16 16:03:24 +03:00
|
|
|
} else if (!_.isEqual(value, existing)) {
|
|
|
|
debug('overwriting url', key);
|
|
|
|
urlCache[key] = _.cloneDeep(value);
|
2017-12-12 00:47:46 +03:00
|
|
|
common.events.emit('url.edited', key, value);
|
2017-11-16 16:03:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
unset(key) {
|
|
|
|
const value = this.get(key);
|
|
|
|
delete urlCache[key];
|
|
|
|
debug('removing url', key);
|
2017-12-12 00:47:46 +03:00
|
|
|
common.events.emit('url.removed', key, value);
|
2017-11-16 16:03:24 +03:00
|
|
|
},
|
|
|
|
get(key) {
|
|
|
|
return _.cloneDeep(urlCache[key]);
|
|
|
|
}
|
|
|
|
};
|