fastify-cacheman
v5.1.0
Published
Small and efficient cache provider for Fastify with In-memory, File and Redis.
Maintainers
Readme
fastify-cacheman
Small and efficient cache provider for Fastify with In-memory, File and Redis.
Important:
Fastify Cacheman since version 2.x.x is no longer include the Redis client library. Installing Redis client library is an optional.
If you still use NodeJS 12.x or below, you should stick with Fastify Cacheman version 2.x.x.
| Version | Fastify Version | Redis Library Version | Node.JS | |---|---|---|---| | ^2.x | ^3.x | 3.x | 10, 12 | | ^3.x | ^4.x | 4.x | 14, 16, 18, 20 | | ^5.x | ^5.x | 5.x | 20, 22 |
Install using NPM
$ npm install fastify-cachemanUsage
JavaScript
const fastify = require('fastify')()
const fastifyCacheman = require('fastify-cacheman')
fastify.register(fastifyCacheman, {
namespace: 'fastify',
engine: 'redis',
client: 'redis://localhost:6379'
})
fastify.get('/', async (req, reply) => {
await fastify.cacheman.set('tester', 'world', 120) // this will cached for 120 seconds
reply.send({ hello: await fastify.cacheman.get('tester') })
})Typescript
import fastify from 'fastify'
import fastifyCacheman from 'fastify-cacheman'
fastify.register(fastifyCacheman, {
namespace: 'fastify',
engine: 'redis',
client: 'redis://localhost:6379'
})
fastify.get('/', async (req, reply) => {
await fastify.cacheman.set('tester', 'world', 120) // this will cached for 120 seconds
reply.send({ hello: await fastify.cacheman.get('tester') })
})API
options
// using memory
var options = {
engine: 'memory'
}
// using file
var options = {
engine: 'file'
}
// using redis
var options = {
engine: 'redis',
port: 6379,
host: '127.0.0.1',
password: 'my-p@ssw0rd'
database: 1
};
// or using redis with url connection string
// Example: redis[s]://[[username][:password]@][host][:port][/db-number]
var options = {
engine: 'redis',
url: 'redis://localhost:6379'
}
// or using redis client with connection string
var options = {
engine: 'redis',
client: 'redis://localhost:6379'
}
// or using redis client with redis instance directly
// Note:
// - Fastify Cacheman start from version 2.x.x, we excluded redis npm package,
// if you want to use redis client, you need to install it by yourself, for example >> npm install redis@5
var options = {
engine: 'redis',
client: require('redis').createClient('redis://localhost:6379')
}For more details, please see Redis Client Configuration Options.
cacheman.set(key, value, [ttl, [fn]])
Stores or updates a value.
fastify.cacheman.set('foo', { a: 'bar' }, function (err, value) {
if (err) throw err;
console.log(value); //-> {a:'bar'}
});Or add a TTL(Time To Live) in seconds like this:
// key will expire in 60 seconds
fastify.cacheman.set('foo', { a: 'bar' }, 60, function (err, value) {
if (err) throw err;
console.log(value); //-> {a:'bar'}
});Note:
You can also use human readable values for ttl like: 1s, 1m, etc. Check out the ms project for additional information on supported formats.
cacheman.get(key, fn)
Retrieves a value for a given key, if there is no value for the given key a null value will be returned.
fastify.cacheman.get(function (err, value) {
if (err) throw err;
console.log(value);
});cacheman.del(key, [fn])
Deletes a key out of the cache.
fastify.cacheman.del('foo', function (err) {
if (err) throw err;
// foo was deleted
});cacheman.clear([fn])
Clear the cache entirely, throwing away all values.
fastify.cacheman.clear(function (err) {
if (err) throw err;
// cache is now clear
});Note:
- Support using callback, promise and async await style.
- Typescript support.
There are more spesific API for each engine type, please see:
Unit test
npm test
