Giter VIP home page Giter VIP logo

recachegoose's Introduction

This is a fork version of cachegoose with following differences :

  • Minimum NodeJS 14
  • Removed old libraries
  • Fixing all vulnerables
  • Up to date
  • Typescript support
  • Replace cacheman with recacheman

recachegoose

Mongoose caching that actually works.

NPM

npm version CircleCI Coverage Status Known Vulnerabilities License NPM download/month NPM download total

About

A Mongoose caching module that works exactly how you would expect it to, with the latest version of Mongoose.

Important:
If you are using Mongoose 4.x or below, you have to use original cachegoose and use version <= 4.x of it.

Usage

  • Use In Memory
var mongoose = require('mongoose');
var cachegoose = require('recachegoose');

cachegoose(mongoose, {
  engine: 'memory'
});
  • Use File
var mongoose = require('mongoose');
var cachegoose = require('recachegoose');

cachegoose(mongoose, {
  engine: 'file'
});
  • Use Redis
var mongoose = require('mongoose');
var cachegoose = require('recachegoose');

cachegoose(mongoose, {
  engine: 'redis',
  port: 6379,
  host: 'localhost'
});

// or with redis url connection string
// redis[s]://[[username][:password]@][host][:port][/db-number]
cachegoose(mongoose, {
  engine: 'redis',
  url: 'redis://localhost:6379'
});

// or with redis client with connection string
// backwards compatibility
cachegoose(mongoose, {
  engine: 'redis',
  client: 'redis://localhost:6379'
});

// backwards compatibility
cachegoose(mongoose, {
  engine: 'redis',
  client: require('redis').createClient('redis://localhost:6379')
});
  • Set Cache
Record
  .find({ some_condition: true })
  .cache(30) // The number of seconds to cache the query.  Defaults to 60 seconds.
  .exec(function(err, records) { // You are able to use callback or promise
    ...
  });

Record
  .aggregate()
  .group({ total: { $sum: '$some_field' } })
  .cache(0) // Explicitly passing in 0 will cache the results indefinitely.
  .exec(function(err, aggResults) {
    ...
  });

You can also pass a custom key into the .cache() method, which you can then use later to clear the cached content.

var userId = '1234567890';

Children
  .find({ parentId: userId })
  .cache(0, userId + '-children') /* Will create a redis entry          */
  .exec(function(err, records) {  /* with the key '1234567890-children' */
    ...
  });

ChildrenSchema.post('save', function(child) {
  // Clear the parent's cache, since a new child has been added.
  cachegoose.clearCache(child.parentId + '-children');
});

Insert .cache() into the queries you want to cache, and they will be cached. Works with select, lean, sort, and anything else that will modify the results of a query.

Clearing the cache

If you want to clear the cache for a specific query, you must specify the cache key yourself:

function getChildrenByParentId(parentId, cb) {
  Children
    .find({ parentId })
    .cache(0, `${parentId}_children`)
    .exec(cb);
}

function clearChildrenByParentIdCache(parentId, cb) {
  cachegoose.clearCache(`${parentId}_children`, cb);
}

If you call cachegoose.clearCache(null, cb) without passing a cache key as the first parameter, the entire cache will be cleared for all queries.

Caching populated documents

When a document is returned from the cache, cachegoose will hydrate it, which initializes it's virtuals/methods. Hydrating a populated document will discard any populated fields (see Automattic/mongoose#4727). To cache populated documents without losing child documents, you must use .lean(), however if you do this you will not be able to use any virtuals/methods (it will be a plain object).

Test

For development mode, you have to use minimum nodejs 14

npm test

recachegoose's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

recachegoose's Issues

Cache returning invalid document

Hey,
on some queries, I receive the error, that the save() function isn't defined for the model. We just replaced cache goose with your library. The query looks like this:

       user = await this.findOne({_id: visitorId}).cache(300,cacheKey).exec();

      if(user) {
         ....
         user.save();
       }

After a while, we get:

TypeError: user.save is not a function

Typescript support

It would be useful to have Typescript support.

Currently this will generate a type validation error:

MyModel.findOne({ name: 'blah' }).cache(60)

A workaround is:

(MyModel.findOne({ name: 'blah' }) as any).cache(60)

Note, although someone could create a definition via the @types package, it would be good to have this as part of the project itself.

I'd propose an index.d.ts definition, but at this point I need to work out the right way to go about it.

