UNPKG

2.78 kBJavaScriptView Raw
1var EventEmitter = require('events').EventEmitter;
2var util = require('util');
3
4/**
5 * Transforms the argument to a string
6 */
7var stringify = function (value) {
8 return typeof(value) === "object" ?
9 JSON.stringify(value) :
10 value + '';
11};
12
13/**
14 * Constructor of the main class RedisItem
15 */
16var RedisItem = function (type, expire) {
17 // We keep type so we don't have to use instanceof maybe this
18 // can be changed to something more clever
19 this.type = type || 0;
20 this.expires = expire || -1;
21};
22
23/**
24 * Constructor of a string
25 */
26var RedisString = function (value, expires) {
27 RedisItem.call(this, "string", expires);
28 this.value = String(value);
29};
30util.inherits(RedisString, RedisItem);
31
32/**
33 * Constructor of a buffer
34 */
35var RedisBuffer = function (value, expires) {
36 RedisItem.call(this, "buffer", expires);
37 this.value = (value instanceof Buffer) ? value : new Buffer(value);
38};
39util.inherits(RedisBuffer, RedisItem);
40
41/**
42 * Constructor of an hash
43 */
44var RedisHash = function () {
45 RedisItem.call(this, "hash");
46 this.value = {};
47};
48util.inherits(RedisHash, RedisItem);
49
50
51var RedisList = function () {
52 RedisItem.call(this, "list");
53 this.value = [];
54};
55util.inherits(RedisList, RedisItem);
56
57RedisList.prototype.rpush = function (values) {
58 for (var i = 0; i < values.length; i++) {
59 this.value.push(stringify(values[i]));
60 }
61};
62
63RedisList.prototype.lpush = function (values) {
64 for (var i = 0; i < values.length; i++) {
65 this.value.unshift(stringify(values[i]));
66 }
67};
68
69RedisList.prototype.rpop = function (value) {
70 return this.value.pop();
71};
72
73RedisList.prototype.lpop = function (value) {
74 return this.value.shift();
75};
76
77/**
78 * Constructor of a set
79 */
80var RedisSet = function () {
81 RedisItem.call(this, "set");
82 this.value = [];
83}
84util.inherits(RedisSet, RedisItem);
85
86/**
87 * Constructor of a sortedset
88 */
89var RedisSortedSet = function () {
90 RedisItem.call(this, "zset");
91 this.value = {};
92}
93util.inherits(RedisSortedSet, RedisItem);
94
95var 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
106RedisItemFactory.createString = function (elt, expire) {
107 return new RedisString(elt, expire);
108};
109
110RedisItemFactory.createBuffer = function (elt, expire) {
111 return new RedisBuffer(elt, expire);
112};
113
114RedisItemFactory.createHash = function () {
115 return new RedisHash();
116};
117
118RedisItemFactory.createList = function () {
119 return new RedisList();
120};
121
122RedisItemFactory.createSet = function () {
123 return new RedisSet();
124}
125
126RedisItemFactory.createSortedSet = function () {
127 return new RedisSortedSet();
128}
129
130/**
131 * Export the constructor
132 */
133module.exports = exports = RedisItemFactory;