Ghost/core/server/services/url/cache.js
Hannah Wolfe 5e5b90ac29
Added Url Service to track all URLs in the system (#9247)
refs #9192

- Introduces a url service that can be initialised
- Added a concept of Resources and resource config.json that contains details about the resources in the system that we may want to make customisable 
- Note that individual resources know how to create their own Urls... this is important for later
- Url Service loads all of the resources, and stores their URLs
- The UrlService binds to all events, so that when a resource changes its url and related data can be updated if needed
- There is a temporary config guard so that this can be turned off easily
2017-11-16 13:03:24 +00:00

40 lines
1.1 KiB
JavaScript

'use strict';
// Based heavily on the settings cache
const _ = require('lodash'),
debug = require('ghost-ignition').debug('services:url:cache'),
events = require('../../events'),
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);
events.emit('url.added', key, value);
} else if (!_.isEqual(value, existing)) {
debug('overwriting url', key);
urlCache[key] = _.cloneDeep(value);
events.emit('url.edited', key, value);
}
},
unset(key) {
const value = this.get(key);
delete urlCache[key];
debug('removing url', key);
events.emit('url.removed', key, value);
},
get(key) {
return _.cloneDeep(urlCache[key]);
}
};