Get support for luin/node_ranaly

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

luin/node_ranaly

Ranaly - a node.js ranaly client

Build Status

Ranaly可以非常简单地统计项目中的各种数据,本项目是ranaly的node.js客户端。想要了解更多关于ranaly的介绍请访问ranaly项目主页

安装

npm install node_ranaly

使用方法

首先加载ranaly库:

var ranaly = require('node_ranaly');

而后创建ranaly连接:

var client = ranaly.createClient(port, host, keyPrefix);

其中posthost是Redis数据库的端口号和主机地址,keyPrefix用来指定ranaly向Redis加入的键的前缀以防止命名冲突,默认值是RANALY:

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

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

Ranaly支持3种数据类型,分别是Amount、Realtime和DataList。

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) {
});

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

len

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

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

range

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

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

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2