Socket
Socket
Sign inDemoInstall

cache-manager-mongoose

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cache-manager-mongoose

mongoose store for node-cache-manager


Version published
Weekly downloads
952
decreased by-9.25%
Maintainers
1
Install size
8.91 kB
Created
Weekly downloads
 

Readme

Source

build status NPM version Coverage Status

cache-manager-mongoose

Mongoose store for node-cache-manager (See https://github.com/BryanDonovan/node-cache-manager)

Originally created as an alternative to node-cache-manager-mongodb store to be able to use an existing mongoose connection.

Usage examples

This store expects that the mongoose instance is provided explicitely.

It is not a concern of this module to establish a connection, but it rather assumes that you already have one and use it in your project.

const
    mongoose = require("mongoose"),
    cacheManager = require("cache-manager"),
    mongooseStore = require("cache-manager-mongoose");

mongoose.connect("mongodb://127.0.0.1/test");

const cache = cacheManager.caching({
    store: mongooseStore,
    mongoose: mongoose
});

// now you can use cache as any other cache-manager cache

Options

All optionas are optional, except for mongoose instance.

The store creates a new Model on initialization, which you can partially customize. See example:

const cache = cacheManager.caching({
    store: mongooseStore,
    mongoose: mongoose, // mongoose instance
    modelName: "MyModelName", // model name in mongoose registry

    // options for model creation
    modelOptions: {
        collection: "cacheman_rcp" // mongodb collection name
        versionKey: false // do not create __v field
    },

    ttl: 300 // time to live - 5 minutes (default is 1 minute),

    connection: connection, // provide only when using mongoose.createConnection()
});

If you want to keep your cache forever, set TTL to zero (0)

The default modelOptions are:

{
    "collection": "MongooseCache",
    "versionKey": false,
    "read": "secondaryPreferred"
}

Custom model

You can also provide your own model as long as it has the same fields as the one used by default.

In this case you don't need to provide a mongoose instance, as all it boils down to is a model object.

Here is an example:

const schema = new mongoose.Schema(
    {
        // standard fields
        _id: String,
        val: mongoose.Schema.Types.Mixed,
        exp: Date,

        // all other fields you like
        foo: String,
        bar: Number
    },
    {
        collection: "my_collection",
        versionKey: false
    }
);

schema.index(
    {exp: 1},
    {expireAfterSeconds: 0}
);
schema.index({foo: 1});

const model = mongoose.model("MyModel", schema);

const cache = cacheManager.caching({
    store: mongooseStore,
    model: model
});

License

MIT No Attribution

https://github.com/aws/mit-0

Keywords

FAQs

Last updated on 14 May 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc