1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/lib/Cache.js

34 lines
794 B
JavaScript
Raw Normal View History

2018-01-17 19:59:33 +02:00
class Cache {
async getItem(name) {
let output = null;
try {
const storage = await Cache.storage();
output = await storage.getItem(name);
} catch (error) {
// console.info(error);
2018-01-17 19:59:33 +02:00
// 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');
2019-09-19 23:51:18 +02:00
await Cache.storage_.init({ dir: `${require('os').tmpdir()}/joplin-cache`, ttl: 1000 * 60 });
2018-01-17 19:59:33 +02:00
return Cache.storage_;
2019-07-29 15:43:53 +02:00
};
2018-01-17 19:59:33 +02:00
2019-07-29 15:43:53 +02:00
module.exports = Cache;