co-cache
Cache result in redis for AsyncFunction.
Last updated a year ago by nswbmw .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install co-cache 
SYNC missed versions from official npm registry.

co-cache

Cache result in redis for AsyncFunction.

Install

$ npm i co-cache --save

Usage

const cache = require('co-cache')(defaultConfig);
cache(fn[, options]) => AsyncFunction

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

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

Example

const Mongolass = require('mongolass')
const mongolass = new Mongolass()
mongolass.connect('mongodb://localhost:27017/test')

const cache = require('.')({
  prefix: 'cache:',
  expire: 10 * 1000 // default expire
})

const User = mongolass.model('User')

;(async function () {
  // create some users
  for (let i = 1; i <= 10; i++) {
    await User.insertOne({ name: i })
  }

  const getUsersByPage = cache(function getUsersByPage(p) {
    return User
      .find()
      .skip((p - 1) * 1)
      .limit(1)
  }, {
    key: function (p) {
      if (p >= 3) {
        return false // only cache 1-2 pages
      }
      return this.name + ':' + p
    }
  })

  await getUsersByPage(1)
  await getUsersByPage(2)
  await getUsersByPage(2)
  console.log(await getUsersByPage(3))

  await User.remove()

  process.exit()
})().catch(console.error)

Default get/set

function defaultGet (redis, cacheKey) {
  return redis
    .get(cacheKey)
    .then((result) => {
      if (result != null) {
        return JSON.parse(result)
      }
    })
    .catch(() => {})
}

function defaultSet (redis, cacheKey, result, ms) {
  // cannot save `undefined`` value, `null` is ok
  if (result === undefined) {
    return
  }
  return redis
    .set(cacheKey, JSON.stringify(result), 'PX', ms)
    .catch(() => {})
}

Test

$ npm run test

License

MIT

Current Tags

  • 3.0.0                                ...           latest (a year ago)

20 Versions

  • 3.0.0                                ...           a year ago
  • 2.5.0                                ...           3 years ago
  • 2.4.1                                ...           3 years ago
  • 2.4.0                                ...           3 years ago
  • 2.3.1                                ...           3 years ago
  • 2.3.0                                ...           4 years ago
  • 2.2.0                                ...           4 years ago
  • 2.1.1                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 1.2.0                                ...           5 years ago
  • 1.1.1                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.0.5                                ...           5 years ago
  • 0.0.4                                ...           5 years ago
  • 0.0.3                                ...           5 years ago
  • 0.0.2                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
  • 0.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 15
This Month 122
Last Day 5
Last Week 62
Last Month 130
Dependencies (2)

Copyright 2014 - 2016 © taobao.org |