add mongoose ^7.0.0

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/recachegoose/package.json b/node_modules/recachegoose/package.json
index 1e1092f..5b40668 100644
--- a/node_modules/recachegoose/package.json
+++ b/node_modules/recachegoose/package.json
@@ -14,7 +14,7 @@
     "sha1": "^1.1.1"
   },
   "peerDependencies": {
-    "mongoose": "^5.0.1 || ^6.0.0"
+    "mongoose": "^5.0.1 || ^6.0.0 || ^7.0.0"
   },
   "devDependencies": {
     "@babel/cli": "^7.18.10",

This issue body was partially generated by patch-package.

Property 'cache' does not exist

Property 'cache' does not exist on type 'Query<Document<unknown, {}, { files: DocumentArray<any> | any[] | { [x: string]: any; }[] | any[]; options: DocumentArray<any> | any[] | { [x: string]: any; }[] | any[]; key?: unknown; name?: unknown; description?: unknown; createdAt?: unknown; }> & Omit<...>, Document<...> & Omit<...>, {}, { ...; }, "findOne">'. Did you mean 'catch'? ts(2551)

This can be simply said that Query.cache() is not a function.

Versions:

"mongoose": "^7.2.2",
"recachegoose": "^9.2.0"

Reproduction

(Using Typescript)

  • middleware.ts
import mongoose from 'mongoose';

import cachegoose from 'recachegoose';

cachegoose(mongoose, {
  engine: 'memory'
});
  • fetch.ts
const result: Board = await Schema.findOne({ key: queries.id }).cache(0);

ERROR:

const result: Board = await Schema.findOne({ key: queries.id }).cache(0);
                                                                ^^^^^
Property 'cache' does not exist on type 'Query<Document<unknown, {}, { files: DocumentArray<any> | any[] | { [x: string]: any; }[] | any[]; options: DocumentArray<any> | any[] | { [x: string]: any; }[] | any[]; key?: unknown; name?: unknown; description?: unknown; createdAt?: unknown; }> & Omit<...>, Document<...> & Omit<...>, {}, { ...; }, "findOne">'. Did you mean 'catch'?

Edit:
Tried with a implementation similar to your documentation README.md. Its the same

Schema.findOne({ key: queries.id }).cache(0).exec(function(err, records) { console.log('Yay i work') });
Property 'cache' does not exist on type 'Query<Document<unknown, {}, { files: DocumentArray<any> | any[] | { [x: string]: any; }[] | any[]; options: DocumentArray<any> | any[] | { [x: string]: any; }[] | any[]; key?: unknown; name?: unknown; description?: unknown; createdAt?: unknown; }> & Omit<...>, Document<...> & Omit<...>, {}, { ...; }, "findOne">'. Did you mean 'catch'?

Property 'cache' does not exist

Property 'cache' does not exist on type 'Query<any[], any, {}, any>'. Did you mean 'catch'?

This can be simply said that Query.cache() is not a function.

Versions:

"mongoose": "^7.0.0",
"recachegoose": "^9.2.1",

code

(Using Typescript)

middleware.ts

import mongoose from 'mongoose';

import cachegoose from 'recachegoose';

cachegoose(mongoose, {
  engine: 'memory'
});

fetch.ts

results = await model.find({ key: queries.id }).cache(20);

ERROR:

const tempModel: mongoose.Model<any> = await this.model("collectionName");
results = await tempModel.find({ key: queries.id }).cache(20);
                                                   ^^^^^
Property 'cache' does not exist on type 'Query<any[], any, {}, any>'. Did you mean 'catch'?

findOne.cache is not a function

I am having trouble using this, with the following error being generated:

 person_1.default.findOne(...).cache is not a function

The database initialisation code:

let mongooseConnection;

function initQueryCaching (connection: any) {
    // exit early if caching is not to be used
    if (!config.database.cache || !config.database.cache.enabled) {
        logger.info('Database query caching: disabled');
        return;
    }

    const cacheConfig = config.database.cache;

    if (!cacheConfig) {
        throw new Error('No database cache config found');
    }

    cachegoose(connection, cacheConfig);
}

async function initDatabase (): Promise<Mongoose> {
    mongoose.Promise = Promise;

    const dbUrl = config.database.url;
    const options = config.database.options;

    const connection = await mongoose.connect(dbUrl, options);

    // Debugging for testing and development
    if (config.database.debug) {
        if (config.database.debugFile) {
            // We append to the file if it exists
            const stream = fs.createWriteStream(config.database.debugFile, { flags: 'a' });
            mongoose.set('debug', (collectionName, method, query, doc) => {
                stream.write(`${moment.utc().toISOString()} [mongodb] ${collectionName}->${method}->${JSON.stringify(query)}\n`);
            });
        } else {
            mongoose.set('debug', true);
        }
    }

    initQueryCaching (connection);

    // import after connection established
    await import('../models');

    mongooseConnection = connection;

    return connection;
}

Then in use:

const result = await Person.findOne({ email: '[email protected]' }).cache(120);

I do notice the result type for findOne() is DocumentQuery.

Any ideas as to what could be causing this?

Environment

  • Nodejs: 14.15.1
  • mongoose: 5.9.26
  • mongodb (package): 3.5.9
  • mongodb server: 5.0.2
  • recachegoose: 8.1.0
  • OS: macOS

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.