Last active
June 8, 2018 03:20
-
-
Save dashmug/14947d90b2c83e93e6853900cae57eac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Don't | |
function handler(event, context) { | |
const s3 = new AWS.S3() | |
return s3.upload({}).promise() | |
} | |
// Better - Init variables outside the handler | |
const s3 = new AWS.S3() | |
function handler(event, context) { | |
return s3.upload({}).promise() | |
} | |
// Example for in-memory caching | |
const cache = {} | |
function handler(event, context) { | |
const now = Date.now() | |
if ('userTypes' in cache && now < cache.userTypes.expiry) { | |
// Data is cached and still fresh | |
// One less database query. | |
return userTypes.data | |
} | |
// Reset cache with fresh data. | |
cache.userTypes = { | |
data: getUserTypesFromDatabase, | |
expiry: now + TWO_HOURS | |
} | |
return cache.userTypes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment