UNPKG

@aws-lambda-powertools/parameters

Version:
30 lines (29 loc) 771 B
/** * Class to represent a value that can expire. * * Upon creation, the value is assigned a TTL (time to live) that is calculated * by adding the current time with the maximum age. */ class ExpirableValue { ttl; value; /** * * @param value - Value to be cached * @param maxAge - Maximum age in seconds for the value to be cached */ constructor(value, maxAge) { this.value = value; const timeNow = new Date(); this.ttl = timeNow.setSeconds(timeNow.getSeconds() + maxAge); } /** * Check if the value has expired. * * @returns {boolean} - True if the value has expired, false otherwise */ isExpired() { return this.ttl < Date.now(); } } export { ExpirableValue };