SpeedGoose
This project is a next-level mongoose caching library which is fully written in typescript. It's caching on two levels. Shared - with redis. And local inside memory. Supports all mongoose operations like find,findOne,count,aggregate... and others. Also supports lean queries. Why it is different?
- It supports caching not only JSON objects in redis, but also whole Mongoose.Document instances in local memory to speed up code, and prevent unnecessary hydrations
- It has auto-clearing ability based on mongoose events. So if the query was cached for some records, and in the meantime that records changes, all cached related results will be cleared.
- Supports custom eventing. For example you wan't to remove given results from cache, but removal logic is not based on removing documents from db but rather field based (like
deleted: true
), then you can apply awasRecordDeleted
callback as an option for the plugin
Getting Started
This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.
Installation
npm install speedgoose
or
yarn add speedgoose
- Simple wrap your mongoose with the library (required)
import {applySpeedGooseCacheLayer} from "speedgoose";
import mongoose from "mongoose";
applySpeedGooseCacheLayer(mongoose, {
redisUri: process.env.REDIS_URI,
redisIndex : process.env.REDIS_INDEX_DB
})
- To enable auto-clearing for given schema, just add plugin to it (required)
import {SpeedGooseCacheAutoCleaner} from "speedgoose";
Schema.plugin(SpeedGooseCacheAutoCleaner)
// additionaly you can pass options for example callback for setting record as deleted
Schema.plugin(SpeedGooseCacheAutoCleaner, {wasRecordDeletedCallback} )
Usage
- With find, count etc...
model.find({}).cacheQuery()
model.find({}).sort({fieldA : 1}).cacheQuery()
model.find({}).lean().cacheQuery()
- With aggregation
model.aggregate([]).cachePipeline()
Roadmap
- Add more examples
- Deep hydration for nested documents
- Cache-based population
- Manual cache clearing for custom keys
- Flowchart of logic
- Tests
- Multitenant (tenant field indicator) support
- [ ]
See the open issues for a full list of proposed features (and known issues).
Contributing
License
Distributed under the MIT License. See LICENSE.txt
for more information.