0.1.0 • Published 1 year ago

cache-bridge v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

cache-bridge

Simplify managing data between cache and database.

Features

  • Get data from cache, and automatically copy it from database when cache miss.
  • Will acquire the lock before accessing database, avoid Cache stampede.
  • Supports lru-cache.
  • Supports Redis, via node-redis or ioredis.

Example

import { createClient } from 'redis'
import { MongoClient, ObjectId } from 'mongodb'
import { createBridge, RedisCacheClient } from './src'

const redis = createClient()
const mongo = new MongoClient('mongodb://localhost:27017')

async function main (): Promise<void> {
  await redis.connect()
  await mongo.connect()

  const { bridge } = createBridge({
    cacheClient: new RedisCacheClient({ client: redis }),
    prefix: 'cache',
    ttl: 5000,
    // set how to get data from the DB
    get: async (id) => {
      return await mongo.db().collection('a').findOne({ _id: new ObjectId(id) })
    },
    // set how to get multiple data from the DB
    getMany: async (idList) => {
      const list = await mongo.db().collection('a')
        .find({
          _id: { $in: idList.map((id) => new ObjectId(id)) }
        })
        .toArray()
      return new Map(
        list.map((data) => [data._id.toHexString(), data])
      )
    }
  })

  const consoleData = async (id: string): Promise<void> => {
    console.log(await bridge.get(id))
  }

  // when called sequentially
  const id1 = '000000000000000000000001'
  await consoleData(id1) // get data from the DB
  await consoleData(id1) // get data from the cache
  await consoleData(id1) // get data from the cache

  // when called simultaneously
  const id2 = '000000000000000000000002'
  await Promise.all([
    consoleData(id2), // get data from the DB
    consoleData(id2), // wait for the previous line to store the data in the cache and get data from the cache
    consoleData(id2) // wait for and get data from the cache
  ])
}

main()
0.1.0

1 year ago

0.1.0-beta.3

1 year ago

0.1.0-beta.2

1 year ago

0.1.0-beta.1

1 year ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.4

3 years ago

0.0.1

3 years ago