react-simple-api
Version:
Create and cache API requests and responses
32 lines • 1.02 kB
JavaScript
var ApiCache = /** @class */ (function () {
function ApiCache() {
this.apiResponses = new Map();
}
// Ensure that there is only one instance of the cache in the application
ApiCache.getInstance = function () {
if (this.instance) {
return this.instance;
}
this.instance = new ApiCache();
return this.instance;
};
ApiCache.prototype.getCachedResponse = function (id) {
return this.apiResponses.get(id);
};
ApiCache.prototype.setCachedResponse = function (id, data, cacheExpiry) {
var _this = this;
this.apiResponses.set(id, data);
if (cacheExpiry && cacheExpiry > 0) {
setTimeout(function () {
_this.deleteCachedResponse(id);
}, cacheExpiry);
}
return data;
};
ApiCache.prototype.deleteCachedResponse = function (id) {
this.apiResponses.delete(id);
};
return ApiCache;
}());
export { ApiCache };
//# sourceMappingURL=cache.js.map