workspace-integrations
Version:
Webex Workspace Integrations NodeJS SDK
42 lines (41 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cacheSec = 60 * 60;
/**
* Cache for remembering values that change relatively seldom,
* such as device and workspace metadata.
*/
class Cache {
constructor() {
this.store = {};
}
async fetch(http, url) {
if (this.get(url)) {
// console.log('got cached', url);
return this.get(url);
}
const res = await http.get(url);
// console.log('fetch', url);
this.set(url, res);
return res;
}
get(resource) {
const record = this.store[resource];
if (record) {
const age = (Date.now() - record.created) / 1000;
// console.log('age', age, 's vs', cacheSec, 's');
if (age < cacheSec) {
// console.log('got cached', resource);
return record.value;
}
}
return null;
}
set(resource, value) {
this.store[resource] = {
created: Date.now(),
value,
};
}
}
exports.default = Cache;