api-cache-mongoose

A NodeJS package to easily cache api calls to a MongoDB Database and update them after X minutes.


Keywords
nodejs, node, mongo, mongoose, mongodb, cache, api, apicall, call, update, interval
License
MIT
Install
npm install api-cache-mongoose@1.0.4

Documentation

api-cache-mongoose

NPM GitHub issues

What is this?*

api-cache-mongoose is an NPM package to easily cache API calls to a MongoDB database and update them after X minutes.

Install

npm install --save api-cache-mongoose

Usage

You will need to set up a mongoose connection. Check examples/database.js

var mongoose = require('mongoose');
var ApiCache = require("api-cache-mongoose")( mongoose );

ApiCache only receives two parameters, The target URL (String) and minutes between api calls (Number) default=5

app.get("/users", function(req, res){
    // ApiCache( url, minutes_between_call )
    ApiCache("https://jsonplaceholder.typicode.com/users", 30).then(function(response){
        // If the last call was less than 30 minutes ago response will load from the database
        // Else response will be fetched from the api url
        res.json(response);
    }, function(err){
        console.warn(err);
        res.json({ success : false });
    });
});

Additional Notes

  • Please report any issues here