0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Refactored URL cache persistance

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

- This refactor is needed to accomodate incoming "resource cache" which shares a lot of common logic with how url cache is operated
This commit is contained in:
Naz 2021-11-16 12:04:12 +04:00 committed by naz
parent 1234a61b52
commit 9b309cf4e7
3 changed files with 28 additions and 23 deletions

View file

@ -110,7 +110,7 @@ async function initCore({ghostServer, config, bootLogger}) {
await jobService.shutdown();
});
ghostServer.registerCleanupTask(async () => {
await urlService.persistUrls();
await urlService.shutdown();
});
debug('End: Job Service');

View file

@ -22,11 +22,11 @@ class UrlService {
/**
*
* @param {Object} options
* @param {String} [options.urlCachePath] - path to store cached URLs at
* @param {String} [options.urlsCachePath] - cached URLs storage path
*/
constructor({urlCachePath} = {}) {
constructor({urlsCachePath} = {}) {
this.utils = urlUtils;
this.urlCachePath = urlCachePath;
this.urlsCachePath = urlsCachePath;
this.onFinished = null;
this.finished = false;
this.urlGenerators = [];
@ -320,7 +320,12 @@ class UrlService {
this.resources.initResourceConfig();
this.resources.initEvenListeners();
const persistedUrls = await this.fetchUrls();
let persistedUrls;
if (labs.isSet('urlCache')) {
persistedUrls = await this.readCacheFile(this.urlsCachePath);
}
if (persistedUrls) {
this.urls = new Urls({
urls: persistedUrls
@ -338,39 +343,39 @@ class UrlService {
});
}
async persistUrls() {
if (!labs.isSet('urlCache') || !this.urlCachePath) {
async shutdown() {
if (!labs.isSet('urlCache')) {
return null;
}
return fs.writeFile(this.urlCachePath, JSON.stringify(this.urls.urls, null, 4));
await this.persistToCacheFile(this.urlsCachePath, this.urls.urls);
}
async fetchUrls() {
if (!labs.isSet('urlCache') || !this.urlCachePath) {
return null;
}
async persistToCacheFile(filePath, data) {
return fs.writeFile(filePath, JSON.stringify(data, null, 4));
}
let urlsCacheExists = false;
let urls;
async readCacheFile(filePath) {
let cacheExists = false;
let cacheData;
try {
await fs.stat(this.urlCachePath);
urlsCacheExists = true;
await fs.stat(filePath);
cacheExists = true;
} catch (e) {
urlsCacheExists = false;
cacheExists = false;
}
if (urlsCacheExists) {
if (cacheExists) {
try {
const urlsFile = await fs.readFile(this.urlCachePath, 'utf8');
urls = JSON.parse(urlsFile);
const cacheFile = await fs.readFile(filePath, 'utf8');
cacheData = JSON.parse(cacheFile);
} catch (e) {
//noop as we'd start a long boot process if there are any errors in the file
}
}
return urls;
return cacheData;
}
/**

View file

@ -5,8 +5,8 @@ const UrlService = require('./UrlService');
// NOTE: instead of a path we could give UrlService a "data-resolver" of some sort
// so it doesn't have to contain the logic to read data at all. This would be
// a possible improvement in the future
const urlCachePath = path.join(config.getContentPath('data'), 'urls.json');
const urlService = new UrlService({urlCachePath});
const urlsCachePath = path.join(config.getContentPath('data'), 'urls.json');
const urlService = new UrlService({urlsCachePath});
// Singleton
module.exports = urlService;