GithubHelp home page GithubHelp logo

leverz / co-cache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nswbmw/co-cache

1.0 2.0 0.0 12 KB

Cache result in redis for GeneratorFunction or function that return a Promise.

JavaScript 100.00%

co-cache's Introduction

co-cache

Cache result in redis for GeneratorFunction or function that return a Promise.

Install

npm i co-cache --save

Usage

var cache = require('co-cache')(defaultConfig);
cache(fn[, options]) => {GeneratorFunction|function->Promise}

defaultConfig {Object}:
options {Object|Number->expire}:

  • client: {Object} redis client of ioredis.
  • prefix: {String} prefix for redis cache, default ''.
  • key: {String|GeneratorFunction|function->Promise} prefix + key == cacheKey, default function.name, if return false, skip get&set cache.
  • expire: {Number->ms} expire in ms.
  • get: {Function} function to get cache, default JSON.parse.
  • set: {Function} function to set cache, default JSON.stringify.
  • others options see ioredis

NB: If both defaultConfig and options missing, cache will not work.

Example

var cache = require('co-cache')();

var getIndex = cache(function getIndex() {
  return client.db('test').collection('test').find().limit(10).toArray();
}, 10000);

var getTopicsByPage = cache(function* getTopicsByPage(p) {
  p = p || 1;
  return yield client.db('test').collection('test').find().skip((p - 1) * 10).limit(10).toArray();
}, {
  prefix: 'cache:',
  key: function (p) { // or function*
    if (p >= 3) {
      return false; // only cache 1-2 pages
    }
    return this.name + ':' + (p || 1);
  },
  expire: 10000
});

co(function* () {
  getIndex().then(function () { ... });
  var topics = yield getTopicsByPage(2);
  ...
}).catch(onerror);

or use defaultConfig:

var cache = require('co-cache')({
  expire: 10 * 1000
});

var getIndex = cache(function getIndex() {
  return client.db('test').collection('test').find().limit(10).toArray();
});

var getTopicsByPage = cache(function* getTopicsByPage(p) {
  p = p || 1;
  return yield client.db('test').collection('test').find().skip((p - 1) * 10).limit(10).toArray();
}, {
  prefix: 'cache:',
  key: function (p) { // or function*
    if (p >= 3) {
      return false; // only cache 1-2 pages
    }
    return this.name + ':' + (p || 1);
  }
});

co(function* () {
  getIndex().then(function () { ... });
  var topics = yield getTopicsByPage(2);
  ...
}).catch(onerror);

Default get/set

function defaultGet(redis, cacheKey) {
  return redis.get(cacheKey).then(function (result) {
    if (result) {
      return JSON.parse(result);
    }
    return null;
  });
}

function defaultSet(redis, cacheKey, result, ms) {
  return redis.set(cacheKey, JSON.stringify(result), 'PX', ms);
}

Test

DEBUG=co-cache node --harmony example

License

MIT

co-cache's People

Contributors

leverz avatar nswbmw avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.