Hapi-Slap cache in Redis
url
access to Redis, default localhost.
expireIn
timer pattern that will be the cache expired, default 300 (5 minutes)
options: url: 'redis://127.0.0.1:6379/0' expireIn: 300
rule
Cache rule expireIn
will not use the timer pattern but rather the defined.
plugins: slap: rule: 'colors' expireIn: 60
clear
all reported caches will be removed, value with base rule
.
plugins: slap: clear: 'colors' \\ or 'colors' or 'colors' 'color-id'
request.getCache()
checks if to exist cache with base on property rule defined in route.
request.getCache(id)
checking if to exist cache with base in the id specific.
request.addCache(values)
adds cache for rule defined in route, values value that will be added to the cache.
request.addCache(values, id)
added cache with base on a specific value id.
request.clearCache()
removes the cache defined in the property clear of the route.
Practical Example:
Configuration Hapijs K7 and Slap
const Hapi = ;const path = ; let server = ; const dir = path; const register = register: options: models: dir adapter: connectionOptions: options: dialect: 'sqlite' register: /* or register: require('hapi-slap') options: { url: 'redis://127.0.0.1:6379/0', expireIn: 300 } */; server; server;
Create routes
server;
Create controller
const list = async { try const model = requestdatabaseColor; const options = attributes: FIELDSCOLOR offset: request limit: request ; const cache = await request; if cache return ; const values = await model; request; return ; catch err return reply; }; const create = async { try const model = requestdatabaseColor; request; const value = await model; return ; catch err return reply; }; const read = async { try const model = requestdatabaseColor; const id = requestparamsid; const options = attributes: FIELDSCOLOR where: id: id ; const cache = await request; if cache return ; const value = await model; if !value return reply; request; return ; catch err return reply; }; const update = async { try const model = requestdatabaseColor; const id = requestparamsid; const payload = requestpayload; const value = await model; if !value return reply; const valueUpdate = await value; request; return ; catch err return reply; }; const destroy = async { try const model = requestdatabaseColor; const options = attributes: FIELDSCOLOR where: id: id ; const value = await model; if !value return reply; await value; request; return ; catch err return reply; };