Skip to content

Latest commit

 

History

History
80 lines (56 loc) · 1.97 KB

File metadata and controls

80 lines (56 loc) · 1.97 KB

npm version

MongoDB store for node cache manager

MongoDB cache store for node-cache-manager. This is a rewrite of v4l3r10/node-cache-manager-mongodb.

Installation

npm install @tirke/node-cache-manager-mongodb
yarn add @tirke/node-cache-manager-mongodb
pnpm add @tirke/node-cache-manager-mongodb

Usage Examples

Init

import { caching } from 'cache-manager'

import { mongoDbStore } from './node-cache-manager-mongodb'

const mongoCache = await caching(mongoDbStore, {
  url: 'mongodb://localhost:27017',
  mongoConfig: { auth: { password: '<password>', username: '<user>' } },
})

Generic usage

import { caching } from 'cache-manager'

import { mongoDbStore } from './node-cache-manager-mongodb'

const mongoCache = await caching(mongoDbStore, {
  url: 'mongodb://localhost:27017',
  mongoConfig: { auth: { password: '<password>', username: '<user>' } },
})

await mongoCache.set('foo', 'bar', { ttl: 5 })
const result = await mongoCache.get('foo')
await mongoCache.del('foo')

Custom collection name

import { caching } from 'cache-manager'

import { mongoDbStore } from './node-cache-manager-mongodb'

const mongoCache = await caching(mongoDbStore, {
  url: 'mongodb://localhost:27017',
  collectionName: 'custom-collection-name',
  mongoConfig: { auth: { password: '<password>', username: '<user>' } },
})

Custom database name

import { caching } from 'cache-manager'

import { mongoDbStore } from './node-cache-manager-mongodb'

const mongoCache = await caching(mongoDbStore, {
  url: 'mongodb://localhost:27017',
  databaseName: 'custom-database-name',
  mongoConfig: { auth: { password: '<password>', username: '<user>' } },
})