Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@middy/cache
Advanced tools
Cache middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda
This middleware offers a simple but flexible caching layer that allows to cache the response associated to a given event and return it directly (without running the handler) if the same event is received again in a successive execution.
By default, the middleware stores the cache in memory, so the persistence is guaranteed only for a short amount of time (the duration of the container), but you can use the configuration layer to provide your own caching implementation.
To install this middleware you can use NPM:
npm install --save @middy/cache
calculateCacheId
(function) (optional): a function that accepts the event
object as a parameter
and returns a promise that resolves to a string which is the cache id for the
give request. By default the cache id is calculated as md5(JSON.stringify(event))
.getValue
(function) (optional): a function that defines how to retrieve the value associated to a given
cache id from the cache storage. It accepts key
(a string) and returns a promise
that resolves to the cached response (if any) or to undefined
(if the given key
does not exists in the cache)setValue
(function) (optional): a function that defines how to set a value in the cache. It accepts
a key
(string) and a value
(response object). It must return a promise that
resolves when the value has been stored.const middy = require('@middy/core')
const cache = require('@middy/cache')
// assumes the event contains a unique event identifier
const calculateCacheId = (event) => Promise.resolve(event.id)
// use in-memory storage as example
const myStorage = {}
// simulates a delay in retrieving the value from the caching storage
const getValue = (key) => new Promise((resolve, reject) => {
setTimeout(() => resolve(myStorage[key]), 100)
})
// simulates a delay in writing the value in the caching storage
const setValue = (key, value) => new Promise((resolve, reject) => {
setTimeout(() => {
myStorage[key] = value
return resolve()
}, 100)
})
const originalHandler = (event, context, cb) => {
/* ... */
}
const handler = middy(originalHandler)
.use(cache({
calculateCacheId,
getValue,
setValue
}))
For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.
Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.
Licensed under MIT License. Copyright (c) 2017-2018 Luciano Mammino and the Middy team.
FAQs
Cache middleware for the middy framework
The npm package @middy/cache receives a total of 7 weekly downloads. As such, @middy/cache popularity was classified as not popular.
We found that @middy/cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.