Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first draft implementation of cache middleware * Added improved tests * Added documentation * Fixes after review * Version bump
- Loading branch information
Showing
8 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const middy = require('../../middy') | ||
const cache = require('../cache') | ||
|
||
describe('💽 Cache stuff', () => { | ||
test('It should cache things using the default settings', (endTest) => { | ||
const originalHandler = jest.fn((event, context, cb) => { | ||
cb(null, event.a + event.b) | ||
}) | ||
|
||
const handler = middy(originalHandler) | ||
.use(cache()) | ||
|
||
const event = {a: 2, b: 3} | ||
const context = {} | ||
handler(event, context, (_, response) => { | ||
handler(event, context, (_, response2) => { | ||
expect(response).toEqual(response2) | ||
expect(originalHandler.mock.calls.length).toBe(1) | ||
endTest() | ||
}) | ||
}) | ||
}) | ||
|
||
test('It should cache things using custom cache settings', (endTest) => { | ||
const calculateCacheId = (event) => Promise.resolve(event.id) | ||
const myStorage = {} | ||
const getValue = (key) => new Promise((resolve, reject) => { | ||
setTimeout(() => resolve(myStorage[key]), 50) | ||
}) | ||
const setValue = (key, value) => new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
myStorage[key] = value | ||
return resolve() | ||
}, 50) | ||
}) | ||
|
||
const originalHandler = jest.fn((event, context, cb) => { | ||
cb(null, event.a + event.b) | ||
}) | ||
|
||
const handler = middy(originalHandler) | ||
.use(cache({ | ||
calculateCacheId, | ||
getValue, | ||
setValue | ||
})) | ||
|
||
const event = {id: 'some_unique_id', a: 2, b: 3} | ||
const context = {} | ||
handler(event, context, (_, response) => { | ||
handler(event, context, (_, response2) => { | ||
expect(response).toEqual(response2) | ||
expect(originalHandler.mock.calls.length).toBe(1) | ||
endTest() | ||
}) | ||
}) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { createHash } = require('crypto') | ||
|
||
module.exports = (opts) => { | ||
const defaultStorage = {} | ||
const defaults = { | ||
calculateCacheId: (event) => Promise.resolve(createHash('md5').update(JSON.stringify(event)).digest('hex')), | ||
getValue: (key) => Promise.resolve(defaultStorage[key]), | ||
setValue: (key, value) => { | ||
defaultStorage[key] = value | ||
return Promise.resolve() | ||
} | ||
} | ||
|
||
const options = Object.assign({}, defaults, opts) | ||
let currentCacheKey | ||
|
||
return ({ | ||
before: (handler, next) => { | ||
options.calculateCacheId(handler.event) | ||
.then((cacheKey) => { | ||
currentCacheKey = cacheKey | ||
return options.getValue(cacheKey) | ||
}) | ||
.then((cachedResponse) => { | ||
if (typeof cachedResponse !== 'undefined') { | ||
return handler.callback(null, cachedResponse) | ||
} | ||
|
||
return next() | ||
}) | ||
}, | ||
after: (handler, next) => { | ||
// stores the calculated response in the cache | ||
options.setValue(currentCacheKey, handler.response) | ||
.then(next) | ||
} | ||
}) | ||
} |
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