https://github.com/capaj/memredis
https://github.com/capaj/memredis
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/capaj/memredis
- Owner: capaj
- Created: 2024-11-03T22:30:41.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-04T07:39:04.000Z (over 1 year ago)
- Last Synced: 2025-07-09T13:08:05.112Z (12 months ago)
- Language: TypeScript
- Size: 35.2 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @capaj/memredis
๐ Redis-backed memoization for your async functions
## Features
๐ Cache async function results in Redis
โก Fast lookups using customizable cache keys
โฐ Configurable TTL (Time To Live)
๐ Debug logging support
๐งน Easy cache clearing - both complete and per-key
๐ Works with ioredis client
## Installation
```bash
pnpm install @capaj/memredis
```
# Usage
First, create your memRedis instance with your Redis client:
```typescript
import { createMemRedis } from '@capaj/memredis'
import { Redis } from 'ioredis' // or any other Redis client
const redis = new Redis()
const memRedis = createMemRedis(redis)
const memoizedGetUser = memRedis(
async (userId: string) => {
// your expensive operation here
return await db.getUserData(userId)
},
{
maxAge: 3600, // cache for 1 hour
cachePrefix: 'user-data'
}
)
// Use the memoized function- this will cache the result in Redis
const userData = await memoizedGetUser.memoized('user123')
const userDataFromRedis = await memoizedGetUser.memoized('user123') // this will return the cached result
// Clear specific cache entry
await memoizedGetUser.clearKey('user123')
// Clear all cache entries for this function
await memoizedGetUser.clear()
```
## License
MIT