const cachedItem = {}; // as you know, anything declared outside the event handler function is reused between invocations. exports.handler = (event, context, callback) => { let data; if (cachedItem && cachedItem.expiresOn > Date.now()) { // Fetch data from cache data = cachedItem.value; } else { // Set the data in cache setCachedData("tobecaheedvalue"); } console.log("isDataCached", data); } const setCachedData = (val) => { cachedItem = { expiresOn: Date.now() + 3600 * 1000, // Set expiry time of 1H value: val } }
Note: Once you update/deploy Lambda’s function then all the cached storage will automatically be removed.
Anything declared outside the event handler function is reused between invocations