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

countredis

v0.1.5

Published

Count On Redis Keys

Readme

countredis - a node.js counter power by redis.

Build Status

countredis可以非常简单地统计项目中的各种数据。

安装

npm install countredis

使用方法

首先加载countredis库:

var countredis = require('countredis');

而后创建countredis连接:

var client = countredis.createClient([port,[host,[auth,[prefix]]]]);

其中 posthost 是Redis数据库的端口号和主机地址, auth 是redis使用的密码, prefix 用来指定 countredis 向Re is加入的键的前缀以防止命名冲突,默认值是 count:

如果程序中已经使用node_redis库建立了到Redis的连接,也可以将该实例传入createClient函数:

var redis = require('redis').createClient();
var client = ranaly.createClient(redis, prefix);

推荐 : 最好传入已经创建好的redis连接,这样可以选择redis的数据库,默认是第一个数据库。

countredis 支持3种数据类型,分别是 AmountRealtimeDataList

Amount

创建一个Amount实例:

var users = new client.Amount('Users');

incr

incr方法用来增加实例的值,如每当有新用户注册时可以通过如下方法增加用户数量:

users.incr(function (err, total) {
	console.log('用户总数为:' + total + '个');
});

incr 函数的定义是:

	incr([increment, [when, [callback]])

其中 increment 指增加的数量,默认为1。 when 指增长发生的时间, Date 类型,默认为 new Date() ,即当前时间。 callback 的第二个参数返回增长后的总数。

get

get 方法用来获取实例在若干个时间的数量,如:

users.get(['20130218', '20130219'], function (err, result) {
	console.log(result);
});

第一个参数是时间的数组,时间的表示方法为 YYYYMMDDYYYYMMDDHH 。如想获取今天和当前小时的注册用户数量:

var now = moment(); // 需要使用moment库
users.get([now.format('YYYYMMDD'), now.format('YYYYMMDDHH')], function (err, results) {
	console.log('今天新注册的用户数量:' + results[0]);
	console.log('当前小时新注册的用户数量:' + results[1]);
});

sum

sum 方法用来获取实例在若干个时间内总共的数量,使用方法和 get 一样,不再赘述。特例是当第一个参数为空时, sum 会返回该Amount实例的总数。如:

users.sum([], function (err, result) {
	console.log('用户总数为:' + total + '个');
});

Realtime

创建一个Realtime实例:

var memory = new client.Realtime('Memory');

incr

incr 方法用来递增实例的值,如增加当前内存占用的空间:

memory.incr(1, function (err, result) {
	console.log('当前内存占用为:' + result);
});

其中第一个参数表示增加的数量,如果省略则默认为1。

set

set 方法用来设置实例的值,如:

memory.set(20);

get

get 方法用来获得实例的值,如:

memory.get(function (err, result) {
	console.log('当前内存占用为:' + result);
});

实时通知

当修改了某个Realtime实例的值后,ranaly会使用Redis的PUBLISH命令派发通知,channel可以通过实例的channel属性获得,如:

var sub = redis.createClient();
sub.subscribe(memory.channel);
sub.on('message', function (channel, message) {
	if (channel === memory.channel) {
		console.log('当前内存占用为:' + message);
	}
});

DataList

创建一个DataList实例:

var userAvatars = new client.DataList('Avatars');

push

push 方法用来向实例加入一个元素,可以是字符串、数组、数组或对象类型,如:

userAvatars.push({
    	url: 'http://demo.com/avatar.png',
	    userID: 17
    }, 50, function (err, length) {
        console.log(err);
        console.log(length);
    });
});

其中第二个参数表示保留的记录数量,默认为100。

len

len 方法用来获得实例的大小,如:

userAvatars.len(function (err, length) {
    console.log(err);
    console.log(length);
});

range

range 方法用来获得队列中的某个片段,第一个参数表示起始元素索引,第二个元素表示末尾元素索引。索引从0开始,支持负索引,即-1表示队列中最后一个元素。如:

userAvatars.range(0, -1, function (err, avatars) {
	avatars.forEach(function (avatar) {
		console.log(avatar.url);
	});
});