https://github.com/ackeecz/kesha
Caching utils based on ioredis and dataloaders
https://github.com/ackeecz/kesha
backend
Last synced: 6 months ago
JSON representation
Caching utils based on ioredis and dataloaders
- Host: GitHub
- URL: https://github.com/ackeecz/kesha
- Owner: AckeeCZ
- License: mit
- Created: 2022-06-07T16:53:00.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-07-17T08:13:08.000Z (almost 2 years ago)
- Last Synced: 2025-03-15T18:17:55.792Z (over 1 year ago)
- Topics: backend
- Language: TypeScript
- Homepage:
- Size: 62.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

# Kesha
Caching utils based on [ioredis](https://www.npmjs.com/package/ioredis)
and [dataloaders](https://github.com/graphql/dataloader)
## Quick start ๐
- Initialize [ioredis](https://www.npmjs.com/package/ioredis) client
```typescript
import Redis from 'ioredis';
const redisClient = new Redis({
// Host, DB,...
})
```
- Use exported utils directly f.e. [rateLimiter](src/lib/rateLimiter.ts)
```typescript
import { rateLimiter } from 'kesha';
const rateLimitedFn = rateLimiter(redisClient, (number: number) => {
console.log('Called')
}, '', 1000)
```
- or create set of utils for client with [createRedisRepository](src/lib/redisRepository.ts)
```typescript
import { createRedisRepository } from 'kesha';
const repo = createRedisRepository(client)
const rateLimitedFn = repo.rateLimiter((number: number) => {
console.log('Called')
}, '', 1000)
```
- Provide typehints for `setJSON` and `getJSON` methods
```typescript
// 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 ๐ณ
```bash
sudo docker-compose -f docker-compose/docker-compose.yml up
```
- Run the tests
```bash
npm run test
```