Skip to content

fast and simple cache using Redis

License

Notifications You must be signed in to change notification settings

day1co/fastcache

Repository files navigation

fastcache

fast and simple cache using redis

version

Getting Started

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 ]

withCache in real world

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;
}

Contributing

test

$ npm run test

build

$ npm run build

watch(continuous build)

$ npm start

may the SOURCE be with you...