1.5.0 • Published 1 month ago

ts-cache-mongoose v1.5.0

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

ts-cache-mongoose

Cache query and aggregate in mongoose using in-memory or redis

npm npm GitHub \ Coverage Quality Gate Status \ Reliability Rating Maintainability Rating Security Rating

npm

Motivation

ts-cache-mongoose is a plugin for mongoose \ Caching queries is a good way to improve performance of your application

Supports and tested with

{
  "node": "16.x || 18.x || 20.x",
  "mongoose": "6.6.x || 7.x || 8.x",
}

Features

  • In-memory caching
  • Redis caching
  • Cache expiration
  • Cache invalidation
  • Cache key generation
  • Cache key prefix
  • Query caching
  • Aggregate caching
  • Supports ESM and CommonJS

Installation

  • Locally inside your project
npm install ts-cache-mongoose
yarn add ts-cache-mongoose
pnpm add ts-cache-mongoose
bun add ts-cache-mongoose
  • This plugin requires mongoose 6.6.x || 7.x || 8.x to be installed as a peer dependency
# For mongoose 6
npm install mongoose@6.12.2
yarn add mongoose@6.12.2
pnpm add mongoose@6.12.2
bun add mongoose@6.12.2
# For mongoose 7
npm install mongoose@7.6.4
yarn add mongoose@7.6.4
pnpm add mongoose@7.6.4
bun add mongoose@7.6.4
# For mongoose 8
npm install mongoose@8.0.0
yarn add mongoose@8.0.0
pnpm add mongoose@8.0.0
bun add mongoose@8.0.0

Example

// On your application startup
import mongoose from 'mongoose'
import cache from 'ts-cache-mongoose'

// In-memory example 
cache.init(mongoose, {
  defaultTTL: '60 seconds',
  engine: 'memory',
})

// OR

// Redis example
cache.init(mongoose, {
  defaultTTL: '60 seconds',
  engine: 'redis',
  engineOptions: {
    host: 'localhost',
    port: 6379,
  },
})

// Connect to your database
mongoose.connect('mongodb://localhost:27017/my-database')

// Somewhere in your code
const users = await User.find({ role: 'user' }).cache('10 seconds').exec()
// Cache hit
const users = await User.find({ role: 'user' }).cache('10 seconds').exec()

const book = await Book.findById(id).cache('1 hour').exec()
const bookCount = await Book.countDocuments().cache('1 minute').exec()
const authors = await Book.distinct('author').cache('30 seconds').exec()

const books = await Book.aggregate([
  {
    $match: {
      genre: 'fantasy',
    },
  },
  {
    $group: {
      _id: '$author',
      count: { $sum: 1 },
    },
  },
  {
    $project: {
      _id: 0,
      author: '$_id',
      count: 1,
    },
  }
]).cache('1 minute').exec()

// Cache invalidation

// To clear all cache, don't use in production unless you know what you are doing
await cache.clear()

// Instead use custom cache key
const user = await User.findById('61bb4d6a1786e5123d7f4cf1').cache('1 minute', 'some-custom-key').exec()
await cache.clear('some-custom-key')

Check my other projects

1.5.0

1 month ago

1.4.7

2 months ago

1.4.6

2 months ago

1.4.5

3 months ago

1.4.4

4 months ago

1.4.3

5 months ago

1.4.2

5 months ago

1.4.1

6 months ago

1.4.0

6 months ago

1.3.0

6 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.9

9 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

11 months ago

1.1.2

8 months ago

1.0.4

12 months ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago