fastify plugin for memoizing responses by expressive settings
fastify plugin for memoize responses by expressive settings.
Use arbitrary cache to serve responses from previous elaboration, matching them by request and response.
npm i fastify-peekaboo
const fastify = require('fastify')
const peekaboo = require('fastify-peekaboo')
const fs = require('fs')
const _fastify = fastify()
_fastify.register(peekaboo, {
// default settings: cache good stuff for 1 day
rules: [{
request: {
methods: true,
route: true
},
response: {
status: (code) => code > 199 && code < 300
}
}],
mode: 'memoize',
storage: { mode: 'memory' },
expire: 86400000, // 1 day in ms
xheader: true,
log: false
})
_fastify.get('/home', async (request, response) => {
const _home = 'welcome!'
response.send(_home)
})
_fastify.get('/image', async (request, response) => {
response.send(fs.createReadStream('image.png'))
})
await _fastify.listen(80)
First call to /home
or /image
will execute the handler; from second time content will be served straight from the cache without running the handlers.
Cache storage can be memory
(ram), fs
.
dataset
feature allow to have different caches and switching between them.
Example: create a new dataset and use it
fastify.post('/dataset', async (request, response) => {
try {
const id = await _fastify.peekaboo.dataset.create(request.body.name)
await _fastify.peekaboo.dataset.set(id)
response.send({ id })
} catch (error) {
response.send({ message: error.message })
}
})
See documentation for full information and examples.
If you are using memory
storage, cache is volatile, no action is required.
In order to keep cache using fs
storage, move dir and content from peekaboo
to peekaboo/default
; otherwise, a new empty cache is created.
Update API calls for set.mode
and get.mode
to mode.set
and mode.get
.
Cache works by matching request and response.
If request
(and response
) match, response
is saved by hashing the matching request
.
The first rule that match the request is choosen.
{
rules: MatchingRule[],
mode: Mode,
storage: Storage,
expire: number,
xheader: boolean,
noinfo: boolean
}
The set of rules that indicate to use cache or not for requests. See matching system for details.
type: string
, one of memoize
, off
, collector
, stock
default: memoize
It set how the cache system behave:
404
if the cache entry does not existsYou can get/set also at runtime by
fastify.get('/cache/mode', async (request, response) => {
response.send({ mode: fastify.peekaboo.mode.get() })
})
fastify.get('/cache/mode/:mode', async (request, response) => {
fastify.peekaboo.mode.set(request.params.mode)
response.send('set mode ' + request.params.mode)
})
mode
type: string
[ memory
| fs
]
default: memory
memory
(default) cache use runtime memoryfs
use filesystem, need also config.path
storage config
type: object
for file
mode
path
string
{
mode: 'memory'
}
{
mode: 'fs',
config: { path: '/tmp/peekaboo' }
}
See storage documentation for further information about to access and manipulate entries.
type: number
default: 86400000
// 1 day
cache expiration in ms, optional.
type: boolean
default: true
add on response header x-peekaboo
and x-peekaboo-hash
if response comes from cache.
type: boolean
default: false
do not store info (matching rule, request) for entries, in order to speed up a little bit in write/read cache and save space; info are needed only for cache manipulation.
Use server log settings
fastify({ logger: true })
See documentation for further information and examples.
v. 2.3.1 [ 2021-03-02 ]
v. 2.3.0 [ 2021-03-01 ]
v. 2.2.0 [ 2020-10-11 ]
function
allow cache based on values, see notes
v. 2.0.0 [ 2020-09-25 ]
dataset
featuremode
public methodsv. 1.3.0 [ 2020-07-25 ]
fastify v3
v. 1.2.0-beta [ 2020-06-18 ] beta
beta
stagemode
(memoize, off, collector, stock)get
, list
, set
, rm
, clear
info
in stored entries and settings.noinfo
to skip thatx-peekaboo-hash
in xheaderv. 1.1.0-alpha [ 2020-05-14 ] alpha
keyv
storagev. 1.0.0-alpha [ 2020-05-03 ] alpha
superstruct
v. 0.5.0-beta [ 2020-04-30 ] beta
v. 0.4.0-beta [ 2019-05-21 ] beta
fastify v.2
v. 0.1.0-alpha [ 2018-12-27 ] alpha
got
in favor of natvie http
clientresponse.rewrite
optionrequest.rewrite
optionThe MIT License (MIT)
Copyright (c) 2018-2020 Simone Sanfratello
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.