1 | var EventEmitter = require('events').EventEmitter;
|
2 | var util = require('util');
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var stringify = function (value) {
|
8 | return typeof(value) === "object" ?
|
9 | JSON.stringify(value) :
|
10 | value + '';
|
11 | };
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var RedisItem = function (type, expire) {
|
17 |
|
18 |
|
19 | this.type = type || 0;
|
20 | this.expires = expire || -1;
|
21 | };
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var RedisString = function (value, expires) {
|
27 | RedisItem.call(this, "string", expires);
|
28 | this.value = String(value);
|
29 | };
|
30 | util.inherits(RedisString, RedisItem);
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | var RedisBuffer = function (value, expires) {
|
36 | RedisItem.call(this, "buffer", expires);
|
37 | this.value = (value instanceof Buffer) ? value : new Buffer(value);
|
38 | };
|
39 | util.inherits(RedisBuffer, RedisItem);
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | var RedisHash = function () {
|
45 | RedisItem.call(this, "hash");
|
46 | this.value = {};
|
47 | };
|
48 | util.inherits(RedisHash, RedisItem);
|
49 |
|
50 |
|
51 | var RedisList = function () {
|
52 | RedisItem.call(this, "list");
|
53 | this.value = [];
|
54 | };
|
55 | util.inherits(RedisList, RedisItem);
|
56 |
|
57 | RedisList.prototype.rpush = function (values) {
|
58 | for (var i = 0; i < values.length; i++) {
|
59 | this.value.push(stringify(values[i]));
|
60 | }
|
61 | };
|
62 |
|
63 | RedisList.prototype.lpush = function (values) {
|
64 | for (var i = 0; i < values.length; i++) {
|
65 | this.value.unshift(stringify(values[i]));
|
66 | }
|
67 | };
|
68 |
|
69 | RedisList.prototype.rpop = function (value) {
|
70 | return this.value.pop();
|
71 | };
|
72 |
|
73 | RedisList.prototype.lpop = function (value) {
|
74 | return this.value.shift();
|
75 | };
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | var RedisSet = function () {
|
81 | RedisItem.call(this, "set");
|
82 | this.value = [];
|
83 | }
|
84 | util.inherits(RedisSet, RedisItem);
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | var RedisSortedSet = function () {
|
90 | RedisItem.call(this, "zset");
|
91 | this.value = {};
|
92 | }
|
93 | util.inherits(RedisSortedSet, RedisItem);
|
94 |
|
95 | var RedisItemFactory = {
|
96 | _item: RedisItem,
|
97 | _string: RedisString,
|
98 | _buffer: RedisBuffer,
|
99 | _hash: RedisHash,
|
100 | _list: RedisList,
|
101 | _set: RedisSet,
|
102 | _sortedset: RedisSortedSet,
|
103 | _stringify: stringify
|
104 | };
|
105 |
|
106 | RedisItemFactory.createString = function (elt, expire) {
|
107 | return new RedisString(elt, expire);
|
108 | };
|
109 |
|
110 | RedisItemFactory.createBuffer = function (elt, expire) {
|
111 | return new RedisBuffer(elt, expire);
|
112 | };
|
113 |
|
114 | RedisItemFactory.createHash = function () {
|
115 | return new RedisHash();
|
116 | };
|
117 |
|
118 | RedisItemFactory.createList = function () {
|
119 | return new RedisList();
|
120 | };
|
121 |
|
122 | RedisItemFactory.createSet = function () {
|
123 | return new RedisSet();
|
124 | }
|
125 |
|
126 | RedisItemFactory.createSortedSet = function () {
|
127 | return new RedisSortedSet();
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | module.exports = exports = RedisItemFactory;
|