fast and simple cache using redis
const { FastCache } = require('@fastcampus/fastcache');
const cache = FastCache.create({ redis: { host: '127.0.0.1', port: 6379, db: 0 } });
await cache.set('foo', 'hello');
await cache.get('foo');
// hello
const list = cache.list('bar');
await list.unshift('one');
await list.push('two');
await list.getAll();
// [ one, two ]
await list.shift();
// one
await list.pop();
// two
const map = cache.map('baz');
await map.set('one', 'first');
await map.set('two', 'second');
await map.get('one');
// first
await map.getAll(['one', 'two']);
// [ first, second ]
checkout example.ts
function getDataByIdWithCache(id) {
return this.cacheService.withCache(`course@${id}`, () => {
return this.getDataById(id);
});
}
async function getDataById(id) {
const course = await this.redstoneDataService.getDataById(id);
if (!course || !course.id) {
logger.warn('unavailable course requested %s', id);
return {};
}
return course;
}
$ npm run test
$ npm run build
$ npm start
may the SOURCE be with you...