npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

querycache

v0.4.4

Published

Caching layer for optimizing mongodb read queries. The cache is invalidated on specified oplog events.

Downloads

15

Readme

querycache

This module provides an easy to use caching layer for any nodejs application that needs to optimize speed for mongodb read queries. The cache defaults to a in-memory data store, but also has support for redis, which makes it easy for multiple frontends to utilize the same cache. The cache automatically gets invalidated on any oplog events that reference the specified database and collections. For this reason, the mongodb instance must be configured as a replicaset. For testing purposes, this can be done by configuring mongo as a singleton replicaset:

mongod --replSet name
mongo --eval 'rs.initiate()'

Install

npm install querycache

Configuration

Configurations are managed by the following environmental variables

QC_ENABLE                     # To enable the cache set this to 'true'. Default
                              # behaviour is not to enable the cache.

QC_URI                        # The mongodb 'local' database connection string,
                              # defaults to "mongodb://127.0.0.1:27017/local".

QC_MAX_ENTRIES                # The maximum allowed number of entries to hold
                              # in cache, defaults to 100000. This setting can
                              # be overwritten by the ``maxEntries``
                              # constructor option.

QC_REDIS_PORT                 # The port to connect to redis on, defaults to
                              # 6379.
QC_REDIS_HOST                 # The redis host, defaults to '127.0.0.1'

If QC_MAX_ENTRIES is reached, the oldest entry is removed when a new query is cached.

Usage examples

In these examples the cache will be invalidated on any updates to either of the collections: 'test1' or 'test2' in the 'test' database.

var QueryCache = require('querycache');
var querycache = new QueryCache({
  dbName: 'test',
  collections: ['test1', 'test2']
});

querycache.set('some key', 'some value', function (err) {
  if (err) throw err;
  querycache.get('some key', function (err, value) {
    if (err) throw err;
    console.log(value);
  });
});
var mongo = require('mongodb').MongoClient;
var QueryCache = require('querycache');
var querycache = new QueryCache({
  dbName: 'test',
  collections: ['test1', 'test2']
});

var someQuery = { values: { $in: [ 'foo', 'bar' ] } };
mongo.connect('mongodb://127.0.0.1:27017/test', function (err, db) {
  if (err) throw err;
  querycache.get(someQuery, function (err, result) {
    if (err || !result) {
      var test1 = db.collection('test1');
      test1.findOne(someQuery, function (err, result) {
        if (err) throw err;
        querycache.set(someQuery, result, function (err) {
          if (err) console.error(err);
          console.log(result);
        });
      });
    } else {
      console.log(result);
    }
  });
});

Using redis as data store

var QueryCache = require('querycache');
var querycache = new QueryCache({
  dbName: 'test',
  collections: ['test1'],
  datastore: 'redis'
});

querycache.set('some key', 'some value', function (err) {
  if (err) throw err;
  querycache.get('some key', function (err, value) {
    if (err) throw err;
    console.log(value);
  });
});