Ghost/core/server/services/url/Resource.js
kirrg001 860718f584 Minimized cached data on resource add/update
refs #9601

- on resource update/add we have cached mobiledoc, html etc
- we have to ensure we exclude the fields (same procdure happens on bootstrap)
- these excluded fields don't have to be cached
- otherwise memory usage is higher in general
- ensure we cache relations with a minimal field set on resource update/add
2018-08-12 14:23:32 +02:00

60 lines
1.2 KiB
JavaScript

const EventEmitter = require('events').EventEmitter,
common = require('../../lib/common');
class Resource extends EventEmitter {
constructor(type, obj) {
super();
this.data = {};
this.config = {
type: type,
reserved: false
};
Object.assign(this.data, obj);
}
getType() {
return this.config.type;
}
reserve() {
if (!this.config.reserved) {
this.config.reserved = true;
} else {
common.logging.error(new common.errors.InternalServerError({
message: 'Resource is already taken. This should not happen.',
code: 'URLSERVICE_RESERVE_RESOURCE'
}));
}
}
release() {
this.config.reserved = false;
}
isReserved() {
return this.config.reserved === true;
}
update(obj) {
Object.assign(this.data, obj);
if (!this.isReserved()) {
return;
}
this.emit('updated', this);
}
remove() {
if (!this.isReserved()) {
return;
}
this.emit('removed', this);
}
}
module.exports = Resource;