-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (46 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var when = require('when')
/**
* generate a serial number using Redis
**/
function SerialNumber(redisClient) {
this.redisClient = redisClient
}
SerialNumber.prototype.init = function() {}
SerialNumber.prototype.next = function(key) {
var dfd = when.defer()
this.redisClient.incr('serial:'+key, function(err, value) {
if (err)
return dfd.reject(err)
dfd.resolve(value)
})
return dfd.promise
}
SerialNumber.prototype.get = function(key) {
var dfd = when.defer()
this.redisClient.get('serial:'+key, function(err, value) {
if (err)
return dfd.reject(err)
dfd.resolve(value)
})
return dfd.promise
}
SerialNumber.prototype.set = function(key, value) {
var dfd = when.defer()
this.redisClient.set('serial:'+key, value, function(err, value) {
if (err)
return dfd.reject(err)
dfd.resolve(value)
})
return dfd.promise
}
SerialNumber.prototype.__reset = function(key) {
var dfd = when.defer()
var that = this
this.redisClient.set('serial:'+key, 0, function(err) {
if (err)
return dfd.reject(err)
dfd.resolve(that.get('serial:'+key))
})
return dfd.promise
}
module.exports = SerialNumber