API
new Suya([options])
This Class expose suya API to developers’ to work with many cache store like redis, and memcached out of box. Though it work well with Node.js in-memory cache too.
const Cache = new Suya({
engine: {
name: [name],
configs: { [configs] },
logging: [boolean]
}
})
// [name] - the name of the in memory engine to use. e.g name: 'node-cache' | 'redis' | 'memcached'
// [configs] - the configurations for the selected engine.
// [boolean] - whether suya should/shouldn't log to console. This is only applicable to redis and memcached. e.g logging: true | false
Node Cache
// node-cache full api
const Cache = new Suya({
engine: {
name: 'node-cache',
}
})
Redis
// redis full api
const Cache = new Suya({
engine: {
name: 'redis',
configs: {
redis: {
options: {
// node-redis configurations options
// https://github.com/NodeRedis/node-redis#options-object-properties
host: '127.0.0.1', // Redis host
port: 6379, // Redis port
password: '[pass]', // Redis password
family: 4, // 4 (IPv4) or 6 (IPv6)
db: 0, // Redis database
},
}
},
logging: true
}
})
Memcached
// memcached full api
const Cache = new Suya({
engine: {
name: 'memcached',
configs: {
memcached: {
// server string format e.g
// single server - user:pass@server1:11211
// multiple servers - user:pass@server1:11211,user:pass@server2:11211
server: '',
// memjs configs options - https://github.com/memcachier/memjs
// some memjs options are overridden by suya. supported options are
// {
// retries: 2,
// retry_delay: 0.2,
// failoverTime: 60,
// }
options: {},
}
}
logging: true
}
})
.forever()
This is a midddleware to cache forever. The only method supported for this middleware is GET
// this is a middleware to cache forever.
const Cache = new Suya([options]).forever()
[Useful Tip]:
.forever()middleware should be use when your data doesn’t change AT ALL and use.resetOnMutate({ indicator: { [key]: [value] } })to make it upto date on every mutation (POST, PUT, PATCH, DELETE).
.duration([n])
This is a midddleware to cache for a specific duration (i.e time to live in cache engine) where [n] is the duration in seconds. The only method supported for this middleware is GET
// this is a middleware to cache for a specific duration.
const Cache = new Suya([options]).duration([n])
// [n] - time to live (TTL) in cache in seconds
[Useful Tip]:
.duration([n])middleware should be use when you are dealing with data that change often and don’t use.resetOnMutate({ indicator: { [key]: [value] } })on mutation because using will clear up the cache on every mutation (POST, PUT, PATCH, DELETE) hence no performance improvement..duration([n])clear cached data when TTL elapse.
.resetOnMutate([options])
This is a midddleware to reset cache on a successful mutation where [key] and [value] can be any indicator on successful mutation. The methods supported for this middleware are POST, PUT, PATCH, DELETE.
// this is a middleware to reset cache engine on successful mutation.
const Cache = new Suya([options]).resetOnMutate({
indicator: {
[key]: [value]
}
})
// [key] - key to watch for on api response(s).
// [value] - value to indicate the mutation was successful.
// this is a middleware to reset cache engine on successful mutation.
let Cache = new Suya([options]).resetOnMutate({
indicator: {
success: true,
}
})
[Useful Tip]: Always use indicator key and value that’s easy to read and understand. Though you’re free to use any key and value of your choice.
.close()
This is a helper method to close open connections to any external resources. This is NOT a middleware.
let Cache = new Suya([options])
process.on('unhandledRejection', (err, promise) => {
console.log(`Error: ${err.message}`)
// close the server
server.close(async () => {
// close connection
await Cache.close()
process.exit(1)
})
})
[Useful Tip]:
.close()helper method should be use when node proccess crashes unexpectedly to close open connections to any external resources.
⏮️ Getting Started | Usage ⏭️