UNPKG

@sap-cloud-sdk/core

Version:
77 lines 2.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Cache = exports.IsolationStrategy = void 0; var moment_1 = __importDefault(require("moment")); /** * Enumerator that selects the isolation type of destination in cache. */ var IsolationStrategy; (function (IsolationStrategy) { IsolationStrategy["Tenant"] = "Tenant"; IsolationStrategy["User"] = "User"; IsolationStrategy["Tenant_User"] = "TenantUser"; IsolationStrategy["No_Isolation"] = "NoIsolation"; })(IsolationStrategy = exports.IsolationStrategy || (exports.IsolationStrategy = {})); /** * Representation of a cache to transiently store objects locally for faster access. * @typeparam T - Type of the cache entries. */ var Cache = /** @class */ (function () { function Cache(validityTime) { this.cache = {}; this.defaultValidityTime = validityTime; } /** * Clear all cached items. */ Cache.prototype.clear = function () { this.cache = {}; }; /** * Specifies whether an entry with a given key is defined in cache. * @param key - The entry's key * @returns boolean A boolean value that indicates whether the entry exists in cache */ Cache.prototype.hasKey = function (key) { return this.cache.hasOwnProperty(key); }; /** * Getter of cached entries. * @param key - The key of the entry to retrieve. * @returns The corresponding entry to the provided key if it is still valid, returns `undefined` otherwise. */ Cache.prototype.get = function (key) { return key && this.hasKey(key) && !isExpired(this.cache[key]) ? this.cache[key].entry : undefined; }; /** * Setter of entries in cache. * @param key - The entry's key * @param entry - The entry to cache * @param expirationTime - The time expressed in UTC in which the given entry expires */ Cache.prototype.set = function (key, entry, expirationTime) { if (key) { var expires = expirationTime ? (0, moment_1.default)(expirationTime) : inferExpirationTime(this.defaultValidityTime); this.cache[key] = { entry: entry, expires: expires }; } }; return Cache; }()); exports.Cache = Cache; function isExpired(item) { if (item.expires === undefined) { return false; } return !item.expires.isAfter((0, moment_1.default)()); } function inferExpirationTime(expirationTime) { return expirationTime ? (0, moment_1.default)().add(expirationTime) : undefined; } //# sourceMappingURL=cache.js.map