ketting
Version:
Opiniated HATEAOS / Rest client.
54 lines • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShortCache = void 0;
const forever_1 = require("./forever");
/**
* ShortCache stores items in the cache for a short time.
*
* This cache can be a good choice if your server heavily relies
* on HTTP cache headers and Ketting runs in your browser, or if in general
* you want very up-to-date data.
*
* The reason in this scenarios it's useful to still have a 'very temporary'
* cache, is because during many operations `get()` may be called in rapid
* succession, and it also allows for enough time for 'embedded items' to
* pe placed in the cache and extracted again.
*/
class ShortCache extends forever_1.ForeverCache {
/**
* Create the short cache.
*
* cacheTimeout is specified in ms.
*/
constructor(cacheTimeout = 30000) {
super();
this.cacheTimeout = cacheTimeout;
this.activeTimers = new Map();
}
/**
* Store a State object.
*
* This function will clone the state object before storing
*/
store(state) {
super.store(state);
this.setTimer(state.uri);
}
setTimer(uri) {
if (this.activeTimers.has(uri)) {
clearTimeout(this.activeTimers.get(uri));
}
// If there is a TON in the cache, this algorithm might
// be optimized by using a linked list and a single timeout
// for the 'next scheduled' expiry.
//
// The expectation is that this is not the case though, so this is the
// lazy/easy way.
this.activeTimers.set(uri, setTimeout(() => {
this.delete(uri);
this.activeTimers.delete(uri);
}, this.cacheTimeout));
}
}
exports.ShortCache = ShortCache;
//# sourceMappingURL=short.js.map