cachex

3.0.0 • Public • Published

cachex

Cache Hook

  • Build Status
  • NPM version
  • Dependencies Status
  • Coverage Status

NPM

Installation

$ npm install cachex --save

Usage

If you have origin SQL query, it is db.js:

exports.getRows = async function () {
  // mock slow query
  var rows = await db.query(sql);
  return rows;
};

Before use cachex, you must provider an cache storage, it can be redis or memcached or memory.

var inMemory = {};
 
var store = {
  get: async function (key) {
    return inMemory[key];
  },
  setex: async function (key, value, expire) {
    inMemory[key] = value;
    setTimeout(function () {
      delete inMemory[key];
    }, expire);
  }
};

The storage object must have get/setex yieldable method.

// db_with_cache.js
var cachex = require('cachex');
var db = require('./db');
 
// cache result 10s
export.getRows = cachex(store, 'db', 'getRows', db.getRows, 10);

Running go:

var db = require('./db_with_cache');
// from db
db.getRows();
// from cache
db.getRows();
// ..10s..pass..
// from db
db.getRows();

License

The MIT license

Readme

Keywords

Package Sidebar

Install

npm i cachex

Weekly Downloads

1

Version

3.0.0

License

MIT

Last publish

Collaborators

  • jacksontian