1 | //- JavaScript source code
|
2 |
|
3 | //- defs-redis.js ~~
|
4 | //
|
5 | // The first step here was just to get things running with plain old arrays in
|
6 | // JavaScript, and I accomplished that. The next step is to jettison anything
|
7 | // that hinders performance, since it's downright sinful to choose a platform
|
8 | // like Node.js + Redis and then squander cycles needlessly ...
|
9 | //
|
10 | // ~~ (c) SRW, 23 Nov 2012
|
11 | // ~~ last updated 21 Mar 2014
|
12 |
|
13 | (function () {
|
14 | ;
|
15 |
|
16 | // Pragmas
|
17 |
|
18 | /*jshint maxparams: 2, quotmark: single, strict: true */
|
19 |
|
20 | /*jslint indent: 4, maxlen: 80, node: true */
|
21 |
|
22 | // Declarations
|
23 |
|
24 | var cluster, redis, url;
|
25 |
|
26 | // Definitions
|
27 |
|
28 | cluster = require('cluster');
|
29 |
|
30 | redis = require('redis');
|
31 |
|
32 | url = require('url');
|
33 |
|
34 | // Out-of-scope definitions
|
35 |
|
36 | module.exports = function (options) {
|
37 | // This function needs documentation.
|
38 | var collect_garbage, conn, db, exp_date, get_avar, get_list, set_avar;
|
39 |
|
40 | collect_garbage = function () {
|
41 | // This function needs documentation.
|
42 | var remaining, seek_and_destroy;
|
43 | seek_and_destroy = function (queue) {
|
44 | // This function needs documentation.
|
45 | var box = queue.replace(/^\$\:([\w\-]+)[&][\w\-]+/, '$1');
|
46 | db.smembers(queue, function (err, response) {
|
47 | // This function needs documentation.
|
48 | if (err !== null) {
|
49 | console.error('Error:', err);
|
50 | return;
|
51 | }
|
52 | var callback, n;
|
53 | callback = function () {
|
54 | // This function needs documentation.
|
55 | n -= 1;
|
56 | if (n === 0) {
|
57 | remaining -= 1;
|
58 | if (remaining === 0) {
|
59 | console.log('Finished collecting garbage.');
|
60 | }
|
61 | }
|
62 | return;
|
63 | };
|
64 | n = response.length;
|
65 | response.forEach(function (key) {
|
66 | // This function needs documentation.
|
67 | db.exists(box + '&' + key, function (err, response) {
|
68 | // This function needs documentation.
|
69 | if (err !== null) {
|
70 | console.error('Error:', err);
|
71 | }
|
72 | if (response === 0) {
|
73 | db.srem(queue, key, function (err) {
|
74 | // This function accepts a second argument,
|
75 | // but I have omitted it because it irritates
|
76 | // JSLint et al. otherwise.
|
77 | if (err !== null) {
|
78 | console.error('Error:', err);
|
79 | }
|
80 | return callback();
|
81 | });
|
82 | } else {
|
83 | callback();
|
84 | }
|
85 | return;
|
86 | });
|
87 | return;
|
88 | });
|
89 | if (n === 0) {
|
90 | callback();
|
91 | }
|
92 | return;
|
93 | });
|
94 | return;
|
95 | };
|
96 | db.keys('$:*', function (err, queues) {
|
97 | // This function needs documentation.
|
98 | if (err !== null) {
|
99 | console.error('Error:', err);
|
100 | return;
|
101 | }
|
102 | remaining = queues.length;
|
103 | queues.forEach(seek_and_destroy);
|
104 | if (remaining === 0) {
|
105 | console.log('Finished collecting garbage.');
|
106 | }
|
107 | return;
|
108 | });
|
109 | return;
|
110 | };
|
111 |
|
112 | conn = url.parse(options.redis);
|
113 |
|
114 | db = redis.createClient(conn.port, conn.hostname, {
|
115 | // For more information, see http://git.io/PRZ7Bw .
|
116 | detect_buffers: false,
|
117 | enable_offline_queue: true,
|
118 | no_ready_check: true,
|
119 | parser: 'hiredis',
|
120 | return_buffers: false,
|
121 | socket_nodelay: true
|
122 | });
|
123 |
|
124 | exp_date = function () {
|
125 | // This function needs documentation.
|
126 | return Math.ceil(options.avar_ttl);
|
127 | };
|
128 |
|
129 | get_avar = function (params, callback) {
|
130 | // This function needs documentation.
|
131 | db.hget(params[0] + '&' + params[1], 'body', callback);
|
132 | return;
|
133 | };
|
134 |
|
135 | get_list = function (params, callback) {
|
136 | // This function needs documentation.
|
137 | db.smembers('$:' + params[0] + '&' + params[1], callback);
|
138 | return;
|
139 | };
|
140 |
|
141 | set_avar = function (params, callback) {
|
142 | // This function needs documentation.
|
143 | var body, box, key, status;
|
144 | if (params.length === 4) {
|
145 | body = params[3];
|
146 | status = params[2];
|
147 | } else {
|
148 | body = params[2];
|
149 | }
|
150 | box = params[0];
|
151 | key = params[1];
|
152 | db.hget(box + '&' + key, 'status', function (err, res) {
|
153 | // This function needs documentation.
|
154 | if (err !== null) {
|
155 | return callback(err, res);
|
156 | }
|
157 | var multi, set1, set2, updated;
|
158 | multi = db.multi();
|
159 | updated = {body: body};
|
160 | if (res !== null) {
|
161 | set1 = '$:' + box + '&' + res;
|
162 | multi.srem(set1, key);
|
163 | }
|
164 | if (status !== undefined) {
|
165 | updated.status = status;
|
166 | set2 = '$:' + box + '&' + updated.status;
|
167 | multi.sadd(set2, key);
|
168 | }
|
169 | multi.hmset(box + '&' + key, updated);
|
170 | multi.expire(box + '&' + key, exp_date());
|
171 | multi.exec(callback);
|
172 | return;
|
173 | });
|
174 | return;
|
175 | };
|
176 |
|
177 | db.auth(conn.auth.split(':')[1]);
|
178 |
|
179 | db.on('connect', function () {
|
180 | // This function needs documentation.
|
181 | if (cluster.isMaster) {
|
182 | console.log('API: Redis storage is ready.');
|
183 | }
|
184 | return;
|
185 | });
|
186 |
|
187 | db.on('end', function () {
|
188 | // This function needs documentation.
|
189 | console.log('(Redis client closed)');
|
190 | return;
|
191 | });
|
192 |
|
193 | db.on('error', function (message) {
|
194 | // This function needs documentation.
|
195 | console.error('Error:', message);
|
196 | return;
|
197 | });
|
198 |
|
199 | process.on('exit', function () {
|
200 | // This function needs documentation.
|
201 | db.quit();
|
202 | console.log('(Redis client released)');
|
203 | return;
|
204 | });
|
205 |
|
206 | return {
|
207 | collect_garbage: collect_garbage,
|
208 | get_avar: get_avar,
|
209 | get_list: get_list,
|
210 | set_avar: set_avar
|
211 | };
|
212 | };
|
213 |
|
214 | // That's all, folks!
|
215 |
|
216 | return;
|
217 |
|
218 | }());
|
219 |
|
220 | //- vim:set syntax=javascript:
|