Compare View
Commits (2)
Changes
Showing 3 changed files Side-by-side Diff
counters.js
| ... | ... | @@ -0,0 +1,35 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +const redis = require('redis'); | |
| 4 | +const config = require('./config'); | |
| 5 | + | |
| 6 | +const redisClient = redis.createClient(config.redis || { host: '127.0.0.1' }); | |
| 7 | + | |
| 8 | +function composeKeyword(name, subname) { | |
| 9 | + return `CHONGLEE_MODEM_COUNTER_${name}_${subname || ''}`; | |
| 10 | +} | |
| 11 | + | |
| 12 | +exports.increment = (name, subname) => { | |
| 13 | + redisClient.INCR(composeKeyword(name, subname)); | |
| 14 | +} | |
| 15 | + | |
| 16 | +exports.reset = (name, subname) => { | |
| 17 | + redisClient.DEL(composeKeyword(name, subname)); | |
| 18 | +} | |
| 19 | + | |
| 20 | +exports.set = (name, subname, value) => { | |
| 21 | + redisClient.SET(composeKeyword(name, subname), Number(value)); | |
| 22 | +} | |
| 23 | + | |
| 24 | +exports.get = (name, subname) => { | |
| 25 | + return new Promise((resolve) => { | |
| 26 | + redisClient.GET(composeKeyword(name, subname), (err, reply) => { | |
| 27 | + if (err) { | |
| 28 | + resolve(0); | |
| 29 | + return; | |
| 30 | + } | |
| 31 | + | |
| 32 | + resolve(Number(reply) || 0); | |
| 33 | + }); | |
| 34 | + }); | |
| 35 | +} | |
| 0 | 36 | \ No newline at end of file |
package-lock.json