{"id":17241240,"url":"https://github.com/sinoon/countredis","last_synced_at":"2025-03-26T03:13:23.496Z","repository":{"id":29622041,"uuid":"33162779","full_name":"sinoon/countRedis","owner":"sinoon","description":null,"archived":false,"fork":false,"pushed_at":"2015-04-21T08:22:44.000Z","size":152,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T00:34:09.771Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sinoon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-31T03:27:49.000Z","updated_at":"2015-04-21T08:22:44.000Z","dependencies_parsed_at":"2022-09-03T14:51:43.828Z","dependency_job_id":null,"html_url":"https://github.com/sinoon/countRedis","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinoon%2FcountRedis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinoon%2FcountRedis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinoon%2FcountRedis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinoon%2FcountRedis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sinoon","download_url":"https://codeload.github.com/sinoon/countRedis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245579676,"owners_count":20638679,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T06:08:23.842Z","updated_at":"2025-03-26T03:13:23.476Z","avatar_url":"https://github.com/sinoon.png","language":"JavaScript","readme":"# countredis - a node.js counter power by redis.\n[![Build Status](https://travis-ci.org/luin/node_ranaly.png?branch=master)](https://travis-ci.org/luin/node_ranaly)\n\ncountredis可以非常简单地统计项目中的各种数据。\n\n## 安装\n```\nnpm install countredis\n```\n## 使用方法\n首先加载countredis库：\n```js\t\nvar countredis = require('countredis');\n```\n而后创建countredis连接：\n```js\nvar client = countredis.createClient([port,[host,[auth,[prefix]]]]);\n```\n其中 `post` 和 `host` 是Redis数据库的端口号和主机地址, `auth` 是redis使用的密码， `prefix` 用来指定 `countredis` 向Re\nis加入的键的前缀以防止命名冲突，默认值是 `count:` 。\n\n如果程序中已经使用[node_redis](https://github.com/mranney/node_redis)库建立了到Redis的连接，也可以将该实例传入createClient函数：\n```\nvar redis = require('redis').createClient();\nvar client = ranaly.createClient(redis, prefix);\n```\n**推荐** : 最好传入已经创建好的redis连接，这样可以选择redis的数据库，默认是第一个数据库。\n\n`countredis` 支持3种数据类型，分别是 `Amount`、`Realtime`和`DataList`。\n\n## Amount\n创建一个Amount实例：\n```js\nvar users = new client.Amount('Users');\n```\n### incr\n`incr`方法用来增加实例的值，如每当有新用户注册时可以通过如下方法增加用户数量：\n```js\nusers.incr(function (err, total) {\n\tconsole.log('用户总数为：' + total + '个');\n});\n```\n`incr` 函数的定义是：\n```\n\tincr([increment, [when, [callback]])\n```\n其中 `increment` 指增加的数量，默认为1。 `when` 指增长发生的时间， `Date` 类型，默认为 `new Date()` ，即当前时间。 `callback` 的第二个参数返回增长后的总数。\n\n### get\n`get` 方法用来获取实例在若干个时间的数量，如：\n```js\nusers.get(['20130218', '20130219'], function (err, result) {\n\tconsole.log(result);\n});\n```\n第一个参数是时间的数组，时间的表示方法为 `YYYYMMDD` 或 `YYYYMMDDHH` 。如想获取今天和当前小时的注册用户数量：\n```js\nvar now = moment(); // 需要使用moment库\nusers.get([now.format('YYYYMMDD'), now.format('YYYYMMDDHH')], function (err, results) {\n\tconsole.log('今天新注册的用户数量：' + results[0]);\n\tconsole.log('当前小时新注册的用户数量：' + results[1]);\n});\n```\n### sum\n`sum` 方法用来获取实例在若干个时间内总共的数量，使用方法和 `get` 一样，不再赘述。特例是当第一个参数为空时， `sum` 会返回该Amount实例的总数。如：\n```js\nusers.sum([], function (err, result) {\n\tconsole.log('用户总数为：' + total + '个');\n});\n```\n## Realtime\n创建一个Realtime实例：\n```js\nvar memory = new client.Realtime('Memory');\n```\n### incr\n`incr` 方法用来递增实例的值，如增加当前内存占用的空间：\n```js\nmemory.incr(1, function (err, result) {\n\tconsole.log('当前内存占用为：' + result);\n});\n```\n其中第一个参数表示增加的数量，如果省略则默认为1。\n\n### set\n`set` 方法用来设置实例的值，如：\n```js\nmemory.set(20);\n```\n### get\n`get` 方法用来获得实例的值，如：\n```js\nmemory.get(function (err, result) {\n\tconsole.log('当前内存占用为：' + result);\n});\n```\n### 实时通知\n当修改了某个Realtime实例的值后，ranaly会使用Redis的`PUBLISH`命令派发通知，`channel`可以通过实例的`channel`属性获得，如：\n```js\nvar sub = redis.createClient();\nsub.subscribe(memory.channel);\nsub.on('message', function (channel, message) {\n\tif (channel === memory.channel) {\n\t\tconsole.log('当前内存占用为：' + message);\n\t}\n});\n```\n## DataList\n创建一个DataList实例：\n```js\nvar userAvatars = new client.DataList('Avatars');\n```\n### push\n`push` 方法用来向实例加入一个元素，可以是字符串、数组、数组或对象类型，如：\n```js\nuserAvatars.push({\n    \turl: 'http://demo.com/avatar.png',\n\t    userID: 17\n    }, 50, function (err, length) {\n        console.log(err);\n        console.log(length);\n    });\n});\n```\n其中第二个参数表示保留的记录数量，默认为100。\n\n### len\n`len` 方法用来获得实例的大小，如：\n```js\nuserAvatars.len(function (err, length) {\n    console.log(err);\n    console.log(length);\n});\n```\n### range\n`range` 方法用来获得队列中的某个片段，第一个参数表示起始元素索引，第二个元素表示末尾元素索引。索引从0开始，支持负索引，即-1表示队列中最后一个元素。如：\n```js\nuserAvatars.range(0, -1, function (err, avatars) {\n\tavatars.forEach(function (avatar) {\n\t\tconsole.log(avatar.url);\n\t});\n});\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinoon%2Fcountredis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsinoon%2Fcountredis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinoon%2Fcountredis/lists"}