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 🙏

© 2025 – Pkg Stats / Ryan Hefner

moa-middleware-rate-cache

v1.2.1

Published

moa-middleware-rate-cache is an expressjs-based middleware!

Readme

rate-cache

moa-middleware-rate-cache is an expressjs-based middleware!

npm version

Install

$ npm install --save moa-middleware-rate-cache

Usages

way 1:

var rate_cache = require('moa-middleware-rate-cache');
var r = new rate_cache(redis, 'xxxxx_key', 40);

参数

  • redis, 传入redis对象
  • 'xxxxx_key', 在redis里缓存的key
  • 40(秒)缓存时间

way 2:

var rate_cache = require('moa-middleware-rate-cache');
var r = new rate_cache(redis, 'xxxxx_key2222');

参数

  • redis, 传入redis对象
  • 'xxxxx_key', 在redis里缓存的key

默认缓存时间是30秒

result message

  • rate_cache.msg_not_exist
{
	data: {},
	status: {
	  code: -1,
	  msg: '生成货单编号失败'
	}
}
  • rate_cache.msg_exist
{
	data: {},
	status: {
	  code: -2,
	  msg: '您的订单已经在处理,不要急嘛~'
	}
}

如果想重置就直接修改rate_cache即可。

Extra API

  • rate_cache.set_uni_key(new_key)
  • rate_cache.mark_key_exist()

Examples

由于rate-cache依赖redis,所以推荐luin写的ioredis

var Redis = require('ioredis');
var redis = new Redis();
var rate_cache = require('moa-middleware-rate-cache');
var r = new rate_cache(redis, 'xxxxx_key', 40);

app.get('/', r.middleware, function (req, res) {
  r.mark_key_exist();
  
  res.status(200).json({
    data:{},
    status:{
      code: 0,
      msg : 'success!'
    }
  });
});

上面是最典型的场景,xxxxx_key是针对某一个表或者某一个操作的key,还有一种更为苛刻的,比如根据表单内容或者某个用户,使用中间件配置即可

var Redis = require('ioredis');
var redis = new Redis();
var rate_cache = require('moa-middleware-rate-cache');
var r2 = new rate_cache(redis,'xxxxx_key111');

var rate_cache_config = function(req, res, next){
  var user_id = req.current_user._id;
  rate_cache.uni_key = user_id + "_create_delivery"
  
  next();
}

app.get('/', rate_cache_config, r2.middleware, function (req, res) {
  r2.mark_key_exist();
  
  res.status(200).json({
    data:{},
    status:{
      code: 0,
      msg : 'success!'
    }
  });
});

redis内存用量查看: https://github.com/sripathikrishnan/redis-rdb-tools