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 IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck 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
Ranaly可以非常简单地统计项目中的各种数据,本项目是ranaly的node.js客户端。想要了解更多关于ranaly的介绍请访问ranaly项目主页。
安装
npm install node_ranaly
使用方法
首先加载ranaly库:
var ranaly = require('node_ranaly');
而后创建ranaly连接:
var client = ranaly.createClient(port, host, keyPrefix);
其中post
和host
是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);
});
第一个参数是时间的数组,时间的表示方法为YYYYMMDD
或YYYYMMDDHH
。如想获取今天和当前小时的注册用户数量:
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.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2