/memredis

Primary LanguageTypeScript

@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

pnpm install @capaj/memredis

Usage

First, create your memRedis instance with your Redis client:

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