diff --git a/CliClient/app/app.js b/CliClient/app/app.js index 00e50aed1..158612deb 100644 --- a/CliClient/app/app.js +++ b/CliClient/app/app.js @@ -21,6 +21,7 @@ const os = require('os'); const fs = require('fs-extra'); const { cliUtils } = require('./cli-utils.js'); const EventEmitter = require('events'); +const Cache = require('lib/Cache'); class Application extends BaseApplication { @@ -34,6 +35,7 @@ class Application extends BaseApplication { this.allCommandsLoaded_ = false; this.showStackTraces_ = false; this.gui_ = null; + this.cache_ = new Cache(); } gui() { @@ -223,12 +225,8 @@ class Application extends BaseApplication { async commandMetadata() { if (this.commandMetadata_) return this.commandMetadata_; - const osTmpdir = require('os-tmpdir'); - const storage = require('node-persist'); - await storage.init({ dir: osTmpdir() + '/commandMetadata', ttl: 1000 * 60 * 60 * 24 }); - - let output = await storage.getItem('metadata'); - if (Setting.value('env') != 'dev' && output) { + let output = await this.cache_.getItem('metadata'); + if (output) { this.commandMetadata_ = output; return Object.assign({}, this.commandMetadata_); } @@ -242,7 +240,7 @@ class Application extends BaseApplication { output[n] = cmd.metadata(); } - await storage.setItem('metadata', output); + await this.cache_.setItem('metadata', output, 1000 * 60 * 60 * 24); this.commandMetadata_ = output; return Object.assign({}, this.commandMetadata_); diff --git a/ReactNativeClient/lib/Cache.js b/ReactNativeClient/lib/Cache.js new file mode 100644 index 000000000..b3f98ecc2 --- /dev/null +++ b/ReactNativeClient/lib/Cache.js @@ -0,0 +1,36 @@ +class Cache { + + async getItem(name) { + let output = null; + try { + const storage = await Cache.storage(); + output = await storage.getItem(name); + } catch (error) { + console.info(error); + // Defaults to returning null + } + return output; + } + + async setItem(name, value, ttl = null) { + try { + const storage = await Cache.storage(); + const options = {}; + if (ttl !== null) options.ttl = ttl; + await storage.setItem(name, value, options); + } catch (error) { + // Defaults to not saving to cache + } + } + +} + +Cache.storage = async function() { + if (Cache.storage_) return Cache.storage_; + Cache.storage_ = require('node-persist'); + const osTmpdir = require('os-tmpdir'); + await Cache.storage_.init({ dir: osTmpdir() + '/joplin-cache', ttl: 1000 * 60 }); + return Cache.storage_; +} + +module.exports = Cache; \ No newline at end of file