@ackee/kesha
v0.4.0
Published
Caching utils based on ioredis and dataloaders
Downloads
348
Readme
Kesha
Caching utils based on ioredis and dataloaders
Quick start 🚀
- Initialize ioredis client
import Redis from 'ioredis';
const redisClient = new Redis({
// Host, DB,...
})
- Use exported utils directly f.e. rateLimiter
import { rateLimiter } from 'kesha';
const rateLimitedFn = rateLimiter(redisClient, (number: number) => {
console.log('Called')
}, '', 1000)
- or create set of utils for client with createRedisRepository
import { createRedisRepository } from 'kesha';
const repo = createRedisRepository(client)
const rateLimitedFn = repo.rateLimiter((number: number) => {
console.log('Called')
}, '', 1000)
- Provide typehints for
setJSON
andgetJSON
methods
// Per key
const repoPerKey = createRedisRepository<{ 'test:key': string }>(client)
repoPerKey.setJSON('test:key', {}) // TS Error: {} is not assignable to string
// Using template litarals to define key prefixes (From TS 4.4+)
const repo = createRedisRepository<{ [K: `test:key:${number}`]: string }>(client)
repo.setJSON('test:key:1', {}) // TS Error: {} is not assignable to string
repo.getJSON('test:key:1') // TS: Returns string
repo.getJSON('test:key:a') // TS: Returns any
Tests 🧪
- Prepare docker container with Redis 🐳
sudo docker-compose -f docker-compose/docker-compose.yml up
- Run the tests
npm run test